YOUR C LEARNING COMPANION

Kahan se padhein?

Learning path
Program bananaChapter 39 of 73

Preprocessing: source ki preparation

Directives, configuration branches aur source selection versus runtime path ka difference samjhein.

5 min readIntermediateC17 baseline
Offline reading
Is chapter mein7 sections
0% through this chapter

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.

Teen alag decisions
Preprocessing
C analysis tak kaunse tokens aayenge?
C translation
Syntax aur types valid hain?
Execution
Runtime par kaunsa path chalega?
Conceptual model hai; three separate programs ki requirement nahi.

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

Complete C17 program
#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

Complete C17 program
#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

DirectiveKaam
#includeHeader/source contents ko current preprocessing translation unit mein participate karata hai.
#define, #undefMacro definition start ya end karta hai; variable assignment nahi.
#errorUnacceptable build configuration par diagnostic require aur successful translation prevent karta hai.
#linePresumed source line/file information adjust karta hai; generated code mein useful.
#pragmaSpecified 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.

C17 · intentional build rejection
#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

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.
Apne words mein samjhaayein

#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.

#if aur ordinary if mein kya farq hai?
Har choice ki explanation padhein
  1. Dono same tarah runtime branches choose karte hain.

    #if preprocessing mein handle hota hai, ordinary runtime statement ki tarah nahi.

  2. #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.

  3. #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.
C17 · illustrative fragment
#define PI 3.14159
#define SQUARE(x) ((x) * (x))
  • #undef: Pehle se defined ek macro ko undefine karta hai.
C17 · illustrative fragment
#undef PI
  • #include: Ek header file ka content include karta hai.
C17 · illustrative fragment
#include <stdio.h>      // System header
#include "myheader.h"   // User-defined header
  • #ifdef, #ifndef, #if, #elif, #else, #endif: Macro definitions ke basis par conditional compilation.
C17 · illustrative fragment
#ifdef DEBUG
printf("Debug mode\n");
#endif
  • #error: Ek custom message ke saath compilation error generate karta hai.
C17 · illustrative fragment
#ifndef PI
#error "PI is not defined"
#endif
  • #pragma: Implementation-defined behavior control karta hai, standard-specified forms jaise #pragma STDC FP_CONTRACT ko chhodkar. #pragma once widely 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:

C17 · illustrative fragment
#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.

CHAPTER 39 / 73

Is chapter mein

Preprocessing: source ki preparation

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping