Tuesday, March 31, 2009

The First C program

Now let us try to write a simple C program, to understand what we have read in our previous blogs.
We will star our program development skill, by writing a simple C program to calculate the average of three numbers. {also see: Rules for writing a C program}


Line No. Program statements
1 /* Calculation of the average of three given number */
2 #include<stdio.h>
3 #include<conio.h>
4 void main()
5 {
6 int a,b,c;
7 float avg;
8 a=5;
9 b=7;
10 c=13;
11 avg=(a+b+c)/3;
12 printf(“The average of %d, %d and %d is %f”, a,b,c,avg);
13 getch();
14 }


Explanation:


Line 1: This line shows the comment written in the program. Any thing written between /* and */ is the comment of the program.
Example:
/* xyz */
Or it may be written in multiple lines as shown below.
/* x
y
z */
While writing a comment, it should be kept in mind that if any command comes in between /*  and */ then it will also work as the comment and not as a function.
And a number of comments can be written at any place in a particular program.


Line 2 & 3: These lines shown the declaration of the header files where the definitions of the functions or the instructions are declared but not for the user defined functions. For example we use printf(), scanf() etc. in our program whose definitions are declared in stdio.h and conio.h header files. So in order to work with those functions we have to call those header files before the main() function. As we proceed further we will learn about different header files and their corresponding functions declared within them.


Line 4: Any C program is nothing but a combination of functions. And main() is one such function, empty parenthesis after main is necessary. A set of statements are written belonging to a particular function are written within a pair of braces.
Example:
main()
{
   statement 1;
   statement 2;
   statement 3;
   ----------------;
   statement n;
}
every function returns a value from where it is called. Thus, void is written before the main() function, so as to return nothing after executing the main() function. And if we don’t write void before the main function then we have to use return 0; at the end of the main().


Line 5: Its the opening brace of the main() function as described in the previous point.


Line 6 & 7: Before using a variable in the function or with in a function it has to be declared first.
For example:
int a,b,c;
float avg;
This line shows the declaration of the variable used in the program in the first of the main() function.


Line 8, 9 & 10: These lines shows the value declaration of the variables, it can also be done while declaring the types of variables.
For example:
int a=5; b=7; c=13;


Line 11: This line shows the logical operation of the program as per the flowchart of the program.


Line 12: This line shows the output operation. printf() function is used to print the values written within its braces to the output screen.


Line 13: In the line the statement getch() is used to pause the output screen till the user touches any key. Though getch() has different utility, still in some program it is used to pause the output screen. If we don’t use the getch() function then the user wouldn’t be able to see the output screen as the computer will process the program so first and will return to the previous window, getch() is an input function so as to hold the output window till the user enters any key. And thus the user gets enough time to hold down the output screen till he completes the observation of the output.


Line 14: This is the closing braces of the main() function.

No comments:

Post a Comment

Please give your valuable comments or queries if you fell its helpful or if you like the contents of the site. Thanks...