Structure of a C Program with Examples

Every C program follows a particular structure that helps programmers write, organize, compile, and execute instructions properly. A C program generally consists of preprocessor directives, macro definitions, global declarations, function prototypes, the main() function, and user-defined function definitions.

प्रत्येक C प्रोग्राम एक विशेष संरचना का पालन करता है, जो प्रोग्रामर को निर्देशों को सही तरीके से लिखने, व्यवस्थित करने, कंपाइल करने तथा निष्पादित करने में सहायता करती है। सामान्यतः एक C प्रोग्राम में प्रीप्रोसेसर डायरेक्टिव्स, मैक्रो डेफिनिशन्स, ग्लोबल डिक्लेरेशन, फंक्शन प्रोटोटाइप, main() फंक्शन तथा यूजर-डिफाइंड फंक्शन की डेफिनिशन शामिल होती हैं।

Structure of a C Program

A C program can be divided into the following important sections:

C प्रोग्राम को निम्नलिखित महत्वपूर्ण भागों में विभाजित किया जा सकता है:

1. Documentation Section
2. Preprocessor Directive Section (Include Section)
3. Definition Section
4. Global Declaration Section
5. Function Prototype Section
6. main function
    6.A Local Variable Declaration
    6.B Executable Statements
7. User-Defined Function Definition

1. Documentation Section

The documentation section contains comments that describe the purpose, name, author, or other important information about the program. Comments are written using // for a single line or /* ... */ for multiple lines.

डॉक्यूमेंटेशन सेक्शन में ऐसे कमेंट्स लिखे जाते हैं जो प्रोग्राम का उद्देश्य, नाम, लेखक या अन्य महत्वपूर्ण जानकारी बताते हैं। सिंगल लाइन कमेंट के लिए // तथा मल्टीपल लाइन कमेंट के लिए /* ... */ का उपयोग किया जाता है।

Example:

/* Program to calculate simple interest */
// This program demonstrates basic C programming.

2. Preprocessor Directive Section (Include Section)

The preprocessor directive section contains instructions that are processed before the actual compilation of the program. The #include directive is used to include header files that provide declarations of library functions.

प्रीप्रोसेसर डायरेक्टिव सेक्शन में ऐसे निर्देश होते हैं जिन्हें प्रोग्राम की वास्तविक कंपाइलेशन प्रक्रिया से पहले प्रोसेस किया जाता है। #include डायरेक्टिव का उपयोग हेडर फाइलों को शामिल करने के लिए किया जाता है, जिनमें लाइब्रेरी फंक्शन्स की घोषणाएँ उपलब्ध होती हैं।

Example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Here, stdio.h provides declarations for input/output functions such as printf() and scanf(). Similarly, stdlib.h provides functions such as malloc() and exit().

यहाँ stdio.h में printf() तथा scanf() जैसे इनपुट/आउटपुट फंक्शन्स की घोषणाएँ उपलब्ध होती हैं। इसी प्रकार stdlib.h में malloc() तथा exit() जैसे फंक्शन्स की घोषणाएँ उपलब्ध होती हैं।

3. Definition Section (Macro Definition)

The definition section is used to define symbolic constants and macros with the help of the #define preprocessor directive. These definitions can be used throughout the source file after their declaration.

डेफिनिशन सेक्शन का उपयोग #define प्रीप्रोसेसर डायरेक्टिव की सहायता से सिंबॉलिक कॉन्स्टेंट तथा मैक्रो को परिभाषित करने के लिए किया जाता है। इनका उपयोग स्रोत फाइल में परिभाषित किए जाने के बाद किया जा सकता है।

Example:

#define PI 3.14159
#define MAX 100

In this example, PI and MAX are symbolic constants. The preprocessor replaces their occurrences with the defined values before compilation.

इस उदाहरण में PI तथा MAX सिंबॉलिक कॉन्स्टेंट हैं। कंपाइलेशन से पहले प्रीप्रोसेसर इनके उपयोग को निर्धारित वैल्यू से बदल देता है।

4. Global Declaration Section

