a new question

Discussion in 'C++' started by c/c++ programming lover, Nov 13, 2008.

  1. I'm sorry I made a mistake last time,Now the correct question is :
    #include<stdio.h>
    int main()
    {
    int a=5;
    printf("%d",++a*++a);
    return 0;
    }
    the result is 49.Could anyone tell me why?
    thank you.
     
    c/c++ programming lover, Nov 13, 2008
    #1
    1. Advertising

  2. c/c++ programming lover

    Ian Collins Guest

    c/c++ programming lover wrote:
    > I'm sorry I made a mistake last time,Now the correct question is :
    > #include<stdio.h>
    > int main()
    > {
    > int a=5;
    > printf("%d",++a*++a);
    > return 0;
    > }
    > the result is 49.Could anyone tell me why?


    They already have.

    --
    Ian Collins
     
    Ian Collins, Nov 13, 2008
    #2
    1. Advertising

  3. c/c++ programming lover

    Michael Guest

    c/c++ programming lover wrote:
    > I'm sorry I made a mistake last time,Now the correct question is :
    > #include<stdio.h>
    > int main()
    > {
    > int a=5;
    > printf("%d",++a*++a);
    > return 0;
    > }
    > the result is 49.Could anyone tell me why?
    > thank you.

    ++a*++a is undefined behaviour, the compiler can produce whatever it
    likes. For example, it can yield 36,42,49,999,-1,segfault,or even launch
    a rocket (if there is suitable hardware connected to it).
     
    Michael, Nov 13, 2008
    #3
  4. c/c++ programming lover wrote:
    > I'm sorry I made a mistake last time,Now the correct question is :
    > #include<stdio.h>
    > int main()
    > {
    > int a=5;
    > printf("%d",++a*++a);
    > return 0;
    > }
    > the result is 49.Could anyone tell me why?


    Your question has already been answered in your original thread.
    Expression '++a * ++a' has no meaning in C++. It produces undefined
    behavior, because it modifies the same variable twice. The result is
    unpredictable, meaning that there's no answer to your "why".

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Nov 13, 2008
    #4
  5. c/c++ programming lover

    Salt_Peter Guest

    On Nov 12, 10:23 pm, "c/c++ programming lover" <>
    wrote:
    > I'm sorry I made a mistake last time,Now the correct question is :
    > #include<stdio.h>
    > int main()
    > {
    >     int a=5;
    >     printf("%d",++a*++a);
    >     return 0;}
    >
    > the result is 49.Could anyone tell me why?
    > thank you.


    its undefined behavior, as was explained to you last time you asked.
    That means that what result you might get is not guaranteed by the
    language.
    The fact that you got 49 is irrelevant.

    You have to use sequence points to get a defined result:

    int a=5;
    ++a;
    ++a;
    printf("%d",a * a);
     
    Salt_Peter, Nov 13, 2008
    #5
  6. c/c++ programming lover

    Rolf Magnus Guest

    Michael wrote:

    > c/c++ programming lover wrote:
    >> I'm sorry I made a mistake last time,Now the correct question is :
    >> #include<stdio.h>
    >> int main()
    >> {
    >> int a=5;
    >> printf("%d",++a*++a);
    >> return 0;
    >> }
    >> the result is 49.Could anyone tell me why?
    >> thank you.

    > ++a*++a is undefined behaviour, the compiler can produce whatever it
    > likes. For example, it can yield 36,42,49,999,-1,segfault,or even launch
    > a rocket (if there is suitable hardware connected to it).


    Nope. The C++ standard doesn't require a suitable hardware. Without it,
    the nuclear strike may be unlikely, but not forbidden by the standard.
     
    Rolf Magnus, Nov 13, 2008
    #6
  7. c/c++ programming lover

    Rolf Magnus Guest

    c/c++ programming lover wrote:

    > I'm sorry I made a mistake last time,Now the correct question is :
    > #include<stdio.h>
    > int main()
    > {
    > int a=5;
    > printf("%d",++a*++a);
    > return 0;
    > }
    > the result is 49.Could anyone tell me why?


    The answer is: Why not?
     
    Rolf Magnus, Nov 13, 2008
    #7
  8. Michael wrote:
    > ++a*++a is undefined behaviour, the compiler can produce whatever it
    > likes. For example, it can yield 36,42,49,999,-1,segfault,or even launch
    > a rocket (if there is suitable hardware connected to it).


    Isn't that contradictory with the standard definition of the prefix
    operator ++, as using it is not undefined behavior?

    Does the standard really explicitly state that it's undefined
    behavior, rather than saying something along the lines that the
    (numerical) result is undefined? (In other words, the expression itself
    is completely valid and will produce an integer value as result. It's
    just not guaranteed what that value will be.)
     
    Juha Nieminen, Nov 13, 2008
    #8
  9. Juha Nieminen wrote:
    > Does the standard really explicitly state that it's undefined
    > behavior,


    Yes. 5/4.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Nov 13, 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. Robb Smith
    Replies:
    2
    Views:
    602
    Saravana
    Jul 18, 2003
  2. Dodo
    Replies:
    1
    Views:
    376
    Ron Natalie
    Aug 26, 2003
  3. George2
    Replies:
    1
    Views:
    356
    James Kanze
    Jan 29, 2008
  4. Sue
    Replies:
    8
    Views:
    155
    Thomas 'PointedEars' Lahn
    Dec 13, 2003
  5. Replies:
    2
    Views:
    513
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page