error: redefinition of ‘main’

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vinaykumaar
    New Member
    • Jan 2018
    • 1

    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;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      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.

      Comment

      Working...