๐Ÿ—๏ธ 2. Structure of a C Program

๐ŸŽ“ How to Learn This Lesson

Do not memorize a C program line by line. Instead, learn to identify the role of every part of a program: comments โ†’ preprocessor directives โ†’ declarations โ†’ main() โ†’ statements โ†’ function calls โ†’ return.

Once you understand the structure, you should be able to look at an unfamiliar C program and explain what each part is doing.

2.1 What Is the Structure of a C Program?

A C program is a collection of declarations, definitions, statements, expressions, functions and other language elements arranged according to the rules of the C language.

There is no single mandatory six-section template that every C program must follow. Textbooks often teach a traditional structure such as documentation, header files, global declarations, main() and user-defined functions because it is useful for beginners, but real C programs can be organized in different ways.

๐Ÿ’ก The important idea

Instead of memorizing a fixed template, learn to recognize the purpose of each component. For example, #include belongs to preprocessing, main() is the entry function of a hosted C program, and statements inside a function describe actions performed by that function.

2.2 Big Picture of a C Source File

A beginner-friendly C source file can look like this:

/* Documentation / comment */

#include <stdio.h>

#define MAX_VALUE 100

int globalValue = 10;

void displayMessage(void);

int main(void)
{
    int number = 20;

    printf("Number = %d\n", number);
    displayMessage();

    return 0;
}

void displayMessage(void)
{
    printf("Hello from another function!\n");
}
C PROGRAM
โ”‚
โ”œโ”€โ”€ Comments
โ”‚
โ”œโ”€โ”€ Preprocessor directives
โ”‚      โ”œโ”€โ”€ #include
โ”‚      โ””โ”€โ”€ #define
โ”‚
โ”œโ”€โ”€ Global declarations / definitions
โ”‚
โ”œโ”€โ”€ Function declarations
โ”‚
โ”œโ”€โ”€ main()
โ”‚      โ”œโ”€โ”€ local declarations
โ”‚      โ”œโ”€โ”€ statements
โ”‚      โ””โ”€โ”€ return
โ”‚
โ””โ”€โ”€ Other function definitions
๐ŸŽฏ Important

Not every program contains every item shown above. A small program may contain only #include, main(), some statements and return 0;. A large program may contain many header files, structures, functions, macros, global objects and multiple source files.

2.3 Documentation and Comments

Comments are notes written for programmers. They are ignored as comments during translation and do not become executable statements.

Type Syntax Purpose
Single-line comment // comment Useful for a short explanation.
Multi-line comment /* comment */ Useful when an explanation spans multiple lines.
#include <stdio.h>

int main(void)
{
    // Display a message
    printf("Hello, C!\n");

    /*
       The program ends
       after printing the message.
    */

    return 0;
}
โš ๏ธ Common confusion

Comments do not make a program execute differently simply because the comment contains programming instructions. For example:

// printf("Hello");

The commented-out printf() is not executed.

2.4 Preprocessor Directives

Preprocessor directives begin with #. They are processed before the compiler translates the resulting C source.

๐Ÿ“ฆ #include Requests inclusion of a header's contents according to preprocessing rules.
๐Ÿ”ข #define Defines a macro.
๐Ÿ”€ #if Controls conditional compilation.
๐Ÿ“ #ifdef Checks whether a macro is defined.
#include <stdio.h>

#define PI 3.14159

int main(void)
{
    printf("PI = %.2f\n", PI);

    return 0;
}
โš ๏ธ Important

A preprocessor directive is not a normal C statement. For example, #include <stdio.h> does not need a semicolon.

2.5 Header Files and #include

A header file commonly provides declarations, macros, types and other information that source files need in order to use an interface.

#include <stdio.h>

int main(void)
{
    printf("Hello\n");

    return 0;
}
Part Meaning
#include Preprocessor directive.
<stdio.h> Standard input/output header.
printf() Standard library function used for formatted output.
โš ๏ธ Header inclusion is not library linking

The header provides declarations and related information to the source code. It does not mean that the header itself contains the executable implementation that is linked into your program.

2.6 The main() Function

In a hosted C environment, the program entry function is main. A common standard form for a program that does not use command-line arguments is:

int main(void)
{
    return 0;
}
Part Meaning
int The function returns an integer value.
main The designated program entry function.
void Indicates that this form of main accepts no arguments.
{ } Contains the function body.
return 0; Returns a successful termination status from main.
๐ŸŽฏ Standard C point

For a hosted C program, prefer standard forms such as int main(void) or int main(int argc, char *argv[]). Do not teach void main() as standard hosted C.

2.7 Function Body and Curly Braces

The braces { and } define a compound statement. For a function definition, they surround the function body.

int main(void)
{
    printf("Hello\n");

    return 0;
}
๐Ÿง  Think of braces as a boundary
int main(void)
      โ”‚
      โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚       function body   โ”‚
โ”‚                       โ”‚
โ”‚   statements           โ”‚
โ”‚   declarations         โ”‚
โ”‚   expressions          โ”‚
โ”‚                       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The statements inside the braces belong to that function body.

2.8 Declarations Inside a Program

A declaration tells the compiler about an identifier and its type or other properties. For example:

int age;
double salary;
char grade;
Declaration Meaning
int age; Declares age as an integer object.
double salary; Declares salary as a double object.
char grade; Declares grade as a character object.
๐Ÿ’ก Why declarations matter

The compiler needs information about identifiers so that it can understand how expressions use them and check whether those uses are valid according to C's rules.

2.9 Statements

A statement represents an action or control operation performed by the program. Examples include expression statements, selection statements, iteration statements and jump statements.

int x = 10;

x = x + 5;

printf("%d\n", x);
Statement Purpose
int x = 10; Declaration with initialization.
x = x + 5; Assignment expression statement.
printf(...); Function-call expression statement.
return 0; Jump statement that returns from the function.
2.10 Expressions Inside Statements

An expression is a combination of operands and operators that can be evaluated according to C's expression rules.

int a = 10;
int b = 20;

int sum = a + b;

Here:

  • a and b are operands.
  • + is an operator.
  • a + b is an expression.
  • int sum = a + b; is a declaration with initialization.
๐ŸŽฏ Important distinction

Do not treat expression and statement as identical concepts. An expression can be part of a statement.

2.11 Function Calls

A function call requests that a function be executed with the supplied arguments.

printf("Hello\n");

The structure can be understood as:

printf Function name
("Hello\n") Argument
; Ends the expression statement

Later, when you learn functions, you will see how to create your own functions and call them from main().

2.12 return and Program Status

The statement:

return 0;

returns the value 0 from main. For a hosted environment, returning from main terminates the program and provides a termination status to the host environment.

โš ๏ธ Common confusion

return 0; does not mean โ€œprint zeroโ€. It returns a value from the function.

๐Ÿง  Think of it like this
main()
  โ”‚
  โ”‚ performs program work
  โ”‚
  โ†“
return 0;
  โ”‚
  โ†“
function ends
  โ”‚
  โ†“
successful termination status
2.13 User-Defined Functions

Large programs should not put all logic inside main(). Programmers can divide functionality into separate functions.

#include <stdio.h>

void greet(void);

int main(void)
{
    greet();

    return 0;
}

void greet(void)
{
    printf("Welcome to CodeBhavya!\n");
}
๐Ÿ“ข Function declaration void greet(void);
โ–ถ Function call greet();
๐Ÿ—๏ธ Function definition void greet(void) { ... }
2.14 Complete Anatomy of a C Program
/* Program description */

#include <stdio.h>

#define MESSAGE "Hello, CodeBhavya!"

int total = 100;

void displayMessage(void);

int main(void)
{
    int number = 10;

    printf("%s\n", MESSAGE);
    printf("Number = %d\n", number);

    displayMessage();

    return 0;
}

void displayMessage(void)
{
    printf("Learning C step by step!\n");
}
Part Example Role
Comment /* Program description */ Documentation for programmers.
Header inclusion #include <stdio.h> Makes standard I/O declarations available.
Macro definition #define MESSAGE ... Defines a preprocessing macro.
Global definition int total = 100; Defines an object at file scope.
Function declaration void displayMessage(void); Declares a function before its use.
main() int main(void) Program entry function in a hosted environment.
Local declaration int number = 10; Defines a local object.
Function call displayMessage(); Calls another function.
Function definition void displayMessage(void) { ... } Provides the implementation of the function.
2.15 Declaration vs Definition

This distinction becomes extremely important as you progress into functions, global variables and multiple source files.

Concept Example Idea
Declaration void greet(void); Tells the compiler about the function.
Definition void greet(void) { ... } Provides the function implementation.
Object definition int count = 10; Defines an object and gives it storage.
โš ๏ธ Why this matters

A compiler may accept a function call when it has seen a proper declaration, but the final program still needs an appropriate definition when linking. This is one reason declarations and definitions must not be confused.

2.16 Why Can a Function Definition Appear Before or After main()?

Consider this program:

#include <stdio.h>

void greet(void);

int main(void)
{
    greet();

    return 0;
}

void greet(void)
{
    printf("Hello!\n");
}

The function definition appears after main(), but the compiler already knows about greet() because of its declaration:

void greet(void);

The same function can be defined before main():

#include <stdio.h>

void greet(void)
{
    printf("Hello!\n");
}

int main(void)
{
    greet();

    return 0;
}
๐ŸŽฏ Core rule

Before a function is used in a way that requires a declaration, the compiler needs an appropriate declaration visible at that point. A function definition also serves as a declaration.

2.17 How the Program Executes

A useful beginner mental model is to separate the source structure from the execution flow.

Source code
    โ”‚
    โ†“
Preprocessing
    โ”‚
    โ†“
Compilation
    โ”‚
    โ†“
Object / executable generation
    โ”‚
    โ†“
Operating system loads program
    โ”‚
    โ†“
main()
    โ”‚
    โ”œโ”€โ”€ declarations
    โ”‚
    โ”œโ”€โ”€ statements
    โ”‚
    โ”œโ”€โ”€ function calls
    โ”‚
    โ””โ”€โ”€ return
    โ”‚
    โ†“
Program termination
๐Ÿ’ก Important distinction

The source file is read and translated before normal execution. During execution, the running program follows its control flow starting from the program's entry point.

2.18 Why Does C Use Semicolons?

The semicolon ; is used to terminate many C statements.

int x = 10;

x = x + 5;

printf("%d\n", x);

return 0;
โš ๏ธ But not every C construct ends with a semicolon

For example, a function definition does not end with a semicolon after its closing brace:

void greet(void)
{
    printf("Hello");
}

Similarly, the if, for and while constructs have their own syntax rules.

2.19 Whitespace and Indentation

Spaces, tabs and newlines are generally used to make source code readable. C does not require a particular indentation style.

int main(void)
{
    int a = 10;
    int b = 20;

    printf("%d\n", a + b);

    return 0;
}

The indentation does not change the meaning of this simple program. It improves readability for humans.

๐ŸŽฏ Good programming practice
  • Use consistent indentation.
  • Keep related statements visually grouped.
  • Use meaningful names.
  • Avoid unnecessarily long lines.
  • Use comments to explain important reasoning.
2.20 C Is Case-Sensitive

C distinguishes between uppercase and lowercase letters.

int age = 20;

printf("%d", age);

These identifiers are different:

age
Age
AGE
aGe
โš ๏ธ Common beginner mistake

Writing:

Printf("Hello");

instead of:

printf("Hello");

will not refer to the same identifier.

2.21 Complete Program โ€” Read It Like a Programmer
#include <stdio.h>

int add(int a, int b);

int main(void)
{
    int x;
    int y;
    int result;

    printf("Enter two numbers: ");
    scanf("%d %d", &x, &y);

    result = add(x, y);

    printf("Sum = %d\n", result);

    return 0;
}

int add(int a, int b)
{
    return a + b;
}
Question Answer
Where is the standard I/O header included? #include <stdio.h>
Where is the function declaration? int add(int a, int b);
Where does the program begin? main()
Where are local variables declared? Inside main().
Where does input happen? scanf()
Where is the function called? result = add(x, y);
Where is the function defined? After main().
What does add() return? The sum of a and b.
2.22 Common Structural Mistakes
Mistake Problem Correct idea
Missing #include <stdio.h> Standard I/O declarations may be unavailable. Include the appropriate header when required.
Using void main() Not a standard hosted C form. Use int main(void).
Missing semicolon Many statements require termination. Check the statement syntax.
Missing } Function or block structure becomes incomplete. Match braces carefully.
Calling a function without an appropriate visible declaration The compiler may not have enough information to process the call correctly. Declare the function before its use when needed.
Confusing header with library Misunderstands preprocessing and linking. Separate header declarations from library implementation/linking.
Putting executable code outside functions Ordinary executable statements belong inside function bodies. Place statements inside an appropriate function.
2.23 Common Beginner Confusions
Confusion Correct Understanding
โ€œEvery C program must have exactly six sections.โ€ The six-section model is a teaching convention, not a universal language requirement.
โ€œ#include is a function.โ€ #include is a preprocessor directive.
โ€œstdio.h contains printf's executable code.โ€ The header provides declarations and related information; implementation is supplied by the library/toolchain.
โ€œreturn 0 prints zero.โ€ return 0; returns a value from the function.
โ€œmain() must always be written with empty parentheses.โ€ int main(void) explicitly indicates no arguments.
โ€œA function must always be defined before main().โ€ A suitable declaration can allow its definition to appear later.
โ€œAll declarations must always come before every statement.โ€ Modern C permits declarations and statements to be arranged more flexibly within blocks, subject to the language rules.
โ€œComments are executed.โ€ Comments are ignored as comments during translation.
2.24 Traditional Textbook Structure vs Actual C
Traditional Teaching Model Actual Meaning
Documentation section Comments and program documentation.
Link section Usually refers to header inclusion using #include.
Definition section May include macros or other definitions.
Global declaration section Declarations/definitions at file scope.
main() section The program's entry function in hosted C.
Subprogram section User-defined function definitions.
๐ŸŽฏ Interview-ready answer

