macro substitution rule

This is a discussion on macro substitution rule within the C Programming forums, part of the General Programming Boards category; i want to know the rule behind the macro substitution ex: Code: #include<stdio.h> #define p P int main() { int ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    macro substitution rule

    i want to know the rule behind the macro substitution

    ex:
    Code:
    #include<stdio.h>
    #define p P
    
    int main()
    {
    int P = 10;
    
    printf("\n P= %d",p);
    }
    o/p: P=10

    my question is why dont macro substitution dont replace p in printf with P.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    The preprocessor understands enough of the C language to recognise that printf is a different identifier from p.

    Roughly speaking, the preprocessor only substitutes p for P where the p is in a context where it looks like a complete token.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    Smile tokenization before macro expansion

    There are 8 phases of Standard C translation
    1) Trigraph Replacement
    2) Line Splicing
    3) Tokenization
    4) Macro Expansion and Directive handling
    5) Character set mapping
    6) String concatenation
    7) Translation
    8) Linkage

    Since tokenization occur prior to macro expansion it does not expand p of printf(). The preprocessor does not parse the source text but it does break it up in tokens for the purpose of locationg macro calls.

    The Preprocessor (C/C++)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Replies: 2
    Last Post: 04-12-2010, 12:57 PM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  4. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21