468,539 Members | 1,487 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,539 developers. It's quick & easy.

error: redefinition of ‘main’

Hi

here is the error I got after executing the program

prog.c:12:5: error: redefinition of ‘main’
int main() {
^~~~
prog.c:3:5: note: previous definition of ‘main’ was here
int main() {
^~~~

#include <stdio.h>

int main() {
#include <stdio.h>

return 0;
}
Jan 9 '18 #1
2 13923
weaknessforcats
9,207 Expert Mod 8TB
Function names must be unique. You cannot have two of the same name because the compiler can't determine which one to call. The error has nothing to do with the name "main". It has only to do with having more than one function with the same name.

BTW: You show an include of stdio.h inside main. All includes should be outside any functions.
Jan 9 '18 #2
donbock
2,422 Expert 2GB
The error messages suggest function main is defined twice in your source file (perhaps once on line 3 and again on line 5). However, the code snippet you provided shows only a single definition of main. If in fact your code only defines main once then something else is going on.

Perhaps this error is an artifact of including <stdio.h> twice ... although doing so ought not to cause any errors.

Perhaps the error does have something to do with the name "main". The C Standard requires that the definition of main be compatible with one of the following function prototypes:
  1. int main(void);
  2. int main();
  3. int main(int argc, char *argv[]);
The second of these is legal but deprecated. Perhaps your compiler warnings are set to an extremely pedantic level that doesn't like you using the deprecated form of main.
Jan 10 '18 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by cody | last post: by
70 posts views Thread by junky_fellow | last post: by
2 posts views Thread by ThomasKA | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.