If an interviewer asks about the โ€œstructure of a C program,โ€ you can explain the traditional model, but also mention that it is a convenient organizational model, not a mandatory six-part template imposed on every C source file.

2.25 C Program Structure Memory Map
๐Ÿง  Remember the structure like this
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚       COMMENTS              โ”‚
โ”‚  What is this program?      โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚       PREPROCESSOR          โ”‚
โ”‚  #include / #define         โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚       DECLARATIONS          โ”‚
โ”‚  Types / functions / names  โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚          main()             โ”‚
โ”‚  Program entry function     โ”‚
โ”‚                             โ”‚
โ”‚  โ”œโ”€โ”€ local declarations     โ”‚
โ”‚  โ”œโ”€โ”€ input                  โ”‚
โ”‚  โ”œโ”€โ”€ processing             โ”‚
โ”‚  โ”œโ”€โ”€ function calls         โ”‚
โ”‚  โ”œโ”€โ”€ output                 โ”‚
โ”‚  โ””โ”€โ”€ return                 โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚   OTHER FUNCTION DEFINITIONSโ”‚
โ”‚   Reusable program logic    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

2.26 Quick Revision

๐Ÿ“ Comments Explain code to humans.
โš™๏ธ Preprocessor Handles directives such as #include and #define.
๐Ÿ“ฆ Header Provides declarations and related interface information.
๐Ÿš€ main() Entry function of a hosted C program.
๐Ÿ“‹ Declaration Introduces an identifier and relevant type information.
โ–ถ Statement Represents an action or control operation.
๐Ÿงฉ Function Packages reusable program logic.
โ†ฉ๏ธ return Returns control/value from a function.
๐Ÿ“Œ Remember the big picture
  • A C source file contains different language elements rather than one rigid template.
  • #include is a preprocessor directive.
  • main() is the program entry function in a hosted C environment.
  • Declarations tell the compiler about identifiers and their types/properties.
  • Statements describe actions or control operations.
  • Functions divide programs into manageable units.
  • A declaration and a definition are not always the same thing.
  • Header inclusion and library linking are different stages.
  • Good indentation improves readability.
  • C is case-sensitive.

2.27 Practice Questions

1 What is the purpose of main() in a hosted C program?
2 What is the purpose of #include?
3 What is the difference between a declaration and a definition?
4 Why is int main(void) commonly preferred over void main()?
5 Does #include perform library linking?
6 Why are braces used in a C function?
7 What does return 0; mean in main()?
8 Can a function definition appear after main()?
9 Why is C called case-sensitive?
10 Is the traditional six-section structure mandatory for every C program?
INTERVIEW PREPARATION

๐ŸŽค Structure of C Program โ€” Interview Questions

These questions focus on the concepts interviewers commonly test when checking whether a candidate understands C program structure rather than simply memorizing syntax.

1. What are the major components of a C program?
2. What is the difference between #include and linking?
3. Why is main() declared as int?
4. What is a function declaration?
5. Can main() call a function defined later in the file?
6. What is the role of a header file?
7. Why does a C program use semicolons?
8. What is the difference between a compile error and a linker error?
PLACEMENT TIPS

๐Ÿ’ก Structure of C Program โ€” Placement Tips

  • Be able to explain preprocessing, compilation, assembly and linking in simple language.
  • Know the difference between a function declaration and a function definition.
  • Remember that #include is a preprocessor directive, not a function.
  • Use int main(void) when teaching a program with no command-line arguments.
  • Never confuse return 0; with printing zero.
  • Understand why a function can be defined after main() when a suitable declaration appears earlier.
  • Know the difference between a header and a library implementation.
  • Do not memorize the traditional six-section structure as a mandatory C rule.
EXTRA PRACTICE

โœ๏ธ Structure of C Program โ€” Extra Practice Questions

  1. Write a simple C program and identify every major component.
  2. Explain the difference between #include and a function call.
  3. Write a program where a user-defined function is declared before main() and defined after main().
  4. Find and correct three structural errors in a small C program.
  5. Draw the complete path: source โ†’ preprocessing โ†’ compilation โ†’ linking โ†’ execution.
  6. Explain why void main() should not be presented as standard hosted C.
  7. Identify declarations, definitions, expressions and statements in a given program.
  8. Write a program containing two user-defined functions and explain the role of each.
โ† Previous Topic: Introduction to C Next Topic: Variables & Constants โ†’