🟢 Beginner  ·  Lesson 05

Structure of a C Program

Parts of a C Program

Every well-written C program follows a standard structure. Understanding this structure helps you write organized, readable, and professional code.

Standard C Program Structure
/* =====================================================
   1. DOCUMENTATION SECTION (Program description)
   ===================================================== */

// 2. PREPROCESSOR / HEADER FILE SECTION
#include <stdio.h>
#include <math.h>
#define PI 3.14159

// 3. GLOBAL DECLARATION SECTION
int counter = 0;       // Global variable

// 4. FUNCTION PROTOTYPE SECTION
void printMenu();
int  add(int, int);

// 5. MAIN FUNCTION
int main() {
    // Local variables
    int a = 5, b = 3;
    printMenu();
    printf("Sum = %d\n", add(a, b));
    return 0;
}

// 6. USER-DEFINED FUNCTIONS
void printMenu() {
    printf("=== Calculator ===\n");
}
int add(int x, int y) {
    return x + y;
}

Preprocessor Section

Lines starting with # are processed by the preprocessor — before the compiler sees the code. This section includes:

  • Header files#include <stdio.h>, #include <math.h>, #include <string.h>
  • Macro definitions#define PI 3.14159
  • Conditional compilation#ifdef, #endif
Header FileFunctions it provides
<stdio.h>printf, scanf, getchar, putchar, fgets, puts
<math.h>sqrt, pow, abs, sin, cos, log, ceil, floor
<string.h>strlen, strcpy, strcat, strcmp
<stdlib.h>malloc, free, rand, srand, atoi, exit
<ctype.h>isdigit, isalpha, isupper, islower, toupper, tolower
<time.h>time, clock, difftime

Global Declaration Section

Variables declared outside all functions are global variables. They are accessible from any function in the file and exist for the lifetime of the program.

C Language – Global vs Local
#include <stdio.h>

int globalCount = 0;  // Global: accessible everywhere

void increment() {
    globalCount++;     // Can access global variable
    int local = 10;   // Local: only inside increment()
}

int main() {
    increment();
    increment();
    printf("Count: %d\n", globalCount);  // 2
    // printf("%d", local); // ERROR: local not visible here
    return 0;
}
⚠️ Use Global Variables Sparingly

Overuse of global variables makes code hard to debug and maintain (any function can change them). Prefer local variables and pass values through function parameters.

main() Function

The main() function is the entry point of every C program. The operating system calls main() when you run the program. Key points:

  • Every C program must have exactly one main() function
  • Return type is always int (in modern C)
  • return 0 indicates successful execution; non-zero = error
  • Can accept command-line arguments: int main(int argc, char *argv[])

User-Defined Functions

Functions can be defined before or after main(). If after, use a prototype before main(). Good practice: one function per logical task.

Comments

C Language – Comments
// Single-line comment (C99 and later)

/* Multi-line comment
   Can span multiple lines
   Used for documentation */

/**
 * Function: calculateArea
 * Parameters: radius (float)
 * Returns: area of circle (float)
 */
float calculateArea(float r) { return 3.14f * r * r; }

Complete Well-Structured Program Example

C Language – Student Grade Calculator
/*
 * Program: Student Grade Calculator
 * Author:  CodingEasily
 * Date:    2025
 * Purpose: Calculate grade from marks input
 */

#include <stdio.h>    // printf, scanf
#include <string.h>  // strcmp

#define MAX_MARKS  100
#define MIN_PASS   40

/* Function prototypes */
char* getGrade(int marks);
void  printResult(char *name, int marks);

/* Main function */
int main() {
    char name[50];
    int  marks;
    printf("Enter student name: ");
    scanf("%s", name);
    printf("Enter marks (0-%d): ", MAX_MARKS);
    scanf("%d", &marks);
    printResult(name, marks);
    return 0;
}

char* getGrade(int marks) {
    if (marks >= 90) return "A+";
    if (marks >= 80) return "A";
    if (marks >= 70) return "B";
    if (marks >= 60) return "C";
    if (marks >= MIN_PASS) return "D";
    return "F";
}

void printResult(char *name, int marks) {
    printf("\n=== Result Card ===\n");
    printf("Student: %s\n", name);
    printf("Marks:   %d/%d\n", marks, MAX_MARKS);
    printf("Grade:   %s\n", getGrade(marks));
    printf("Result:  %s\n", marks >= MIN_PASS ? "PASS" : "FAIL");
    printf("===================\n");
}
Enter student name: Rahul Enter marks (0-100): 87 === Result Card === Student: Rahul Marks: 87/100 Grade: A Result: PASS ===================

Summary

  • A C program has 6 parts: Documentation → Preprocessor → Global declarations → Prototypes → main() → User-defined functions
  • #include brings in library function definitions
  • Global variables are accessible everywhere; prefer local variables
  • Every program has exactly one main() — execution starts there
  • return 0 = success; non-zero = error
  • Comments (// and /* */) improve readability — use them!

Frequently Asked Questions

What are the parts of a C program?
A typical C program has a preprocessor section (the #include and #define lines), an optional global declaration section, the main function where execution begins, and any user-defined functions. Comments can appear anywhere to explain the code.
What is the main function in a C program?
main is the function where every C program starts running. The operating system calls it first, and the code inside it runs top to bottom. It usually returns 0 to signal that the program finished successfully.
What is the preprocessor section in C?
The preprocessor section is the set of lines beginning with #, such as #include <stdio.h> and #define. These are handled before compilation: includes pull in library headers, and defines set up macros and constants.
Why are comments used in a C program?
Comments explain what the code does or why, making it easier for people to read and maintain. The compiler ignores them entirely, so they never affect how the program runs. C supports single-line // and multi-line /* ... */ comments.
Does the order of sections matter in a C program?
Generally you place includes and defines at the top, then globals, then functions, with main among them. A function or variable must be declared before it is used, which is why headers and prototypes usually come first.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.