The First C Program

Before we begin with our first C program do remember the following rules that are applicable to all C programs:

  • Each instruction in a C program is written as a separate statement. Therefore a complete C program would comprise of a series of statements.
  • The statements in a program must appear in the same order in which we wish them to be executed.
  • Blank spaces may be inserted between two words to improve the readability of the statement.
  • All statements are entered in small case letters.
  • However, no blank spaces are allowed within a variable, constant, or keyword.
  • Every C statement must end with a (;). Thus; acts as a statement terminator.

Program  /* Calculation of simple interest */

main( )
{
int p, n ;
float r, si ;
p = 1000 ;
n = 3 ;
r = 8.5 ;
/* formula for simple interest */
si = p * n * r / 100 ;
printf ( "%f" , si ) ;
}

Now understand the above program…

  • Comment about the program should be enclosed within /* */.
  • main( ) is a collective name given to a set of statements. This name has to be main( ), it cannot be anything else. All statements that belong to main( ) are enclosed within a pair of braces { } as shown below.
main( )
{
statement 1 ;
statement 2 ;
statement 3 ;
}
  • Any variable used in the program must be declared before using it. For example, int p, n ;
float r, si ;
  • Any C statement always ends with a ;

For example,

float r, si ;
r = 8.5 ;
  • All output to screen is achieved using readymade library functions. One such  function is printf( ). We have used it display on the screen the value contained in si. The general form of printf( ) function is,
printf ( "<format string>", <list of variables> ) ;
<format string> can contain,
%f for printing real values
%d for printing integer values
%c for printing character values

In addition to format specifiers like %f, %d, and %c the format string may also contain any other characters. These characters are printed as they are when the printf( ) is executed.