Lesson 6 +10 XP

A Few Common C Problems

A Few Common C Problems

Every beginner trips on the same few things. Here's how to read the walls you'll hit.

Problem 1: The window closes instantly

You run the program and the console flashes away. That is success: the program finished quickly and the window closed.

Fix: run it from a terminal that stays open, or add a line that waits before the program ends.

printf("Press enter to continue...");
getchar();

Problem 2: Missing semicolon

Forgot a ;? The compiler complains with something like "expected ; at end of instruction".

Fix: check the end of the last line.

Problem 3: Missing return 0

Most functions should return a value. main should end with return 0; so the OS knows the program did successfully.

Problem 4: Wrong header

Typing #include <stdio> (without .h) or wrong names will confuse the compiler. Remember <stdio.h>.

Problem 5: Case sensitivity

C is case-sensitive: main vs Main are two different things.

Read the errors

Compilers points at the file and line number (e.g. main.c:3). Fix the first error first - one error often causes many others.

TL;DR

  • Instant window close = your program ran and ended.
  • Each statement needs a semicolon ;.
  • return 0; at the end of main reports success.
  • C is case-sensitive; main must be lowercase.
  • Start debugging from the first error and its line number.