preprocessor problemn

This is a discussion on preprocessor problemn within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #define SQR(A) A*A main() { int x=5,y; y=4*SQR(x-3); printf("%d",y); } Here output is 2. But I am not ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    10

    preprocessor problemn

    Code:
    #include<stdio.h>
    #define SQR(A) A*A
    
    main()
    {
     int x=5,y;
     y=4*SQR(x-3);
    printf("%d",y);
    }
    Here output is 2.
    But I am not able to understand why the output becomes 2 ?
    In our stdy book we dont get enough dicussion in this preprocessor topic. But I know that ntire macro expression should be enclosed with in paranthesis.So defining #define SQR(A) A*A is wrong it should be like this #define SQR(A) (A*A).I have no clear idea when we wrongly define #define SQR(A) A*A and I think it will not work at all and then i think the output of the above programme is 4.
    Help me to understand the code and why output is 2. Give some link on preprocessor to get clear idea about it.

  2. #2
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    When the preprocessor processes your program, it becomes:
    Code:
    /* a bunch of included header file code from stdio.h here */
    main()
    {
     int x=5,y;
     y=4*x-3*x-3;
    printf("%d",y);
    }
    The A*A is replaced with whatever you have in the parentheses after SQRT. In this case x-3 replaces A in both places, so x-3*x-3. 4*5-3*5-3 == 20-15-3 == 2.

    If you want to use a preprocessor macro for SQRT, you can not put anything except a single value as the parameter or "strange" results like this will occur. If you want to have a sqrt() function that you can call with x-3 (or any other expression), you need to make it a function.
    Last edited by jEssYcAt; 04-09-2009 at 02:01 AM.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,198
    Quote Originally Posted by jEssYcAt View Post
    If you want to use a preprocessor macro for SQRT, you can not put anything except a single value as the parameter or "strange" results like this will occur. If you want to have a sqrt() function that you can call with x-3 (or any other expression), you need to make it a function.
    It is possible to reduce the strange effects of the macro, with extra brackets.
    Code:
    #define SQR(A) ((A)*(A))
    As long as the argument of the macro doesn't have side effects, that will work. 4*SQR(x-3) will expand to 4*((x-3)*(x-3)) which is what you want.

    The catch is it won't work with expressions that have side effects. For example SQR(++x) will expand to 4*((++x)*(++x)) which modifies x twice and causes undefined behaviour. In such cases, a function is needed (eg so x is only incremented once and its value retrieved, before calling the function).

    The thing to remember is that macros are not functions, even if they sort of look like them. They perform text substitution on the code that is actually compiled.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

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