Paramclasses Academy - Blog

Tutorials Details

Structure of a C Program in C Language – Complete Tutorial for Beginners

Structure of a C Program in C Language – Complete Tutorial for Beginners

October 12, 2026

1. Introduction

C programming seekhne ke liye sabse pehle humein yeh samajhna zaroori hai ki ek C program ka structure kaisa hota hai. Jaise kisi building ko banane ke liye ek proper structure aur design ki zaroorat hoti hai, waise hi C program ko likhne ke liye bhi kuch predefined rules aur components hote hain.

Is module mein hum seekhenge ki C program ke different parts kya hote hain, #include<stdio.h> ka kya use hai, main() function kyun important hai, aur printf() aur return 0 ka kya kaam hota hai.

2. What Is the Structure of a C Program?

C program ka structure un statements, functions aur instructions ka arrangement hota hai jinke through hum compiler ko batate hain ki program kaise execute karna hai.

Ek basic C program mein generally ye components hote hain:

  • Documentation section

  • Preprocessor directive

  • Definition section (optional)

  • Global declaration section (optional)

  • main() function

  • Local declaration section

  • Executable statements

  • User-defined functions (optional)

Har program mein sabhi sections hona compulsory nahi hai. Basic program likhne ke liye kuch essential components hi kaafi hote hain.

3. Basic Syntax of a C Program

#include <stdio.h>

int main(void)
{
    printf("Hello World!");

    return 0;
}

Output:

Hello World!

Yeh ek simple C program hai jo screen par Hello World! print karta hai. Ab iske har part ko detail mein samajhte hain.

4. Components of a C Program

4.1 Documentation Section

Documentation section mein program ke baare mein comments likhe jaate hain. Comments programmer ko code samajhne mein help karte hain.

Example:

// This program prints Hello World

C mein comments ke do main types hote hain:

Single-line comment

// This is a single-line comment

Multi-line comment

/*
This is a multi-line comment.
It can span multiple lines.
*/

Compiler comments ko executable instructions ki tarah treat nahi karta. Inka purpose code ko readable aur maintainable banana hai.

4.2 Preprocessor Directive

Example:

#include <stdio.h>

#include ek preprocessor directive hai. Iska use program mein header file include karne ke liye hota hai.

stdio.h standard input/output header hai, jo printf() aur scanf() jaise standard input/output functions ki declarations provide karta hai.

Important: #include ke baad semicolon (;) nahi lagaya jata.

4.3 Definition Section

Is section mein commonly macros aur symbolic constants define kiye jaate hain.

Example:

#define PI 3.14159

Yahan PI ek macro hai. Preprocessor source code mein PI ko uski replacement value 3.14159 se replace karta hai.

Example usage:

#include <stdio.h>
#define PI 3.14159

int main(void)
{
    printf("%f", PI);
    return 0;
}

Output:

3.141590

4.4 Global Declaration Section

Is section mein global variables aur functions ki declarations likhi ja sakti hain, jinhe multiple functions access kar sakte hain, subject to their scope and linkage.

Example:

int total = 100;

Agar total ko kisi function ke bahar declare kiya gaya hai, to woh global-scope variable hai.

Global variables ko carefully use karna chahiye, kyunki bahut saare functions se unki value change hone par code ko maintain karna difficult ho sakta hai.

4.5 main() Function

int main(void)
{
    // Program statements
    return 0;
}

main() C program ka designated entry point hai. Hosted C programs mein execution main() se start hota hai.

  • int batata hai ki function integer value return karta hai.

  • main function ka naam hai.

  • void ka matlab hai ki function koi parameters accept nahi karta.

  • { } function body ko define karte hain.

  • return 0; successful program termination indicate karta hai.

Note: int main(void) beginners ke liye ek clear aur standard form hai.

4.6 Local Declaration Section

Function ke andar declare kiye gaye variables ko local variables kaha jaata hai.

Example:

int main(void)
{
    int age = 20;

    return 0;
}

Yahan age ek local variable hai. Iska scope us block tak hota hai jisme ise declare kiya gaya hai.

4.7 Executable Statements

Executable statements woh instructions hoti hain jo program run hone par perform hoti hain.

Example:

printf("Welcome to C Programming");

Yeh statement screen par message display karti hai.

4.8 User-Defined Functions

Programmer apne task ke according custom functions bhi bana sakta hai.

Example:

void greet(void)
{
    printf("Welcome!");
}

Is function ko call karne par Welcome! print hoga. Function ki definition likhne se woh automatically execute nahi hota; use call karna hota hai.

5. Complete C Program with Explanation

Ab ek complete example dekhte hain jisme hum student ki basic information print karenge.

#include <stdio.h>

int main(void)
{
    // Declare variables
    int rollNumber = 101;
    int age = 20;

    // Display student information
    printf("Student Information\n");
    printf("Roll Number: %d\n", rollNumber);
    printf("Age: %d\n", age);

    return 0;
}

Output:

Student Information
Roll Number: 101
Age: 20

Line-by-line Explanation

Code

Explanation

#include <stdio.h>

Standard input/output declarations include karta hai.

int main(void)

Program ka entry-point function define karta hai.

int rollNumber = 101;

Integer variable declare aur initialize karta hai.

int age = 20;

Student ki age store karta hai.

