Structure Of C Program With Example. main() in C . printf in C .

Form of a C Program :

  • Each instruction in a C program is written as a separate statement.
  • The statements in a program must appear in the same order in which we wish them to be executed; unless of course, the logic of the problem demands a deliberate ‘jump’ or transfer of control to a statement, which is out of sequence.
  • Blank spaces may be inserted between two words to improve the readability of the statement. However, no blank spaces are allowed within a variable, constant, or keyword.
  • All statements should be in small case letters.
  • C has no specific rules for the position at which a statement is to be written in a given line. That’s why it is often called a free-form language.
  • Every C statement must end with a; Thus; acts as a statement terminator.

A simple C program

/*Calculation of simple interest*/
int main()
int p,n;
float r,si;
p = 1000;
n = 3;
r = 8.5;
/*formula for simple interest*/
si = pnr/100;
return 0;

What is main()

main() form the crucial part of any C program.

  • main() is a function. A function is nothing but a set of statements. In a C program, there can be multiple functions. All statements that belong to main() are enclosed within a pair of braces {} as shown below.
int main()
statement 1;
statement 2;
  • main() function always returns an integer value, hence there is an int before main(). The integer value that we are returning is 0. 0 indicates success. If for any reason the statements in main() fail to do their intended work we can return a non-zero number from main(). This would indicate failure.
  • Some compilers like Turbo C/C++ even permit to return nothing from main(). In such a case we should precede it with the keyword void.

printf() and its Purpose

C does not contain any instruction to display output on the screen. All output to the screen is achieved using readymade library functions. One such function is printf().

  • Once the value of si is calculated it needs to be displayed on the screen. We have used printf() to do so.
  • For us to be able to use the printf() function, it is necessary to use #include<stdio.h> at the beginning of the program. #include is a pre-processor directive.
  • The general form of printf() function is,
printf(“<format string>”,<list of variables>);

<format strinng> can contain,

%f for printing real values
%d for printing character values
%c for printing character values

                          Examples of usage of printf() function:

printf(“Simple interest = Rs. %f”,si);
printf(“Principal = %d\nRate = %f”,p,r);

Leave a Comment