C Notes
Complete explanation of C program structure — documentation section, preprocessor directives, global declarations, main function, user-defined functions, and how each section works together with practical examples.
Every C program follows a specific structure. Unlike Python where you can just write code anywhere, C has rules about how a program must be organized. Understanding this structure is the first step to writing correct C code.
The Big Picture
Here's the complete anatomy of a C program:
Complete Example with All Sections
Welcome to TempConverter v1.0 1. Celsius to Fahrenheit 2. Fahrenheit to Celsius Enter your choice (1 or 2): 1 Enter temperature: 100 100.00°C = 212.00°F Note: Above boiling point! Total conversions this session: 1
Section-by-Section Breakdown
Section 1: Documentation Section
This is a comment block at the top of your file describing the program. It's optional but considered professional practice:
/*
* Program: Student Grade Calculator
* Author: Rahul Sharma
* Date: March 15, 2024
* Purpose: Calculate and display student grades
* based on marks in 5 subjects
*
* Compilation: gcc -o grades grades.c
* Usage: ./grades
*/C supports two types of comments:
/* Multi-line comment
Can span multiple lines
Useful for documentation */
// Single-line comment (C99 and later)
// Each line needs its own //Section 2: Preprocessor Directives
Lines starting with # are processed BEFORE compilation. They're instructions to the preprocessor:
// Include standard library headers
#include <stdio.h> // printf, scanf, fopen, etc.
#include <stdlib.h> // malloc, free, atoi, exit
#include <string.h> // strcpy, strlen, strcmp
#include <math.h> // sqrt, pow, sin, cos
// Include your own header files
#include "myutils.h" // Custom header (searched in current directory)
// Define constants (replaced by preprocessor, no memory used)
#define PI 3.14159265
#define MAX_STUDENTS 100
#define COURSE_NAME "C Programming"
// Define macros (inline code replacement)
#define SQUARE(x) ((x) * (x))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))Section 3: Global Declarations
Variables declared outside all functions are global — accessible from any function in the file:
School: ABC Institute Students: 1 Students: 2 Final count: 2
Best Practice: Minimize global variables. They make code harder to understand and debug. Prefer passing data through function parameters.
Section 4: Function Prototypes
A prototype tells the compiler "this function exists, here's what it looks like" — without giving the full implementation yet:
Sum: 8 Average: 86.6 ==============================
Section 5: The main() Function
main() is the entry point — where execution begins. Every C program must have exactly one main() function.
// If run as: ./program hello world Program name: ./program Arguments: 2 arg[1] = hello arg[2] = world
The Return Value
#include <stdio.h>
int main() {
// return 0 = program succeeded
// return non-zero = program failed (error code)
FILE *file = fopen("data.txt", "r");
if (file == NULL) {
printf("Error: Could not open file!\n");
return 1; // Error code 1 = file not found
}
// Process file...
fclose(file);
return 0; // Success
}The shell can check this return value:
$ ./program
$ echo $? ← prints the return value (0 = success)Section 6: User-Defined Functions
Functions you create to organize your code into reusable blocks:
Primes between 1 and 50: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 After increment: 11 21 31 41 51
The Simplest Valid C Program
int main() {
return 0;
}This is a complete, valid C program. It does nothing, but it compiles and runs successfully. Every other part (headers, functions, variables) is added as your program grows in complexity.
Compilation and Execution Flow
// File: hello.c
#include <stdio.h>
int main() {
printf("Hello, C World!\n");
return 0;
}Compilation steps: $ gcc hello.c -o hello ← Compile source to executable $ ./hello ← Run the executable Hello, C World! What happens internally: 1. Preprocessor: #include <stdio.h> → copies stdio content into file 2. Compiler: Converts C to assembly language 3. Assembler: Converts assembly to machine code (object file) 4. Linker: Connects your code with printf's implementation from C library 5. Output: Executable binary file 'hello'
Common Mistakes in Program Structure
// MISTAKE 1: Missing #include
// printf("Hello"); ← Compiler warning: implicit declaration
// MISTAKE 2: Missing semicolons
// int x = 5 ← Error: expected ';'
// MISTAKE 3: Wrong main signature
// void main() { } ← Non-standard (use int main)
// MISTAKE 4: Using function before declaring it
// int main() {
// greet(); ← Error if greet() isn't declared above main
// }
// void greet() { printf("Hi\n"); }
// FIX: Add prototype above main
// void greet(); ← Prototype
// int main() { greet(); return 0; }
// void greet() { printf("Hi\n"); }
// MISTAKE 5: Forgetting return 0
// int main() { printf("Hi\n"); } ← Implicit return 0 in C99, but bad practiceInterview Questions
Q1: What are the main sections of a C program?
Answer: A C program has six sections: (1) Documentation section (comments describing the program), (2) Preprocessor directives (#include, #define), (3) Global declarations (global variables and constants), (4) Function prototypes (declarations), (5) main() function (entry point, mandatory), (6) User-defined function definitions.
Q2: What is the role of main() in a C program?
Answer: main() is the entry point of every C program — execution always begins here. The operating system calls main() when the program starts. It must return an integer: 0 for success, non-zero for error. Every C program must have exactly one main() function.
Q3: What is the difference between #include <file.h> and #include "file.h"?
Answer: #include <file.h> searches for the header in system/standard library directories (like /usr/include). #include "file.h" first searches the current directory (where your .c file is), and if not found, then searches system directories. Use <> for standard library headers and "" for your own custom headers.
Q4: What happens if you don't include <stdio.h> but use printf()?
Answer: In C89, the compiler assumes printf() returns int and generates a warning about implicit declaration. In C99/C11, it produces an error because implicit function declarations are not allowed. The code may still compile with warnings but can cause undefined behavior if the assumed function signature doesn't match the actual one.
Q5: Can a C program have multiple functions? How does the compiler know where to start?
Answer: Yes, a C program can have any number of functions. The compiler always starts execution at main() — this is mandated by the C standard. Other functions execute only when explicitly called from main() or from a function that was called from main(). The linker sets the program's entry point to the address of main().
Summary
A C program's structure is straightforward but must follow specific rules: preprocessor directives at the top, followed by global declarations, function prototypes, the mandatory main() function, and finally user-defined function implementations. Understanding this structure ensures your programs compile correctly and remain organized as they grow in complexity. The key takeaway is that main() is the heart of every C program — everything else exists to support and organize the code that runs from within main().
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Structure of a C Program.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, basics, structure, program, structure of a c program
Related C Programming Topics