Quick Start with C



Programming in C in 7 days!
By: Siamak Sarmady

Part–1: Quick Start with C:
programming language is perhaps the most popular programming language. C was created in 1972 by Dennis Ritchie at the Bell Labs in USA as a part of UNIX operating system. C was also used to develop some parts of this operating system. From that time C programming language has been the de facto programming language when fast programs are needed or the software needs to interact with the hardware in some way. Most of the operating systems like Linux, Windows™, and Mac™ are either developed in C language or use this language for most parts of the operating system and the tools coming with it.

This course is a quick course on C Programming language. In our first lesson we will first write our first C program. We will then learn about printing to screen, variables and functions. We assume that you are familiar with at least one of the popular operating systems.

For this course you can use the following compilers or Programming Environments.
• Gcc and cc in Unix and Linux operating systems
• Borland C or Turbo C in DOS operating system or in Command line environment of windows operating system
• “Bloodshed Dev-Cpp” integrated development environment (IDE) gives you a complete and compact programming environment. It comes with “MinGW” and “GCC” C Compilers and you should not need anything else for this course.

We use “Bloodshed Dev-Cpp” for this course and we suggest you also use it. “Bloodshed Dev-Cpp” is free and it can be downloaded from the website http://www.bloodshed.net (currently under the URL http://www.bloodshed.net/dev/devcpp.html).

Your first C program:
Let's write our first C program.

Example 1-1: example1-1.c
#include
main()
{
printf("Hello World!\n");
system("pause"); //this line is only needed under windows
}

Details of Test program

• #include
Tells C compiler to include the file "stdio.h" in this point of your C program before starting compile step. This "include file” contains several definitions, declarations etc.

• main()
C program consist of one or more functions. Functions are building blocks of C programs. main() function is different from other functions by that it is the start point of execution. Our program contains only function while complicated programs may contain thousands.

• {
O pening brace marks the start of a block. Closing brace will mark its end. This one marks main () function start
• printf("Hello world!");
This line of code prints the statement between quotation marks on your output screen. \n tells program to start a new line in output screen.
• Each command line in C ends with ";" character. Control statements are exceptions. You will soon be able to determine when you must use ; to end a line of code.

• system(“pause”);
The output window will close in Windows™, immediately after program execution has been finished. In this way you will not be able to see results of the execution (as it happens very fast).  We have put this command to pause the window and wait for a keystroke before closing the window.  You can remove this line from our examples if you do not use Windows operating system. This command actually sends the “pause” command to windows operating system and windows runs the its “pause” command at this point. We will learn more about this command in later lessons.

• }
closes main() function.

This program contains only one function while complicated programs may contain several functions.

Data Types and Variables:
C uses several data types of data. These include characters, integer numbers and float numbers. In C language you must declare a variable before you can use it. By declaring a variable to be an integer or a character for example will let computer to allocate memory space for storing and interpreting data properly.

Naming a variable:
It is better that you use meaningful names for your variables even if this causes them to become long names. Also take this in mind that C is case sensitive. A variable named "COUNTER" is different from a variable named "counter".

Functions and commands are all case sensitive in C Programming language. You can use letters, digits and underscore _ character to make your variable names. Variable names can be up to 31 characters in ANSI C language. The declaration of variables must take place just after the opening brace of a block. For example we can declare variables for main() function as below code:
main()
{
int count;
float sum,area;
.
.
.
}
First character in a variable name must be a letter or an underscore character. It cannot be a C programming language-reserved word (i.e. Commands and pre defined function names etc). An example for using variables comes below:

Example 1-2:example1-2.c
#include
main()
{
int sum;
sum=12;
sum=sum+5;
printf("Sum is %d",sum);
system("pause");
}

General form for declaring a variable is:
Type name;

The line sum=sum+5; means: Increase value of sum by 5. We can also write this as sum+=5; in C programming language. printf function will print the following:

Sum is 17

In fact %d is the placeholder for integer variable value that its name comes after double quotes.

Common data types are:
int        integer
long    long integer
float    float number
double            long float
char    character

Other  placeholders are:
%d decimal integer
%ld decimal long integer
%s string or character array
%f float number
%e double (long float)

printf () function used in this example contains two sections. First section is a string enclosed in double quotes. It is called a format string. It determines output format for printf function. Second section is "variable list" section.

We include placeholders for each variable listed in variable list to determine its output place in final
output text of printf function.

Control characters
As you saw in previous examples \n control character makes a new line in output. Other control characters are:
\n New line
\t tab
\r carriage return
\f form feed
\v vertical tab

Multiple functions
Look at this example:

Example 1-3:example1-3.c
#include
main()
{
printf("I am going inside test function now\n");
test();
printf("\nNow I am back from test function\n");
system("pause");
}
test()
{
int a,b;
a=1;
b=a+100;
printf("a is %d and b is %d",a,b);
}

In this example we have written an additional function. We have called this function from inside main function. When we call the function, program continues inside test () function and after it reached end of it, control returns to the point just after test() function call in main(). You see declaring a function and calling it, is an easy task. Just pay attention that we used ";" when we called the function but not when we were declaring it.

Loading
Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Flying Twitter Bird Widget By ICT Sparkle