C Hello World Program
Writing Your First C Program
Create a file named hello.c. In C, the filename can be anything, but it's a good practice to use something meaningful. Here, we use hello.c for our Hello World example.
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Hello, World!
Compiling the Program
Open a terminal or command prompt where your hello.c file is saved and run the following command:
gcc hello.c -o hello
What happens during compilation?
The gcc command invokes the GNU Compiler Collection for C. Here's what it does:
- Reads your
.csource file — written in human-readable C code. - Checks for syntax errors and compiles it into machine code.
- Generates an executable file named
hello.
This executable is now ready to be run on your operating system.
Running the Program
To run the compiled program, use the following command in your terminal:
./hello
What happens during execution?
The system executes the compiled machine code in the hello binary:
- It loads the program into memory.
- Starts execution from the
mainfunction. - Prints
Hello, World!to the console.
Hello, World!
You’ve now compiled and executed your first C program — a foundational step in learning the C programming language.
Common Issues to Watch For
- Missing semicolons: C statements must end with
;. - Case sensitivity: C is case-sensitive —
mainandMainare different. - Missing headers: Functions like
printfrequire#include <stdio.h>.
Comments
Loading comments...