C - Hello World
Your First C Program
Every C program starts at main
. Let’s print a message and learn how to compile/run from the terminal.
Learning Objectives
- Write, compile, and run a simple C program.
- Understand the role of
#include
andmain
.
Prerequisites
Code
// hello.c
#include <stdio.h>
int main(void)
{
printf("Hello World!\n");
return 0;
}
Compile and Run
gcc -std=c11 -Wall -Wextra -O0 -g hello.c -o hello
./hello
# Output: Hello World!
Common Pitfalls
- Forgetting a newline (
\n
) at the end of the printed line. - Missing semicolons after statements.
Checks for Understanding
- What header provides
printf
? - What is the return type of
main
?
Show answers
<stdio.h>
int
Practice
- Modify the program to print your name on a new line.
- Print the result of
2 + 3
usingprintf
.