printf(...)

Text aur variable values display karta hai.

return 0;

Successful termination status return karta hai.

%d aur \n kya hain?

  • %d integer value display karne ke liye format specifier hai.

  • \n newline character hai, jo output ko next line par le jaata hai.

  • ; statement ke end ko mark karta hai.

  • { } code blocks ki boundaries define karte hain.

6. Real-Time Example: Simple Bill Display

Maan lijiye ek shopkeeper ko customer ka bill display karna hai.

#include <stdio.h>

int main(void)
{
    int quantity = 3;
    int price = 50;
    int total = quantity * price;

    printf("Simple Bill\n");
    printf("Quantity: %d\n", quantity);
    printf("Price per item: %d\n", price);
    printf("Total Amount: %d\n", total);

    return 0;
}

Output:

Simple Bill
Quantity: 3
Price per item: 50
Total Amount: 150

Is example mein:

  • quantity items ki sankhya store karta hai.

  • price ek item ki price store karta hai.

  • total quantity aur price ko multiply karke total amount calculate karta hai.

Yeh example dikhata hai ki variables, expressions aur output statements milkar ek useful program kaise banate hain.

7. Rules to Remember While Writing a C Program

  1. C language case-sensitive hai. main, Main aur MAIN alag identifiers hain.

  2. Most executable statements ke end mein semicolon (;) lagta hai.

  3. Function body aur blocks ke liye curly braces { } use hote hain.

  4. String literals double quotes mein likhe jaate hain, jaise "Hello".

  5. #include <stdio.h> ke baad semicolon nahi lagta.

  6. Variable use karne se pehle use declare karna chahiye.

  7. Braces aur parentheses ko correctly close karna zaroori hai.

  8. Standard hosted C program mein int main(void) ek suitable starting point hai.

8. Common Errors in C Programming

Error 1: Semicolon missing

Incorrect:

int age = 20

Correct:

int age = 20;

Error 2: printf() ke around quotes missing

Incorrect:

printf(Hello);

Correct:

printf("Hello");

Error 3: stdio.h include nahi kiya

Agar printf() use kar rahe hain, to standard declaration available karne ke liye stdio.h include karein.

#include <stdio.h>

Error 4: Curly brace missing

Incorrect:

int main(void)
{
    printf("Hello");
    return 0;

Correct:

int main(void)
{
    printf("Hello");
    return 0;
}

Error 5: Incorrect capitalization

Incorrect:

Printf("Hello");

Correct:

printf("Hello");

C language mein printf() lowercase letters mein likha jaata hai.

9. How to Compile and Run a C Program

Agar aapke computer mein GCC compiler installed hai, to program ko module2.c naam se save karein.

Terminal ya command prompt mein:

gcc module2.c -o module2

Program run karne ke liye:

Windows:

module2.exe

Linux/macOS:

./module2

Agar compiler errors show karta hai, to error message padhkar line number aur syntax check karein.

10. Practical Programs for Practice

Program 1: Print Your Name

Task: Apna naam screen par display karein.

Expected output:

My name is Rahul

Program 2: Display Three Numbers

Task: Teen integer variables declare karein aur unki values print karein.

Expected output:

10
20
30

Program 3: Calculate Total Marks

Task: Teen subjects ke marks store karein aur total calculate karke display karein.

Example output:

Subject 1: 70
Subject 2: 80
Subject 3: 90
Total Marks: 240

Program 4: Print a Product Bill

Task: Product name, quantity aur price display karein. Quantity aur price ko multiply karke total amount calculate karein.

11. Interview Questions

Q1. What is the structure of a C program?

C program ka structure functions, declarations, preprocessor directives aur executable statements ka organized arrangement hota hai.

Q2. What is the purpose of #include <stdio.h>?

Yeh standard input/output functions ki declarations provide karne wali header file include karta hai.

Q3. What is the purpose of main()?

Hosted C program ka execution main() function se start hota hai.

Q4. Why is return 0; used?

Yeh operating environment ko successful program termination status return karta hai.

Q5. What is the difference between comments and executable statements?

Comments code ko explain karte hain aur executable instructions ki tarah run nahi hote. Executable statements program ke operations perform karte hain.

Q6. Is semicolon required after #include <stdio.h>?

Nahi. #include ek preprocessor directive hai aur iske end mein semicolon nahi lagta.

12. Quick Revision

  • #include <stdio.h> — standard input/output declarations include karta hai.

  • int main(void) — program ka entry-point function.

  • printf() — output display karne ke liye.

  • int — integer type declare karne ke liye.

  • return 0; — successful termination status.

  • ; — most statements ke end mein.

  • { } — code blocks ke liye.

  • // aur /* ... */ — comments ke liye.

13. Homework

  1. Ek program likhein jo aapka naam, age aur city print kare.

  2. Ek program likhein jo do numbers ka sum display kare.

  3. Ek program likhein jo student ka naam aur total marks print kare.

  4. Ek program likhein jo quantity aur price se bill calculate kare.

  5. Basic C program ke har component ka purpose apne words mein explain karein.

Conclusion

Is module mein aapne C program ka basic structure, header files, main() function, variables, executable statements aur return statement samjhe. In components ko achhe se samajhna aage ke modules—variables, data types, operators aur input/output—seekhne ke liye important hai.