C
Basic Structure of C & Preprocessor Directives
From Compilation to Program Structure

What is written inside a C program?

We already know that a compiler translates C code. Now we will study the structure of that code.

Where does execution of a C program normally begin?
Begin with the earlier flow: C Source Code → Compiler → Executable → Output. Ask students what they recognize in the code shown on the right.
first_program.c
#include <stdio.h> int main(void) { printf("Hello, Students!"); return 0; }
Build the program

Basic Structure of a C Program

A C program is organized into clearly defined sections.

hello.cStep 1 of 6
Step 1
Do not reveal the full program immediately. Pause at every step and ask what students think the next line should be.
Program Anatomy

Sections of a C Program

circle_area.cProgram sections
/* Program to calculate circle area */ #include <stdio.h> #define PI 3.14159 int main(void) { float radius = 2.0f; float area = PI * radius * radius; printf("Area = %.2f", area); return 0; }
Program Structure
Program Anatomy
Each section has a specific purpose in the program.
Most C statements end with a semicolon, but preprocessor directives do not.
Ask students to name the section before clicking. Keep the distinction simple: declaration creates/describes something; a statement performs an action.
The main function

Understanding int main(void)

1

int

The function returns an integer value to the operating environment.

int main(void)

main

Program execution normally begins from the main function.

main
( )

void

This form indicates that no arguments are being received by the function.

(void)
{ }

Braces

They mark the beginning and end of the function body.

;

Semicolon

Most C statements end with a semicolon.

0

return 0;

It ends main() and indicates successful completion.

Use the standard beginner form int main(void). Avoid teaching void main().
Preprocessing Stage

Preprocessing Before Compilation

Directives beginning with # are handled before normal compilation.

C Source File
program.c
Preprocessor
handles # directives
Expanded Source
directives processed
Compiler
translates C code
Core sentence: “The preprocessor handles lines beginning with # before normal compilation.”
Header Inclusion

Using #include

Header files provide declarations required by library functions.

Use stdio.h

Provides the declaration of printf()
include_demo.c
Header is available. Click “Check Program”.

Remember

#include <stdio.h>

Angle brackets are normally used for standard or system headers.

#include "myheader.h"

Quotation marks are commonly used for a header created in our own project.

A preprocessor directive begins with # and normally does not end with a semicolon.
For beginners, say that stdio.h makes the declaration of printf available. Do not say that it copies the whole library.
Macro Definition

Using #define

Choose the value of PI

macro_demo.c
#define PI 3.14159 float radius = 2.0f; float area = PI * radius * radius;

Expanded line

The preprocessor performs textual replacement before normal compilation.

No = sign and no semicolon are used in this simple object-like macro definition.
Ask students what the line becomes before clicking Replace. Mention that macro names are often written in uppercase by convention.
Common Programming Errors

Incorrect and Correct C Program

Small syntax and structure mistakes can prevent successful compilation.

incorrect.c5 errors
include <stdio.h> #define VALUE = 10; void main() { printf("Value = %d", VALUE) return 0; }
Error 1: First line
Error 2: Macro definition
Error 3: main function
Error 4: printf statement
Error 5: Macro punctuation
Five corrections are available.
Let the class work for one minute first. Then reveal one correction at a time.
Program Sequence

Correct Order of a C Program

The program sections follow a logical and readable order.

Available lines

Your program

This checks whether students remember the order: include, main, opening brace, statement, return, closing brace.
Knowledge Review

C Program Structure and Preprocessor Quiz

Question 1 of 6
Ask students to answer aloud before clicking. Repeat any item answered incorrectly by most of the class.
One-minute recap

Remember this structure

Comments
#include / #define
main() Function
Statements + return 0;

Preprocessor directives are handled before normal compilation, and program execution normally begins from main().

Finish by asking students to write the complete Hello Students program without looking at the screen.