423,109 Members | 2,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,109 IT Pros & Developers. It's quick & easy.

error: redefinition of ‘main’

P: 1
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
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,150
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

Expert 100+
P: 2,373
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.