C Programming
Master the fundamentals of C programming with comprehensive tutorials, examples, and hands-on exercises
Hello World Program in C
Let's create your first C program! The "Hello World" program is a simple program that displays "Hello, World!" on the screen.
Basic Hello World Program
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Code Explanation
#include <stdio.h>
This is a preprocessor directive that includes the standard input/output library, which contains the printf() function.
int main()
This is the main function where program execution begins. Every C program must have a main() function.
printf("Hello, World!\n");
This function prints the text "Hello, World!" to the screen. The \n is an escape sequence for a new line.
return 0;
This statement returns 0 to the operating system, indicating that the program executed successfully.
How to Compile and Run
Save the Code
Save the code in a file named hello.c
Compile
Open terminal and run: gcc hello.c -o hello
Execute
Run the program: ./hello (Linux/Mac) or hello.exe (Windows)
Output
When you run this program, you will see:
Hello, World!
Explanation
#include <stdio.h>: Brings in functions likeprintf()from the standard I/O library.int main(): Program entry point where execution begins. Must return anint.printf(): Prints text to the console;\nadds a newline so the shell prompt appears on the next line.return 0;: Signals successful completion to the operating system.
Keywords
Quick Tips
- Compile with
gcc hello.c -o helloand run./hello. - Every statement ends with a semicolon; missing it causes compile errors.
- Use meaningful file names and keep examples small while learning.