main ( ) in C language is the only function that can be defined once in a program. Therefore, each and every program written in C language has a definition of main ( ) function. This is because the grammar of the C language is written so that it allows only the main ( ) function in the program. main ( ) function can be defined with arguments and without arguments.
main ( ) function without argument:
int main ( )
{
}
mian ( ) function with argument:
int main(int argc, char *argv[ ])
{
}
argc – denotes number of arguments passed to main ( ) function.
argv – denotes a pointer that points to the first array element of argc+1 pointers, the last element of array of argc+1 is null. If argv[0] is non-null, it contains the name of the program. argv[0] is empty if the host environment does not reflect the name of the program.
argc is the argument count and argv is argument vector. The name of the arguments in function main ( ) are not keywords, that is argc and argv are not keywords so name argc and argv can be changed. Thus main ( ) function with the following declaration is equally valid:
int main(int acc, char** avv)
The return value from the main function indicates the termination status of the program. Successful termination indicated by 0 and the non-zero value indicates that the program terminated in failure.
The main( ) function marks the beginning of the program. The call to the main ( ) function is made when all the static and non-static objects are initialized. Thus, the main ( ) function is the predefined entry point of the program.
The main ( ) function has two parameters that can accept multibyte character strings passed using the command prompt. These parameters are also known as command-line arguments. The pointers arg[1] .. argv[argc-1] points to the first character of the passed string. argv[0] is the first character of the string which is null-terminated. This string beginning at argv[0] is used to call the program and begin program execution.
The argv array size is argc+1, and the end of the array is marked by argv[argc]. The value at argv[argc] is a null pointer. The properties possessed by the main ( ) function is as follows:
- The programmer can not specify the prototype of the program.
- The main ( ) function returns an integer value. Execution of the return statement is the same as the execution of the exit statement. On the execution of the return statement, the compiler of the language flushes and terminates all open connections of the streams and discards the temporary file created to execute the program. When this process is done, control is returned to the execution environment.
- If the return type of the main( ) function is not defined and the program terminates without executing the return statement, then the executing program’s termination status is undefined. On the other hand, if the program contains the main ( ) function definition having a return type integer and the executing program terminates without executing the return statement, then also control flushes and terminates all open connections of the streams and discards the temporary file created to execute the program. When this process is done, control is returned back to the execution environment. This is because the compiler considers the closing curly bracket of the main function to be a return statement.
It is often considered that the main ( ) function marks the beginning of the execution to be more precise the first function to be executed _start( ). The C runtime routine gives the _start ( ) function. This _start () function is automatically linked to the C program when it is compiled. Therefore, executing the main ( ) function highly relies on the operating system to get executed.
Not only C, but the Unix program also expects a return statement at the end of the program. The value returned at the end of the program is 0 on successful execution of the program and -1 when program execution is unsuccessful.
Structure of C program
/* 1 # include< name of the directory > */
/* 2 defines macros */
/* 3 external linking/declarations */
/* 4 typedefs */
/* 5 declaration of global variables */
/* 6 prototype of the function */
int main(int argc, char *argv[ ])
{
/*7 program instructions */
}
/* function decalration */
# includes<name_of_the_directory>
#include is a preprocessor directory that makes the compiler include a referenced file. Header files in the C program have .h extension and do not contain executable code.
defines macros
This is used to specify macros. Macros are defined by all capital letters, capital letters are used to distinguish macro from variables and function names.
External linking/declaration
This section declares variables that add the declared variable to the compilation file and makes available to the compiled program.
typedefs
typedefs are used for structures declaration, unions, and enumerations.
Global variable decelerations
Global variables are declared to be used in the main ( ) function and all the other functions declared in the program. Value of global variables persists throughout the programs.
Prototype of functions
A function prototype is declared before main ( ). It specifies what functions will be used in the program. Function definition is given after main ( ) function.
Program Instructions
Program instructions are written to achieve the desired objective. All the variables and function calls are written within the main ( ) function. The program logic is written using programming instructions and these instructions build main ( ).
Definition of function
Functions are used to attain the power of reusability. The declared function can be called from main( ) as well as from other functions.
When the C program is executed the operating system searches the beginning of the program from where the program execution should start. The search operating system ends at main ( ). The Operating System maintains an address table in which it marks the entry of the program with the address of the main ( ) function. All the functions which are declared in the program are associated with the main function. The Operating System links the address of all the functions with the address of the main ( ) function. Thus it makes an extra entry in the address table that contains a pointer to associated functions.
Think if there is no main then there will be no entry in the address table maintained by the operating system. If there will be no entry of main ( ) function in the address table of main ( ) function then there will be no entry of associated function in the address table maintained by the operating system. This will halt program execution, and the objective of writing a program will not be achieved.
Leave a Reply