The main function in the C programming language is necessary for
programs to run and to be compiled. In the main function program starts it's execution, making this
function an entry point to your program. The main function, just like any other function in C, is opened
and closed with curly braces. Within these curly braces, then, tasks are performed, such as calling of
other functions, definitions of variables, printing of information and so on.
Return type of the portable main function is int, hence an integer has to be returned from this function.
Note: returned integer from the main function indicates exit status of the program.
2. What are the different types of the main
function in C?
In the C programming language, there are quite a number of ways of defining the main function. Different types
of main functions can be grouped into the following two categories:
The portable main functions in C are supported by all operating systems. These main functions can be
compiled and can run on any OS that has a C compiler.
int main(void);
int main(int argc, char *argv[]);
The difference between these two types of portable functions lays in the parameters' part. In the case of
int main(void), a single parameter is void. Meaning that this main function does not receive any
command-line arguments that are passed during the execution of the program.
In contrast with int main(int argc, char *argv[]). This type of
portable main function receives two parameters from the command-line. First parameter is int argc, which indicates the number of arguments that were
provided in the command-line during the execution. Assuming our program was executed in such a way:
./a.out "one argument" "another argument", then argc in the
main program would receive the integer value of 3 (1. ./a.out, 2. "one argument", 3. "another
argument").
Second parameter is char *argv[]. Which is an array of pointers
that points to each argument written in the command-line during execution. Given your program is
executed with ./a.out "one argument" "another argument", then
argv would contain these four elements.
Non-portable main functions are those main functions in C that compile and run only on a limited number
of operating systems and are not fully supported. Here are two examples of such functions below:
void main(void);
int main(int argc, char *argv[], char *env[]);
The void main(void) function does not return any data because the
return type of this function is void nor does it receive any
data from the command-line.
Note: return statement within functions with a return type of void can be used without specification of
return value ( return ; )
The int main(int argc, char *argv[], char *env[]) function has
three parameters. Two of which, we have already discussed
above. Third parameter is mostly supported in UNIX systems. This parameter (envp) receives a two-dimensional array. This array, in turn,
contains pointers to the environment variables and is terminated by a NULL pointer. To test if your
operating system supports this type of main function and to print out environment variables, feel free to compile and run the code below.
Example 1:.
#include<stdio.h>
intmain(intargc,char *argv[],char*envp[])
{
inti = 0;
while(envp[i]!=NULL)
{
printf("%s\n",envp[i++]);
}
return0;
}
To summarize different types of the main function in C, we could assert that C offers multiple ways to
enter programs. This can be utilized and tailored to the specific needs of your program.
3. Quiz of the reader's knowledge
Quiz : Main Function in C
1Which main function is non-portable?
2Can a program run and be compiled without main function?
3What does the parameter argv of the main function contain?