Results 1 to 6 of 6

Thread: preprocessor

  1. #1
    Junior Member
    Join Date
    Sep 2007
    Answers
    19

    preprocessor

    #define a 10
    main()
    {
    printf("%d..",a);
    foo();
    printf("%d",a);
    }
    void foo()
    {
    #undef a
    #define a 50
    }
    The output is 10..10 how it will come


  2. #2
    Contributing Member
    Join Date
    Sep 2006
    Answers
    42

    Re: preprocessor

    Hi,
    The preprocessor is invoked by the compiler as the first phase in the translation process. The preprocessor replaces all occurrences of the macro with the value. In the above program the second definition of a is after the second printf(). Hence the O/P is 10..10


  3. #3
    Junior Member
    Join Date
    Sep 2007
    Answers
    19

    Re: preprocessor

    But this one dispaly 50..50
    #define a 10
    foo()
    {
    #undef a
    #define a 50
    }
    main()
    {
    printf("%d..",a);
    foo();
    printf("%d",a);
    }


  4. #4
    Contributing Member
    Join Date
    Sep 2006
    Answers
    42

    Re: preprocessor

    #define a 10
    print() {
    printf("%d..",a);
    }

    foo()
    {
    #undef a
    #define a 50
    }
    main()
    {
    printf("%d..",a);
    foo();
    printf("%d",a);
    print();
    }

    Check this out. This will make it clear.

    (The o/p should be 10..50..50)




    --Smile, it makes people wonder what you are thinking.


  5. #5
    Junior Member
    Join Date
    Sep 2007
    Answers
    19

    Re: preprocessor

    hi... I need a explanation for this..
    #define f(g,g2) g##g2
    main()
    {
    int var12=100;
    printf("%d",f(var,12));
    }
    the output is 100


  6. #6
    Contributing Member
    Join Date
    Sep 2006
    Answers
    42

    Re: preprocessor

    The very first thing that a C compiler does is:
    It invokes the preprocessor. The function of the preprocessor is to replace all preprocessor directives with their appropriate substitution.

    Consider the program that you have:

    #define f(g,g2) g##g2
    main()
    {
    int var12=100;
    printf("%d",f(var,12));
    }

    In this program ## means concat the two variables passed to this function. This is called Token Concatenation.





    The preprocessor will replace this with:
    main()
    {
    int var12=100;
    printf("%d", var12);
    }

    Go through the following links:
    en . wikipedia . org/wiki/C_preprocessor
    www . cs . utah.edu/dept/old/texinfo/cpp/cpp . html




    --Smile, it makes people wonder what you are thinking.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact