Main Function

In a C program, the main() function is the entry point. The program execution starts with the main() function. It 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 any 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 of C main() Function

The following is the syntax of the main() function in C language −

intmain(){//one or more statements;return0;}

Syntax Explained

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.

Valid Signatures of C main() Function

The signatures (prototype) of a main() function may be −

intmain(){..return0;}

Or

intmain(void){..return0;}

Or

intmain(int argc,char*argv[]){..return0;}

Example of C main() Function

The following example demonstrates the main() function:

#include <stdio.h>intmain(){// Write code from hereprintf("Hello World");return0;}

Important Points about C main() Function

  • A C program must have a main() function.
  • The 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() returns 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.

How does main() Works in C?

The program’s execution starts from the main() function as it is an entry point of the program, it starts executing the statements written inside it. 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.

The exit() in C main() Function

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: Using exit() in the main() Function

#include <stdio.h>#include <stdlib.h>intadd(int,int);intmain(){int i;for( i =1; i<=5; i++){if( i ==3){printf(" \n exiting ..");exit(0);}elseprintf(" \n Number is %d", i);}return0;}

Output

Number is 1
Number is 2
exiting ..

Command-line Arguments with main() in C

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 −

Syntax

The following is the syntax of main() function with command-line arguments:

intmain(int argc,char*argv[]){..return0;}

Argument Definitions

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.

Example: Using main() Function with Command-line Arguments

Consider the following program to understand command−line arguments.

#include <stdio.h>#include <stdlib.h>intadd(int,int);intmain(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);}return0;}

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 1020
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.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *