Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Main function with three arguments [duplicate]

Tags:

c

I ran into a function like this earlier:

int main(int argc, char **argv, char **argw){


}

Why is there a need for three arguments, and how does this actually work?

like image 691
user1090614 Avatar asked Feb 21 '14 16:02

user1090614


People also ask

How many arguments are passed in main () function?

The main() function has two arguments that traditionally are called argc and argv and return a signed integer.

How many arguments is too many for a function?

Functions with three arguments (triadic function) should be avoided if possible. More than three arguments (polyadic function) are only for very specific cases and then shouldn't be used anyway.

Can we pass arguments in main ()?

Yes, we can give arguments in the main() function. Command line arguments in C are specified after the name of the program in the system's command line, and these argument values are passed on to your program during program execution. The argc and argv are the two arguments that can pass to main function.

How many arguments can pass through a function?

Except for functions with variable-length argument lists, the number of arguments in a function call must be the same as the number of parameters in the function definition. This number can be zero. The maximum number of arguments (and corresponding parameters) is 253 for a single function.


1 Answers

The third argument to main is normally called envp.

int main(int argc, char **argv, char **envp) {

Many compilers provide a third argument to main, but it is not specified in the C standard, so using it is undefined behaviour. If you try to port the code to a platform that doesn't provide a third parameter the program will most likely fail.

Is char *envp[] as a third argument to main() portable

like image 62
Klas Lindbäck Avatar answered Sep 27 '22 18:09

Klas Lindbäck