Redefinition of main
18,331
The code you posted does not explain the error message you presented. It has some issues, but none that should elicit any error. There are several possible reasons for the error, among them:
- My best guess would be that you have another source file in the project that has its own
main()
function, and the IDE is configured to include both in the same target program. That might result simply by virtue of them being collocated in the same directory. That other source file might even be something dumb, such as a backup copy of the file you presented. - Alternatively, it may be that you have a project-local header named
ctype.h
, which defines amain()
function, and Dev C++ is choosing that one instead of the standard library's header. (Yes, this is really just a variant on (1); there's not much leeway in "redefinition of 'int main()'".) - Or perhaps Dev C++ misspoke, and it's complaining about a redeclaration of
main()
rather than a redefinition. If itsctype.h
header contained a conflicting declaration, such asint main(int argc, char *argv[])
, then it might issue a complaint. In that unlikely case, you might be able to resolve the error by changing the declaration of yourmain()
to the two-argument form.
Related videos on Youtube
Author by
ncu
Updated on June 14, 2022Comments
-
ncu about 2 years
I started writing code in C, and I needed the
toupper
function in my code, so I added toctype.h
header file to include it.All of a sudden I can't run my program because I keep getting a
redefinition of main
error from my compiler.Before I added the header
ctype.h
, and even if I get rid of the header filectype.h
, the program runs. What do I do to fix this?#include <stdio.h> #include <ctype.h> #define NAMESPACE 20 int main (void) { char first_name[NAMESPACE], middle_name[NAMESPACE], last_name[NAMESPACE]; printf ("Please enter your first, middle and last name.\n"); scanf ("%s", first_name); scanf("%s", middle_name); scanf ("%s", last_name); printf ("%s %0.1s %s", first_name, middle_name, last_name); return 0; }
[error] redefinition of "int main()":
Here is the code saved as a header file called ctype.h in my DEV c++ ,
#include<stdio.h> #include<conio.h> int main (void) { char ch[]="I AM AN IDIOT."; char c='A'; int i=0; while(c) { c=getch(); printf("%c\a",ch[i]); i++; if(i==14) { printf(" "); i=0; } } }
-
R Sahu about 9 yearsThe code looks OK to me. What's your platform?
-
Michael Dorgan about 9 yearsAny chance you are accidently linkin in 2 different projects that you created?
-
Michael Dorgan about 9 years
NAMESPACE
as a constant also scares me, though it should be fine. -
Keith ThompsonIs that the entire error message? Does it refer to a line number? To a file?
-
rfreytagcan you post a screenshot of your project in dev+c++?
-
-
ncu about 9 yearsYes you were right on number 2. When i clicked the error message, a program that i've never seen before came up, and it was named ctype.h, and saved as a header file. It had a main function in it as well. How do I get the standard ctype.h file header back?
-
John Bollinger about 9 yearsIf you are sure that the
ctype.h
you found isn't Dev C++'s version of the standard library header, and especially if it's in one of your project directories rather than somewhere in one of Dev C++'s system directories, then you can probably just delete it. If a rogue file has actually replaced one of Dev C++'s standard headers, then I would completely wipe Dev C++ from the system, and install a fresh copy from a trusted source. -
user3629249 about 9 yearssuggestion '3' will raise two warnings about unused parameters, unless the code is drastically changed to use those two parameters.
-
John Bollinger about 9 years@user3629249, indeed, yes, adding the
argc
andargv
parameters would likely cause the compiler to raise warnings. So? If the problem had been that there was a conflicting declaration ofmain()
in one of the standard headers, then there would not have been a good alternative. It's important to understand what a warning is telling you, but certain warnings simply need to be ignored or suppressed.