The global declaration section contains declarations that are written outside all functions. It may include global variables, constants, and function prototypes. A global variable declared at file scope can generally be accessed by functions in the same source file, subject to its linkage and declaration.

ग्लोबल डिक्लेरेशन सेक्शन में ऐसी घोषणाएँ होती हैं जो सभी फंक्शन्स के बाहर लिखी जाती हैं। इसमें ग्लोबल वेरिएबल, कॉन्स्टेंट तथा फंक्शन प्रोटोटाइप शामिल हो सकते हैं। फाइल स्कोप पर घोषित ग्लोबल वेरिएबल को सामान्यतः उसी सोर्स फाइल के फंक्शन्स द्वारा एक्सेस किया जा सकता है, जो उसकी लिंकेज तथा घोषणा पर निर्भर करता है।

Example:

int count = 0;
float salary = 25000.0f;

The variables count and salary are declared outside the main() function. Therefore, they have file scope.

यहाँ count तथा salary वेरिएबल main() फंक्शन के बाहर घोषित किए गए हैं। इसलिए इनका फाइल स्कोप होता है।

5. Function Prototype Section

A function prototype tells the compiler about a function's name, return type, and parameter types before the function is called. It is generally declared before the main() function when the function definition is written later in the program.

फंक्शन प्रोटोटाइप कंपाइलर को किसी फंक्शन का नाम, रिटर्न टाइप तथा पैरामीटर के टाइप के बारे में जानकारी देता है। जब फंक्शन की डेफिनिशन प्रोग्राम में बाद में लिखी जाती है, तब इसका प्रोटोटाइप सामान्यतः main() फंक्शन से पहले घोषित किया जाता है।

Example:

int add(int, int);
void display(void);

Here, add() returns an integer and accepts two integer arguments, whereas display() does not accept any arguments and does not return a value.

यहाँ add() दो integer आर्गुमेंट स्वीकार करता है तथा integer वैल्यू वापस करता है, जबकि display() कोई आर्गुमेंट स्वीकार नहीं करता और कोई वैल्यू वापस नहीं करता।

6. main() Function Section

The main() function is the entry point of a hosted C program. Program execution normally begins from main(). It contains local variable declarations and executable statements. It may also call other library functions or user-defined functions.

main() फंक्शन एक Hosted C प्रोग्राम का एंट्री पॉइंट होता है। सामान्यतः प्रोग्राम का निष्पादन main() से शुरू होता है। इसमें लोकल वेरिएबल की घोषणाएँ तथा एक्जीक्यूटेबल स्टेटमेंट लिखे जाते हैं। यह अन्य लाइब्रेरी फंक्शन्स या यूजर-डिफाइंड फंक्शन्स को भी कॉल कर सकता है।

The main() function generally contains the following parts:

main() फंक्शन में सामान्यतः निम्नलिखित भाग होते हैं:

6.A. Local Variable Declaration and Initialization: Variables required within the main() function are declared and initialized here.

main() फंक्शन के अंदर आवश्यक वेरिएबल को यहाँ घोषित तथा इनिशियलाइज किया जाता है।

int a = 10, b = 20;
float result;

6.B. Executable Statements: These statements perform the actual operations of the program, such as input, calculation, function calls, and output.

ये स्टेटमेंट प्रोग्राम के वास्तविक कार्य करते हैं, जैसे इनपुट लेना, गणना करना, फंक्शन कॉल करना तथा आउटपुट प्रदर्शित करना।

result = a + b;
printf("Result = %.2f\n", result);

The return statement is used to terminate main() and return an exit status to the host environment. Returning 0 conventionally indicates successful execution.

रिटर्न स्टेटमेंट का उपयोग main() को समाप्त करने तथा होस्ट एनवायरनमेंट को एग्जिट स्टेटस वापस करने के लिए किया जाता है। सामान्यतः 0 वापस करना सफल निष्पादन को दर्शाता है।

return 0;

7. User-Defined Function Definition Section

A user-defined function is a function created by the programmer to perform a specific task. Its definition may be written before or after the main() function. If its definition appears after main(), a suitable function prototype should generally be declared before the function is called.

