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

9th Feb 2019, 11:43 AM
Anna
Anna - avatar
12 Réponses
+ 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 };
9th Feb 2019, 1:05 PM
Schindlabua
Schindlabua - avatar
+ 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.
9th Feb 2019, 10:33 PM
Sonic
Sonic - avatar
+ 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 ,
10th Feb 2019, 5:13 PM
Jerald Lashy Jeffery
Jerald Lashy Jeffery - avatar
+ 1
Ok thanks
11th Feb 2019, 1:14 PM
Kavya
Kavya - avatar
0
salom
12th Feb 2019, 8:20 PM
Karim Yangiboyev
Karim Yangiboyev - avatar
0
Atlana
12th Feb 2019, 8:34 PM
Karim Yangiboyev
Karim Yangiboyev - avatar
- 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 };
11th Feb 2019, 4:43 AM
Huzaifa usman
Huzaifa usman - avatar