Preprocessor directive

Discussion in 'C Programming' started by asit, Jan 13, 2008.

  1. asit

    asit Guest

    #include <stdio.h>
    #define ADD(x) x + x + x

    int main()
    {
    int a=5;
    printf("%d",ADD(a++));
    printf("\n");
    getch();
    return 0;
    }

    In the above problem, the output should be 18. But my gcc compiler
    shows 15. Why ???

    if it's UB, then tell me the reason ??

    thank you
    asit, Jan 13, 2008
    #1
    1. Advertising

  2. asit wrote:
    > #include <stdio.h>
    > #define ADD(x) x + x + x
    >
    > int main()
    > {
    > int a=5;
    > printf("%d",ADD(a++));
    > printf("\n");
    > getch();
    > return 0;
    > }
    >
    > In the above problem, the output should be 18. But my gcc compiler
    > shows 15. Why ???
    >
    > if it's UB, then tell me the reason ??
    >
    > thank you

    printf("%d",ADD(a++));
    expands to
    printf("%d",a++ + a++ + a++));
    Here you get and modify a several times without a sequence point in
    between -> UB

    Bye, Jojo
    Joachim Schmitz, Jan 13, 2008
    #2
    1. Advertising

  3. "asit" <> wrote in message
    news:...
    > #include <stdio.h>
    > #define ADD(x) x + x + x

    I hope that if you learn anything from Keith Thompson, it is to not
    do this. ^^^^
    --

    "We are being told that a competent, trustworthy president is someone
    who brandishes his religion like a neon sign, loads a gun and goes out
    hunting for beautiful winged creatures, and tries to imitate a past
    president who, by the way, never shot a bird or felt the need to imitate
    anybody."

    ~~ Patti Davis Is Not Flattered by GOP Candidates' Pale Imitations of
    Her Father



    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Reagan Revision, Jan 13, 2008
    #3
  4. asit

    Guest

    On Jan 13, 11:49 am, asit <> wrote:
    > #include <stdio.h>
    > #define ADD(x) x + x + x
    >
    > int main()
    > {
    > int a=5;
    > printf("%d",ADD(a++));
    > printf("\n");
    > getch();
    > return 0;
    >
    > }
    >
    > In the above problem, the output should be 18. But my gcc compiler
    > shows 15. Why ???
    >
    > if it's UB, then tell me the reason ??

    'a' is modified more than once with no sequence points in between.
    Furthermore, you call getch() without definition, which is not a
    standard function.
    The code shouldn't even compile because of this.
    , Jan 13, 2008
    #4
  5. On Jan 13, 9:49 am, asit <> wrote:

    > #define ADD(x) x + x + x


    this macro may surprise you in other ways as well
    try:

    printf (ADD(2) * 4);

    --
    Nick Keighley
    Nick Keighley, Jan 13, 2008
    #5
  6. writes:
    > On Jan 13, 11:49 am, asit <> wrote:
    >> #include <stdio.h>
    >> #define ADD(x) x + x + x
    >>
    >> int main()
    >> {
    >> int a=5;
    >> printf("%d",ADD(a++));
    >> printf("\n");
    >> getch();
    >> return 0;
    >>
    >> }
    >>
    >> In the above problem, the output should be 18. But my gcc compiler
    >> shows 15. Why ???
    >>
    >> if it's UB, then tell me the reason ??

    > 'a' is modified more than once with no sequence points in between.
    > Furthermore, you call getch() without definition, which is not a
    > standard function.
    > The code shouldn't even compile because of this.


    The code should compile under a C90 implementation. The compiler will
    assume that getch() is a function returning int. It may fail to link
    or exhibit undefined behavior during execution if getch() doesn't
    actually exist, or if it returns something other than int or expects
    one or more arguments.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 13, 2008
    #6
  7. Nick Keighley <> writes:
    > On Jan 13, 9:49 am, asit <> wrote:
    >> #define ADD(x) x + x + x

    >
    > this macro may surprise you in other ways as well
    > try:
    >
    > printf (ADD(2) * 4);


    Here's another example for fans of HHGTTG.

    #include <stdio.h>

    #define SIX 1+5
    #define NINE 8+1

    int main(void)
    {
    printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
    return 0;
    }

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 13, 2008
    #7
  8. asit wrote:
    > #include <stdio.h>
    > #define ADD(x) x + x + x
    >
    > int main()
    > {
    > int a=5;
    > printf("%d",ADD(a++));
    > printf("\n");
    > getch();
    > return 0;
    > }
    >
    > In the above problem, the output should be 18.


    No, it has _no_ defined or expected behavior.

    But my gcc compiler
    > shows 15. Why ???


    Because that's how your compiler chose to interpret the malformed code
    you wrote.

    > if it's UB, then tell me the reason ??


    Check the FAQ. This is baby stuff.
    Martin Ambuhl, Jan 13, 2008
    #8
  9. asit wrote:
    >
    > #include <stdio.h>
    > #define ADD(x) x + x + x

    [...]
    > printf("%d",ADD(a++));

    [...]
    > if it's UB, then tell me the reason ??


    Play compiler. What does the printf line expand to?

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Jan 15, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Trying_Harder

    Preprocessor directive

    Trying_Harder, Oct 2, 2003, in forum: C Programming
    Replies:
    16
    Views:
    660
    Kevin Easton
    Oct 5, 2003
  2. Replies:
    3
    Views:
    508
    Default User
    Mar 13, 2007
  3. Replies:
    1
    Views:
    371
    Ian Collins
    Mar 13, 2007
  4. Replies:
    2
    Views:
    420
    Walter Roberson
    Nov 28, 2007
  5. Replies:
    5
    Views:
    461
    Joe Greer
    Jan 8, 2008
Loading...

Share This Page