यूजर-डिफाइंड फंक्शन वह फंक्शन होता है जिसे प्रोग्रामर किसी विशेष कार्य को करने के लिए स्वयं बनाता है। इसकी डेफिनिशन main() फंक्शन से पहले या बाद में लिखी जा सकती है। यदि इसकी डेफिनिशन main() के बाद लिखी जाती है, तो फंक्शन को कॉल करने से पहले सामान्यतः उसका उचित प्रोटोटाइप घोषित करना चाहिए।

Example:

int add(int a, int b)
{
return a + b;
}

The above function accepts two integers and returns their sum.

उपरोक्त फंक्शन दो integer वैल्यू स्वीकार करता है तथा उनका योग वापस करता है।

Example: C Program to Calculate Simple Interest

The following program demonstrates the structure of a C program by calculating simple interest using the given principal amount, rate of interest, and time.

निम्नलिखित प्रोग्राम दी गई मूलधन राशि, ब्याज दर तथा समय की सहायता से साधारण ब्याज की गणना करके C प्रोग्राम की संरचना को प्रदर्शित करता है।

Formula:

Simple Interest = (Principal × Rate × Time) / 100

C Program:

#include <stdio.h>
int main(void)
{
float p, r, t, si;
printf("Enter Principal, Rate and Time:\n");
if (scanf("%f %f %f", &p, &r, &t) != 3)
{
printf("Invalid input!\n");
return 1;
}
if (p < 0 || r < 0 || t < 0)
{
printf("Values cannot be negative.\n");
return 1;
}
si = (p * r * t) / 100.0f;
printf("Simple Interest = %.2f\n", si);
return 0;
}

Output:

Enter Principal, Rate and Time:
10000 5 2
Simple Interest = 1000.00

Explanation: In this program, stdio.h is included to use input/output functions. The main() function declares four local variables: p, r, t, and si. The program accepts the principal amount, rate, and time, checks the input, calculates simple interest, and displays the result.

व्याख्या: इस प्रोग्राम में इनपुट/आउटपुट फंक्शन्स का उपयोग करने के लिए stdio.h हेडर फाइल शामिल की गई है। main() फंक्शन में चार लोकल वेरिएबल p, r, t तथा si घोषित किए गए हैं। प्रोग्राम मूलधन, ब्याज दर तथा समय का इनपुट लेता है, इनपुट की जाँच करता है, साधारण ब्याज की गणना करता है तथा परिणाम प्रदर्शित करता है।

Important Points About C Program Structure

  1. Every hosted C program must have one main() function as its entry point. प्रत्येक Hosted C प्रोग्राम में एंट्री पॉइंट के रूप में एक main() फंक्शन होना आवश्यक है।

  2. Header files are included using the #include directive. हेडर फाइलों को #include डायरेक्टिव की सहायता से शामिल किया जाता है।

  3. Macros and symbolic constants can be defined using #define. मैक्रो तथा सिंबॉलिक कॉन्स्टेंट को #define की सहायता से परिभाषित किया जा सकता है।

  4. Global declarations are written outside all functions. ग्लोबल डिक्लेरेशन सभी फंक्शन्स के बाहर लिखे जाते हैं।

  5. Function prototypes provide information about functions before their use. फंक्शन प्रोटोटाइप किसी फंक्शन के उपयोग से पहले उसके बारे में जानकारी प्रदान करता है।

  6. User-defined function definitions can appear before or after main(), provided declarations are handled correctly. यूजर-डिफाइंड फंक्शन की डेफिनिशन main() से पहले या बाद में लिखी जा सकती है, बशर्ते आवश्यक घोषणाएँ सही तरीके से की गई हों।

  7. Statements in C generally end with a semicolon (;). C लैंग्वेज में सामान्यतः प्रत्येक स्टेटमेंट के अंत में सेमीकोलन (;) लगाया जाता है।

  8. The standard return type of main() in a hosted C program is int. Hosted C प्रोग्राम में main() का स्टैंडर्ड रिटर्न टाइप int होता है।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025