Preprocessor in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chella
    New Member
    • Mar 2007
    • 51

    Preprocessor in C

    Hi,

    Please help me to understand what really happens in the below code.
    [code=c]
    #define num 20
    main()
    {
    printf("%d,",nu m);
    func();
    printf("%d",num );
    }
    void func()
    {
    #undef num
    #define num 50
    }
    [/code]
    The output is 20,20.
    I expect the output to be 20,50
    It seems to be strange to understand. Help me

    Thanks in advance,
    Chella
    Last edited by sicarie; Oct 4 '07, 01:15 PM. Reason: Code tags
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Hi,

    I had read somewhere "Macros are evils" but today I have experienced it after finding your problem's solution. I am getting output 50,50!! (Linux)

    Experts can throw some light on this issue.

    Regards

    Comment

    • Savage
      Recognized Expert Top Contributor
      • Feb 2007
      • 1759

      #3
      Originally posted by chella
      Hi,

      Please help me to understand what really happens in the below code.

      #define num 20
      main()
      {
      printf("%d,",nu m);
      func();
      printf("%d",num );
      }
      void func()
      {
      #undef num
      #define num 50
      }

      The output is 20,20.
      I expect the output to be 20,50
      It seems to be strange to understand. Help me

      Thanks in advance,
      Chella
      Ok,let's see:

      If I run your program I get 20,20( Dev-C++ on WinXp),but if you comment the function and add
      [CODE="cpp"]#undef num
      #define num 50[/CODE]
      directly into main I got 20,50 which is correct.

      Now,zoddila got 50,50 on Linux which makes me think that result is platform depended.But when I tried the following:

      [CODE="cpp"]void func()
      {
      #define num2 50
      }[/CODE]

      and tried calling it in main it said that num2 is undefined,so it must reside on stack which is cleared when function finishes.So,fur thermore I tried:

      [CODE="cpp"]void func()
      {
      #undef num
      #define num 50
      printf("%d\n",n um);
      }[/CODE]

      and result was correct.As the conclusion:

      Never define a macro inside a function.
      You can define it in main and it will seem as normal but it isn't.Macro will have other value in other functions,so always declare them in global namespace..

      Savage

      Comment

      • Meetee
        Recognized Expert Contributor
        • Dec 2006
        • 928

        #4
        Thanks Savage for explanation

        Regards

        Comment

        • Savage
          Recognized Expert Top Contributor
          • Feb 2007
          • 1759

          #5
          Originally posted by zodilla58
          Thanks Savage for explanation

          Regards
          I' more then happy to help you two..:D

          Savage

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Also remember that macros are handled by the preprocessor, as are most # direcectives before the code ever gets anywhere near the compiler.

            I recomend as an exercise that, if your compiler supports it, you output the code after preprocessing. This is the actual code compiled and normally you will find that the file is very long (because all headers have been included) has large amounts of blank lines (from the sections in the headers that are removed though preprocessor directives) and contains no macros, only the values that they had set.

            In the original program by the time the code of function is executed the preprocessor statements it contained are long gone, they do not translate to code and macros are not variables. Macros are text substitutions in the code.

            Comment

            • chella
              New Member
              • Mar 2007
              • 51

              #7
              Thank you very much. All the replies you gave for this problem are valuable!!

              Regards,
              Chella

              Comment

              Working...