Skip to main content

The main Function

   
     The main Function

The main is a part of every C program. C permits diferent forms of main statement. Following forms are allowed.

  • main()
  • int main
  • void main()
  • main(void)
  • void main(void)
  • int main(void)

The empty pair of parentheses indicates that the function has no arguments. This may be explicitly indicated by using the keyword void inside the parentheses. We may also specify the keyword int or void before the word main. The keyword void means that the function does not return any information to the operating system and int means that the function retúrns an integer value to the operating system. When int is specified, the last statement in the program must be "return O". For the sake of simplicaty, we use the first form in our program.

Example:

                 
       
       // Programm of Addition
      // Written by Santosh Asole
     
    #include<stdio.h>
     main ()
     {
    int x, y, z;
      x=10;
      y=20;
       z=x+y;
       printf("Addition is = %d",z);
      }

This program when executed produce following output:

         Addition is = 30

The first two line of the program are comment line.Write comments in the program is a symptoms of a good programmer. It is a good practice to use comment line in the beginning to give information such as name of the program, author name, date, etc.

The words number is variable names that are used to store numeric data. The numeric data may be either in integer form or in real form. In C, all variables should be declared to tell the compiler what the variable name are and what type of data they hold. The variables must be declared before they are used. The declarations
                   int number;
tell the compiler that number is an integer (int)  number.Declaration statements must appear the beginning of the functions as shown in above program .All declaration statements end with a semicolon i.e. ";". C supports many other data types and they are discuss in detail in another page.
The words such as int  are called the keywords and cannot be used as variable names. A list of keywords is given in another page.

Data is stored in a variable by assigning a data value to it. This is done in above program. Integer value 10 is assigned to the integer variable x and an integer value 20 is assigned to the integer value y. The result of addition of two integer numbers 10 and 20 is assigned to the integer variable z. 
The statements
      x=10;
      y=20;
      z=x+y;
are called the asaignment statements.Every assignment statements must end with a semicolon.
The next statement is an output statement that print the value of z. 
The print statement 
                  printf("%d", z);
contains two arguments. The first argument "%d" tells compiler that the value of the second argument z should be printed as decimal integer.
Note that these arguments are seperated by comma.

Comments