Thread: preprocessing

  1. #1
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    preprocessing

    I m getting output as 3 4.

    How its evaluated? ..

    My doubt is preprocessing happens b4 compiling.. then why first printf not printing 4..

    Is it something like scope??



    Code:
    const int perplexed = 2;
    #define perplexed 3
    
    
    void func();
    
    int main()
    {
      
              printf("%d",perplexed); // getting 3 here
    #ifdef perplexed
    #undef perplexed
    #define perplexed 4
    #endif
    printf("%d",perplexed);      // getting 4 here

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,404
    Why are you defining a macro name to be the same as a variable name?

    Quote Originally Posted by ganesh bala
    My doubt is preprocessing happens b4 compiling.. then why first printf not printing 4
    Because at that point perplexed is defined to be 3.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > My doubt is preprocessing happens b4 compiling.. then why first printf not printing 4..
    1. Use proper English. Not some half-ass lazy version. Don't tell me you don't know how to spell "before".

    2. If the preprocessor had no concept of order it'd be totally useless. Where is your book? Why would it take the last defined value and re-substitute all the previous occurances of the token with the new value?! Makes no sense.
    Last edited by zacs7; 02-18-2009 at 01:58 AM. Reason: Oops

  4. #4
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Wink

    Thanks laserlight.. i just used var-name as macro name to check which one gets printed..

    Now i m clear.. Thanks zacs7 for pointing out.. i try to use proper English :-)..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C preprocessing
    By deepakwar in forum C Programming
    Replies: 7
    Last Post: 11-13-2007, 02:57 AM
  2. Shell command as a preprocessing token
    By hzmonte in forum C Programming
    Replies: 6
    Last Post: 11-01-2005, 07:45 PM
  3. Windows GUI Preprocessing
    By Jaken Veina in forum Windows Programming
    Replies: 5
    Last Post: 07-22-2005, 01:22 PM
  4. Errors in preprocessing.
    By Tronic in forum C++ Programming
    Replies: 17
    Last Post: 03-21-2004, 04:32 PM
  5. Preprocessing C
    By trem in forum C Programming
    Replies: 3
    Last Post: 04-08-2002, 01:42 PM