2.3 STRUCTURE OF A C PROGRAM
Before we discuss about structure of a C program let us see a simple C program which does not perform any operation and only prints “Hello World” message:
1. program header comment2. preprocessor directives (if any)3. int main ( void )4. {5. statement(s);6. return 0 ;7. }
/* This is my First C Program */
then statement “This is my First C Program” will be treated as comment. In any program the program header comment always comes first.
ii) Preprocessor Directives:
Preprocessor directives are special statements that is included in the program code as you have seen in section 2.3.1 of this unit. These directives are not program statements but special instructions(directives) for the preprocessor and on the basis of these directives some activities are performed before actual program code compilation. For example #include <stdio.h> is called preprocessor directive (command).It directs the preprocessor to include all the functions defined in stdio.h in the program before it compiles the code written in the program. For more clarity please see the following prinf() statement:
printf(“Hello World\n”); statement in program given in previous page function printf( ) is defined in stdio.h and has been included in that program.
You remember that a preprocessor directive is written in only a single line of code. As soon as a newline character ‘\n’ is found, the preprocessor directive is considered to end.
No semicolon (;) is used for the end of a preprocessor directive as in the case of other statement like: printf(“Hello World\n”); or return 0;.
In general “a preprocessor is a program which processes the input data and produce output that is used as input to another program”.
stdio.h – The #include <stdio.h> directive indicates the preprocessor to include a copy of the standard input/output header file stdio.h in the program code.
iii) int main ( void ): Generally every program has a function called main. This is the point from where program execution begins. The main () is placed in the source code file as the first function for the purpose of readability. In your program there must be a function with this name otherwise c compiler can not successfully compile and link your program.
Prototype for writing a function is:
return type function_name ( arguments)
A function may take some value for performing operations. The value(s) given to a function is known as arguments to the function. For example suppose you have written a function:
int sum( int a, int b)
for finding sum of two integers. This function return an integer and take two integers as argument it means you can pass two values of integers to the function sum. In side parenthesis ( ) after main, void is written, it means nothing is there inside ( ).
iv) The Function Body of main( ):The main function starts with a left brace (curly bracket){
begins the body of every function. A corresponding right brace } ends the function body. In side { and } are the statements to be performing the operation. For example if you are writing a program to find the area of a circle in C, then operations need to be performed for this purpose will be written inside {and }.
v) Statement(s): These are the main portion of any program. Statements written in a program decides about the activity to be performed such as input operations, output operations, logical operations etc.
vi) return 0; : Because function main() returns an integer value, there must be a statement that indicates what this value is. The last statement is return 0 ;. It ends with a semicolon. Also remember that all statements in C end with a semicolon.
After learning about structure of a C program, now let us see the basic components of C programming. These components are used for writing C programs for solving problems.
Next section is going to cover almost all the basic components of C programming. This section will give you idea for using basic components in the program you will write.

Comments
Post a Comment