🟢 Beginner · Lesson 05
C Program की संरचना
C Program के Parts
हर well-written C program एक standard structure follow करता है। इस structure को समझने से organized, readable और professional code लिख सकते हैं।
Standard C Program Structure
/* 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
C Language
// Single-line comment /* Multi-line comment Multiple lines ho sakta hai */
💡 Comments ज़रूरी क्यों?
Comments code को readable बनाते हैं। 6 महीने बाद आप खुद का code भूल जाते हैं! अच्छे comments बताते हैं कि function क्या करता है, parameters क्या हैं, और edge cases कौन से हैं।
Complete Example Program
C Language – Student Grade Calculator
/* Program: Student Grade Calculator | CodingEasily */ #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"); }
Naam daalen: Priya
Marks (0-100): 87
=== Result ===
Naam: Priya
Marks: 87/100
Grade: A
Result: PASS
सारांश
- 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 करें!
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C program के parts क्या हैं?
एक सामान्य C program में preprocessor section (
#include और #define lines), एक वैकल्पिक global declaration section, main function जहाँ execution शुरू होता है, और कोई भी user-defined functions होते हैं। Comments code समझाने को कहीं भी आ सकते हैं।C program में main function क्या है?
main वह function है जहाँ हर C program चलना शुरू करता है। Operating system इसे पहले call करता है, और इसके अंदर का code ऊपर से नीचे चलता है। यह आमतौर पर सफल समाप्ति दर्शाने को 0 लौटाता है।C में preprocessor section क्या है?
Preprocessor section
# से शुरू होने वाली lines का समूह है, जैसे #include <stdio.h> और #define। ये compilation से पहले संभाले जाते हैं: includes library headers लाते हैं, और defines macros तथा constants बनाते हैं।C program में comments क्यों इस्तेमाल होते हैं?
Comments बताते हैं कि code क्या करता है या क्यों, लोगों के लिए पढ़ना और रखरखाव आसान बनाते हुए। Compiler इन्हें पूरी तरह अनदेखा करता है, तो ये program के चलने को कभी प्रभावित नहीं करते। C single-line
// और multi-line /* ... */ comments समर्थन करता है।क्या C program में sections का क्रम मायने रखता है?
आमतौर पर आप includes और defines ऊपर रखते हैं, फिर globals, फिर functions, जिनमें
main शामिल होता है। किसी function या variable को इस्तेमाल से पहले declare करना ज़रूरी है, इसीलिए headers और prototypes आमतौर पर पहले आते हैं।💻 लाइव कोड एडिटर
इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.