Your first C program
8 min lesson
- A "Hello, World!" program is the simplest complete C program; it prints one line of text and exits.
- It teaches you the minimum structure every C program needs: a main function and a way to output text.
- #include <stdio.h> tells the compiler to include the standard input/output library, which provides printf.
- int main(void) is the entry point; execution starts at the first line inside its braces { }.
- printf is a function that prints formatted text to the screen; \n inside the string means "move to a new line."
- return 0; tells the operating system that the program finished successfully; 0 means success.
- Every statement inside main ends with a semicolon (;), which is like a period at the end of a sentence.
- The compiler translates this code into machine instructions; when you run the executable, the computer follows those instructions and prints the text.
- This tiny program is your first proof that your compiler and editor are working correctly.
- Once you can run Hello World, you can start changing the text and adding more lines.
- Every C program needs a main function; execution starts there.
- Use #include <stdio.h> and printf to print text.
- End each statement with a semicolon and use \n to start a new line.
- If your program does not compile, check for missing semicolons and typos first; mistakes are normal and fixable.
// This program prints a friendly greeting to the screen.
// You will see one line of text when you run it.
#include <stdio.h> // Include the standard input/output library for printf
// The main function is where the program starts running
int main(void) {
// Print the greeting and move to a new line at the end
printf("Hello, World!\n");
// Tell the operating system the program finished successfully
return 0;
}What happens: Hello, World!
Breaking it down
#include <stdio.h>
#include <stdio.h>This line tells the compiler to bring in the standard input/output library, which contains printf. Without it, the compiler would not know what printf means.
int main(void)
int main(void)This declares the main function, the starting point of every C program. The int means it returns a whole number to the operating system, and void means it takes no input.
printf("Hello, World!\n");
printf("Hello, World!\n");printf prints the text inside the quotes. The \n is an escape sequence that moves the cursor to the next line. Without it, the next output would appear on the same line.
return 0;
return 0;This ends the main function and returns 0 to the operating system, which signals success. A non-zero value usually means something went wrong.
A helpful analogy
Writing Hello World is like saying your first word in a new language. It is short and simple, but it proves you can communicate. The compiler is like a translator that checks your sentence and turns it into something the computer understands.
Nesting rules
- 1.Every opening brace { must have a matching closing brace }.
- 2.Statements inside main must be inside the braces; code outside any function will not compile.
- 3.The semicolon goes at the end of a statement, not after the opening brace or before the closing brace.
Common mistakes
Forgetting the semicolon after printf
Avoid
printf("Hello, World!\n")Use instead
printf("Hello, World!\n");The compiler expects a semicolon to mark the end of a statement. Without it, you get an error like 'expected ; before return'.
Misspelling printf or main
Avoid
prinf("Hello");Use instead
printf("Hello");C is case-sensitive and spelling-sensitive. A typo makes the compiler think you are calling an unknown function.
Using the wrong slash for newline
Avoid
printf("Hello/n");Use instead
printf("Hello\n");The newline escape is a backslash followed by n: \n. A forward slash /n prints a literal /n instead of moving to a new line.
Quick tips
- •Change the text inside the quotes and run the program again to see the output change.
- •Try removing the \n and see how the output differs (it may merge with your terminal prompt).
- •Compile with gcc -Wall -Wextra main.c -o main to see helpful warnings.
- •If you see 'command not found' when running ./main, make sure you are in the same folder as the executable.
- •Keep this program as a template; you can copy it whenever you start a new C file.