Structure of a C Program
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.
/* ===================================================== 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 File | Functions 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.
#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; }
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 0indicates 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
// 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
/* * Program: Student Grade Calculator * Author: CodeKaFunda * 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"); }
Summary
- A C program has 6 parts: Documentation → Preprocessor → Global declarations → Prototypes → main() → User-defined functions
#includebrings 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!
C Program के Parts
हर well-written C program एक standard structure follow करता है। इस structure को समझने से organized, readable और professional code लिख सकते हैं।
/* 1. DOCUMENTATION — Program description */ // 2. PREPROCESSOR / HEADER FILES #include <stdio.h> #define PI 3.14159 // 3. GLOBAL DECLARATIONS (सभी functions के लिए accessible) int counter = 0; // 4. FUNCTION PROTOTYPES void printMenu(); // 5. MAIN FUNCTION (program यहाँ से शुरू होता है) int main() { printMenu(); return 0; } // 6. USER-DEFINED FUNCTIONS void printMenu() { printf("Menu\n"); }
Preprocessor Section
# से शुरू होने वाली lines preprocessor process करता है — compiler से पहले।
| Header File | Functions |
|---|---|
<stdio.h> | printf, scanf, fgets, puts |
<math.h> | sqrt, pow, sin, cos, floor |
<string.h> | strlen, strcpy, strcat, strcmp |
<stdlib.h> | malloc, free, rand, exit, atoi |
<ctype.h> | isdigit, isalpha, toupper, tolower |
main() Function
main() function हर C program का entry point है। OS इसे तभी call करता है जब आप program run करते हैं।
- हर C program में exactly एक
main()होनी चाहिए - Return type हमेशा
intहोता है return 0= successful; non-zero = error
Comments
// Single-line comment /* Multi-line comment Multiple lines ho sakta hai */
Comments code को readable बनाते हैं। 6 महीने बाद आप खुद का code भूल जाते हैं! अच्छे comments बताते हैं कि function क्या करता है, parameters क्या हैं, और edge cases कौन से हैं।
Complete Example Program
/* Program: Student Grade Calculator | CodeKaFunda */ #include <stdio.h> #define MAX 100 #define PASS 40 char* grade(int m); void result(char *n, int m); int main() { char name[50]; int marks; printf("Naam daalen: "); scanf("%s", name); printf("Marks (0-%d): ", MAX); scanf("%d", &marks); result(name, marks); return 0; } char* grade(int m) { if(m>=90) return "A+"; if(m>=80) return "A"; if(m>=70) return "B"; if(m>=PASS) return "C"; return "F"; } void result(char *n, int m) { printf("\n=== Result ===\nNaam: %s\nMarks: %d/%d\nGrade: %s\nResult: %s\n", n, m, MAX, grade(m), m>=PASS ? "PASS":"FAIL"); }
सारांश
- C program के 6 parts: Documentation → Preprocessor → Global → Prototypes → main() → User-defined functions
#includelibrary function definitions लाता है- Global variables सब जगह accessible; prefer local variables
- हर program में exactly एक
main()— execution यहाँ से शुरू return 0= success; non-zero = error- Comments (
//और/* */) readability improve करते हैं — ज़रूर use करें!