1

Why my C program doesn't work when I declare the main function like this (I inverted the arguments position) :

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

I compiled it without problem but I got errors when I run it.

Thanks.

rabah Rachid
  • 89
  • 1
  • 8
  • Another one for you to try: `int main(int argv, char **argc)` — someone accidentally wrote that in a class I was teaching back in the days before there were C compilers that supported prototypes at all. It took several minutes to spot the problem. As noted in the answers, GCC and other modern compilers will identify this problem for you if you enable appropriate warnings. – Jonathan Leffler Oct 26 '13 at 15:22

3 Answers3

3

Due to the incorrect main() signature, this is not a valid C program.

Try enabling compiler warnings. My compiler tells me about the problem:

$ gcc -Wall test.c 
test.c:1:5: warning: first argument of 'main' should be 'int' [-Wmain]
test.c:1:5: warning: second argument of 'main' should be 'char **' [-Wmain]

See What are the valid signatures for C's main() function?

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

Unlike in C++, functions in C are solely identified by their names, not their arguments. E.g. linker will be pretty happy when it sees a 'main' function.

Nevertheless, there are certain assumptions how main() is called by the operating system resp. the runtime environment. When your parameters are wrong, your program will see unexpected values and might crash.

And btw, you probably will see errors or warnings when you enable diagnostics (e.g. '-Wall -W') when building the program.

ensc
  • 6,704
  • 14
  • 22
1

This is an incorrect main() signature. You may check the main function.

The parameters argc, argument count, and argv, argument vector,1 respectively give the number and values of the program's command-line arguments. The names of argc and argv may be any valid identifier in C, but it is common convention to use these names.

Also check What should main() return in C and C++?

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331