Chapter 2: Your First C Program
1. Your First C Program – “Hello, World!”
This is the classic first program every C programmer writes. It simply prints “Hello, World!” on the screen.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// My first C program – Hello, World! // Date: January 20, 2026 #include <stdio.h> // Library needed for printf int main() // Program starts here { printf("Hello, World!\n"); // Prints the message printf("Learning C is fun!\n"); // Extra line for fun return 0; // Program ends successfully } |
Output when you run it:
|
0 1 2 3 4 5 6 7 |
Hello, World! Learning C is fun! |
2. Structure of a C Program (The Basic Layout)
Every C program follows this fixed structure:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
// 1. Comments (notes for humans – computer ignores them) /* 2. Preprocessor Directives – always at the top */ #include <stdio.h> // Most common library for input/output // #include <math.h> // For math functions (if needed) // #define MAX 100 // Define constants (optional) // 3. Global Declarations (optional – outside main) // int globalNumber = 50; // void myFunction(); // Function declaration // 4. Main Function – compulsory – program starts here! int main() // Entry point of every C program { // 5. Statements – your actual code goes here printf("Hello from C!\n"); int number = 25; // Example variable printf("My favorite number is %d\n", number); // 6. Return statement – ends the program return 0; // 0 means "everything was successful" } // Closing brace – main function ends |
Key Points to Remember:
- Preprocessor lines (#include, #define) come first
- Exactly one int main() function is required
- Every statement ends with a ; (semicolon)
- Code inside { } (curly braces)
3. Compilation and Execution Process
(How your code becomes a running program)
C is a compiled language – so your code goes through these steps:
- Write the source code You create a file like hello.c
- Preprocessing Compiler replaces #include files, removes comments, handles #define (This is automatic – you don’t see it)
- Compilation Converts C code → Assembly code Command: gcc -S hello.c → creates hello.s
- Assembly Converts assembly → Machine code (binary) Creates object file: hello.o
- Linking Combines your object file + standard libraries (like printf from stdio.h) Creates final executable file
- Execution Run the executable → see output on screen
Most common single command (does everything):
|
0 1 2 3 4 5 6 |
gcc hello.c -o hello |
- gcc = compiler
- hello.c = your source file
- -o hello = name of final executable file
Run the program:
- On Linux / macOS:
text0123456./hello
- On Windows:
text0123456hello.exe
If you forget -o:
|
0 1 2 3 4 5 6 |
gcc hello.c |
It creates default executable: a.exe (Windows) or a.out (Linux/macOS)
4. Comments in C
Comments are notes you write for yourself or others – the computer completely ignores them.
Two Types:
Type 1: Single-Line Comment
|
0 1 2 3 4 5 6 7 |
// This is a single-line comment printf("Hello"); // This comment is after the code on the same line |
Type 2: Multi-Line Comment
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
/* This is a multi-line comment. You can write many lines here. Very useful for explaining big parts of code. Author: Your Name Date: January 20, 2026 */ |
Best Practice Example – Full Program with Good Comments
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/* Program: Hello World with Personal Message Purpose: My very first C program Author: [Your Name] Date: January 20, 2026 */ #include <stdio.h> // Required for printf function int main() // Main function – program starts here { // Print a welcome message with decoration printf("************************************\n"); printf(" Hello, World! \n"); printf("************************************\n"); // Print some personal info printf("I am learning C programming!\n"); printf("It is fun and very powerful! 🚀\n"); return 0; // End program successfully } |
Practice Tasks for Today
- Copy the Hello, World! program above and run it.
- Add your own message and some comments.
- Try removing a ; (semicolon) on purpose – compile and see the error.
- Write a small program that prints your name and favorite hobby – add comments to every important line.
