• C Programming Video Tutorials

C - Main Function



In a C program, main() function is the entry point. The program execution starts with the main() function. The main() function is designed to perform the main processing of the program, and clean up any resources that were allocated by the program. In a C code, there may be number of functions, but it must have a main() function. Irrespective of its place in the code, it is the first function to be executed.

Syntax

The main() function is defined as per the following syntax −

int main(){
   //one or more statements;
   return 0;
}

As a part of its syntax, a function has a name that follows the rules of forming an identifier (starting with an alphabet or underscore and having alphabet, digit or underscore). The name is followed by a parenthesis. Typically, the main() function is defined with no arguments, although it may have argv and argv argument to receive values from the command line.

The prototype of a main() function may be −

int main() {
   . .
   return 0;
}

Or

int main(void){
   . .
   return 0;
}

Or

int main(int argc, char *argv[]){
   . .
   return 0;
}

Though a program must have main() function, main is not a C keyword. It is classified as a user−defined function because its body is not pre−decided, it depends on the processing logic of the program. By convention, int is the return type of main(). The last statement in the function body of main() return 0, to indicate that the function has been successfully executed. Any non−zero return value indicates failure.

Some old C compilers let you define main() function with void return type. However, this is considered to be non−standard and is not recommended.

As compared to other functions, the main() function

  • Can't be declared as inline.
  • Can't be declared as static.
  • Can't have its address taken.
  • Can't be called from your program.

Other functions within the source program are defined to perform certain task. The main function can call any of these functions. When main calls another function, it passes execution control to the function, optionally passing the requisite number and type of arguments, so that execution begins at the first statement in the called function. The called function returns control to main when a return statement is executed or when the end of the function is reached. Note that return statement is implicitly present as the last statement when its return type is int.

A program usually stops executing when it returns from or reaches the end of main, although it can terminate at other points in the program for various reasons. For example, you may want to force the termination of your program when some error condition is detected. To do so, you can use the exit function.

Exit in main

The C exit() function is a standard library function used to terminate the calling process. Use exit(0) to indicate no error, and exit(1) to indicate that the program is exiting with because of an error encountered.

Example

#include <stdio.h>
#include <stdlib.h>

int add(int, int);

int main (){
   int i;
   for ( i = 1; i<=5; i++){

      if ( i == 3 ){
         printf  (" \n exiting ..");
         exit(0);
      }
      else

      printf  (" \n Number is %d", i);
   }

   return 0;
}

Output

Number is 1
Number is 2
exiting ..

Command−line arguments

Typically, the main() function is defined without any arguments. However, you may define main() with arguments to let it accept the values from the command line. In this type of usage, main() function is defined as follows −

int main(int argc, char *argv[]){
   . . 
   return 0;
}

The argument definitions are as follows −

argc − The first argument is an integer that contains the count of arguments that follow in argv. The argc parameter is always greater than or equal to 1.

argv − The second argument is an array of null−terminated strings representing command-line arguments entered by the user of the program. By convention, argv[0] is the command with which the program is invoked. argv[1] is the first command−line argument. The last argument from the command line is argv[argc − 1], and argv[argc] is always NULL.

Consider the following program to understand command−line arguments.

#include <stdio.h>
#include <stdlib.h>

int add(int, int);

int main (int argc, char *argv[]){
   int x, y, z;

   if (argc<3){
      printf("insufficient arguments");
   }
   else{
      x = atoi(argv[1]);
      y = atoi(argv[2]);
      z = x+y;
      printf("addition : %d", z);
   }
   return 0;
}

Just compile and build the program as test.c, don’t run from the IDE in which you have edited and compiled. Go to the command prompt and run the program as follows −

C:\Users\mlath>test 10 20
addition : 30

In this chapter, we learned the importance and syntax of defining a main() function in C. Any C program must have a main() function. As a convention it should return 0 to indicate successful execution. You can also define arguments to a main() function, they can be passed from the command−line.

Advertisements