Give general structure of C program. Explain with an example program.
A C program is essentially a group of instructions that are to be executed as a unit in a given order to perform a particular task.
Each C program must contain a main() function. This is the first function called when the program starts to run or execute.
A C program is traditionally arranged in the following order but not strictly as a rule.
/* Comment lines */ Preprocessor Directives [Global Declaration] int main()
{
[Local Declarations] [Executable statements]
}
[User-defined Functions] Example Program:
Consider first a simple C program which simply prints a line of text to the computer screen. This is traditionally the first C program you will see and is commonly called the “Hello World” program for obvious reasons.
#include <stdio.h> int main()
{
/* This is how comments are implemented in C to comment out a block of text */
// or like this for a single line comment printf( "Hello World\n" ) ;
return 0;
}
All C compiler include a library of standard C functions such as printf which allow the programmer to carry out routine tasks such as I/O operations, mathemetical operations, string operations etc. but which are not part of the C language, the compiled C code merely being provided with the compiler in a standard form.
Header files must be included which contain prototypes for the standard library functions and declarations for the various variables or constants needed. These are normally denoted by a .h extension and are processed automatically by a program called the Preprocessor prior to the actual compilation of the C program.
Therefore, The line #include <stdio.h>
Instructs the pre-processor to include the file stdio.h into the program before compilation so that the definitions for the standard input/output functions including printf will be present for the compiler.
As you can see this program consists of just one function the mandatory main function. The parentheses, ( ), after the word main indicate a function while the curly braces, { }, are used to denote a block of code -- in this case the sequence of instructions that make up the function.
Comments are contained within a /* ... */ pair in the case of a block(mutli-line) comment or a double forward slash, //, may be used to comment out single line.
The line printf("Hello World\n " ) ;
is the only C statement in the program and must be terminated by a semi-colon. The statement calls a function called printf which causes its argument, the string of text within the quotation marks, to be printed to the screen. The characters \n are not printed as these characters are interpreted as special characters by the printf function in this case printing out a newline on the screen. These characters are called escape sequences in C and cause special actions to occur and are preceded always by the backslash character, \ .
Global and Local Declaration statements are used to declare global and local variables, arrays, functions, pointers, etc.