+20

C/C++: Why doesn't int main() cause an error when I provide command line arguments?

So, obviously every C/C++ program needs a function int main() as an "entry point" for the OS. It can either be declared like this: int main() // no parameters {...} or like this: int main(int argc, char *argv[]) // parameters for command line arguments {...} The funny thing is that I can call both int main() WITH command line arguments (that will just be ignored) and int main(int argc, char *argv[]) WITHOUT command line arguments. I can even define int main(void) and explicitly tell the compiler that the main function doesn't accept any parameters and it'll still work without any problems if I provide command line arguments. How is that possible? If I declare a function int my_function(int, char*) and try to call it without providing exactly one int and one char* argument, there will be an error. In the same way, I can't declare a function int my_function(void) and call it with one or more arguments...

2/9/2019 11:43:17 AM

Anna

12 Answers

New Answer

+31

Anna I think this thread might help you: (Have a look) https://stackoverflow.com/questions/33317216/why-there-is-no-error-when-passing-command-line-arguments-when-declaring-main-as/33317423

+13

Your program will always be given at least one argument by the way (the program name), so argv is never empty. You can declare it as `int main()` because it is convenient, and the C standard explicitly says you can declare main in two possible ways. And to showcase that your OS calling your program, and a C function calling a C function is quite different, you can even do this: const int main[] = { -443987883, 440, 113408, -1922629632, 4149, 899584, 84869120, 15544, 266023168, 1818576901, 1461743468, 1684828783, -1017312735 };

+8

So it seems that the shell or 'runtime' has no strict checking of the main function signature, always passing command line arguments to main, and as Schindlabua said, there is always at least one argument argv[0]. I.e. argc>=1. Also it seems that whatever is the signature for main used by the programmer, the one with agrc and argv is implicitly always used by the compiler.

+7

Anna In C, int main() and int main(void) has different meanings. int main() means, main function can be called either with any number of arguments or no arguments. int main(void), means main should be called with no arguments. Also note that main in C and C++ have few differences like in C it is legal to call main within main i.e int main() { main(); } In C++ it is illegal to call main within main, doing that leads to undefined behavior.

+3

Its the main method and cannot return an argument error because of the program name

+3

After tried that question for time from 3 different compilers,! i saw that the output is mostly characterised by the kind of compiler you are using ... What i think is that for such trivial programs use at least 3 different compilers so as to make sure that the errors are compiler based. Imagine if you are still using a 2006 compiler for Morden programs ..... Obvious the results will be biased since some compilers are under developed ,

+2

Uppalapu void main() was never a default. It was never even a part of C Standard. However some compilers support it through extensions. The only correct definition of main are 1. int main(); 2. int main(int argc, char ** argv or char *argv[]) Some compilers even supports a third form int main(int argc, char** argv, char ** env) the third variable is for specifying the environment variables which includes path settings, memory related settings etc. Early C supported main definition without return type i.e main() or main(void). In fact in early C, the return type of the function(any function including main) that does not mention the return type, was considered implicitly as int. If you want to write portable code stick to the Standard.

+2

Aman Please do not pollute someone else thread by posting out of context posts. Thankyou.

+1

Ok thanks

0

0

-1

Your program will always be given at least one argument by the way (the program name), so argv is never empty. You can declare it as `int main()` because it is convenient, and the C standard explicitly says you can declare main in two possible ways. And to showcase that your OS calling your program, and a C function calling a C function is quite different, you can even do this: const int main[] = { -443987883, 440, 113408, -1922629632, 4149, 899584, 84869120, 15544, 266023168, 1818576901, 1461743468, 1684828783, -1017312735 };