How to Fix ‘Undefined Reference to main’
Error in C
This error means the compiler can't find the required main()
function in your C code. Let’s explore why it happens and how to fix it.

🔍 Why This Error Occurs
- Missing
main()
function in the source file - Misspelled or incorrectly defined
main()
- Compiling the wrong file or an empty file
- Using
main
inside a library file instead of a main application file
✅ How to Fix It
1. Make Sure You Have a Proper main()
Function
int main() {
return 0;
}
2. Don’t Misspell the Function
Wrong:
int Main() {
return 0;
}
Right:
int main() {
return 0;
}
3. Check File Selection in Online Compilers
If you're using an online compiler with multiple files, make sure the file that contains main()
is selected as the main entry file.
4. Don't Use main()
in a Header or Library File
Library files should not include main()
. Reserve main()
for the main source file only.
📌 Final Tip
The C compiler always looks for the main()
function to start program execution. If it’s not defined or not spelled correctly, you'll get this error.
🧑💻 Bonus: Correct C Program Template
#include <stdio.h>
int main() {
printf("Program compiled and ran successfully!\n");
return 0;
}
📎 Related Reads:
- Fix Segmentation Fault in Online C Compiler
- Run C Code Online Without Installing Anything
- Common C Programming Errors and How to Fix Them