Preprocessing: source ki preparation
Directives, configuration branches aur source selection versus runtime path ka difference samjhein.
Is chapter mein7 sections
Ordinary C translation se pehle preprocessing directives aur macro replacement handle karti hai. Isse source select, declarations include aur tokens replace ho sakte hain. In steps ko follow karke preprocessor aur runtime conditions ka difference samjhenge.
1. Preparation aur execution alag samjhein
Preprocessing translation model ka part hai. Earlier phases source characters, backslash-newline splicing aur comments handle karti hain; phir directives aur macro replacement preprocessing tokens par operate karte hain. Implementation internally phases combine kar sakti hai, bas required behavior preserve ho. Separate preprocessor executable toolchain ka choice hai, language requirement nahi.
- Preprocessing
- C analysis tak kaunse tokens aayenge?
- C translation
- Syntax aur types valid hain?
- Execution
- Runtime par kaunsa path chalega?
Directive preprocessing line ke start par # preprocessing token se begin hoti hai; preceding whitespace allowed hai. Yeh ordinary statement nahi aur C statement wala semicolon nahi maangti. Immediately newline se followed backslash physical lines ko directive processing se pehle join karta hai. Accidental trailing backslash next line ka meaning change kar sakta hai.
2. Runtime se pehle configuration choose karein
#include <stdio.h>
#ifndef CODEDILE_TRACE
#define CODEDILE_TRACE 0
#endif
int main(void)
{
#if CODEDILE_TRACE
puts("trace enabled");
#else
puts("trace disabled");
#endif
return 0;
}
Default definition se program trace disabled print karta hai. GCC compilation mein -DCODEDILE_TRACE=1 add karne se us invocation ke liye macro define hota hai aur doosri branch select hoti hai. Command-line flag GCC interface hai; #if C directive hai. Program chalne par koi branch user se setting choose nahi karwati.
#ifdef NAME ya #if defined(NAME) macro ki existence check karta hai. #if NAME preprocessing integer constant expression evaluate karta hai; C17 mein macro replacement aur defined-processing ke baad remaining ordinary identifiers zero se replace hote hain. Isliye NAME ko 0 define karne par bhi #ifdef NAME true hai. Local variable, enum constant ya sizeof(int) se #if ordinary C type information access nahi kar sakta.
3. Excluded text aur unexecuted statement mein farq hai
#include <assert.h>
#if 0
int broken = ;
#endif
#define CODEDILE_LIMIT 7
int main(void)
{
int limit = CODEDILE_LIMIT;
assert(limit == 7);
return 0;
}
Invalid C declaration normal C analysis se pehle exclude hoti hai. #if 0 ko if (0) se replace karein toh branch mein valid C syntax aur appropriate type constraints phir bhi required hain. Excluded preprocessing groups arbitrary raw text nahi: comments, strings, tokenization aur balanced conditional directives matter karti hain. Close na hone wala comment apparently disabled region mein bhi preprocessing break kar sakta hai.
4. Har directive ka specific kaam samjhein
| Directive | Kaam |
|---|---|
#include | Header/source contents ko current preprocessing translation unit mein participate karata hai. |
#define, #undef | Macro definition start ya end karta hai; variable assignment nahi. |
#error | Unacceptable build configuration par diagnostic require aur successful translation prevent karta hai. |
#line | Presumed source line/file information adjust karta hai; generated code mein useful. |
#pragma | Specified implementation behavior control karta hai; standard STDC forms ke apne rules hain. |
Agla fragment required macro absent hone par intentionally diagnostic require aur successful translation prevent karta hai. Yeh build-rejection example hai, runnable program nahi. Jab guessed configuration silently use karna misleading ho, tab #error useful hai. Exact diagnostic text aur tool ka later behavior implementation par depend karte hain.
#ifndef CODEDILE_CAPACITY
#error "Define CODEDILE_CAPACITY before building this file"
#endif
__FILE__ aur __LINE__ presumed source locations batate hain; __DATE__ aur __TIME__ translation ke hain, current runtime clock ke nahi. __STDC_VERSION__ claimed language revision batata hai, jaise C17 ke liye 201710L aur C23 ke liye 202311L. Optional library feature available hone ka proof nahi. Reserved underscore patterns se application names na banayein.
Primary references aur aage ki reading
- WG14 N1570 — public C11 committee draft: 5.1.1.2, 6.10.1–6.10.9. C17 baseline ke stable core rules ke liye; final ISO publication nahi.
- WG14 N3096 — public C23 working draft: 6.10. C23 notes C17 example checks se separate hain.
- GCC: preprocessing options
Aage badhne se pehle
- Preprocessing ordinary semantic analysis se pehle directives aur replacement handle karti hai.
- Preprocessor condition source select karti hai; runtime object test nahi karti.
- Configuration ke baad included source mein valid declarations aur compatible interfaces chahiye.
In ideas ko alag rakhein
- Defined macro / Nonzero macro value
- Macro zero value ke saath defined ho sakta hai; existence aur numeric value ke tests alag hain.
#if se source select karna aur ordinary if se runtime path choose karna kaise alag hain?
Optional: bolkar samjhein ya note rakhein. Tayyar hon toh aage badh sakte hain.Apni understanding check karein
Optional hai. Answers save ya send nahi hote; chapter kabhi lock nahi hota.
Har choice ki explanation padhein
- Dono same tarah runtime branches choose karte hain.
#if preprocessing mein handle hota hai, ordinary runtime statement ki tarah nahi.
- #if preprocessing mein source select karta hai; if ordinary execution mein path select karta hai.
✓ Sahi reasoning. Conditional inclusion batata hai kaunse source tokens later translation mein jaayenge.
- #if har runtime variable ki current value dekh sakta hai.
Preprocessing condition rules future runtime object values access nahi dete.
C17
Earlier edition ka quick reference
Existing section links aur saved passages ke liye retained. Expanded explanation upar hai; documented corrections source review mein recorded hain.
Preprocessing ordinary C semantic analysis se pehle directives aur preprocessing tokens par macro replacement handle karti hai. Directive preprocessing line ke beginning mein # token se shuru hoti hai; usse pehle whitespace allowed hai. Directives macro definitions, header inclusion, conditional compilation aur related behavior control karti hain.
Main Preprocessor Directives
- #define: Text substitution ke liye ek macro define karta hai.
#define PI 3.14159
#define SQUARE(x) ((x) * (x))
- #undef: Pehle se defined ek macro ko undefine karta hai.
#undef PI
- #include: Ek header file ka content include karta hai.
#include <stdio.h> // System header
#include "myheader.h" // User-defined header
- #ifdef, #ifndef, #if, #elif, #else, #endif: Macro definitions ke basis par conditional compilation.
#ifdef DEBUG
printf("Debug mode\n");
#endif
- #error: Ek custom message ke saath compilation error generate karta hai.
#ifndef PI
#error "PI is not defined"
#endif
-
#pragma: Implementation-defined behavior control karta hai, standard-specified forms jaise
#pragma STDC FP_CONTRACTko chhodkar.#pragma oncewidely supported extension hai, standard C include guard nahi. -
#line: Diagnostics ke liye reported line number aur filename change karta hai.
Predefined Macros: Preprocessor kuch standard macros provide karta hai jaise __FILE__, __LINE__, __DATE__, __TIME__, aur __STDC__, jo respectively current filename, line number, compilation date, time, aur standard conformance mein expand hote hain.
Example:
#include <stdio.h>
#define LIMIT 5
int main(void) {
for (int i = 0; i < LIMIT; i++) {
printf("%d\n", i);
}
return 0;
}
Agla idea padhne ke liye tayyar?
Mark this chapter as read whenever you feel ready.