a new question

  • Thread starter c/c++ programming lover
  • Start date
C

c/c++ programming lover

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.
 
I

Ian Collins

c/c++ programming lover said:
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.
 
M

Michael

c/c++ programming lover said:
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).
 
A

Andrey Tarasevich

c/c++ programming lover said:
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".
 
S

Salt_Peter

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);
 
R

Rolf Magnus

Michael said:
++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.
 
R

Rolf Magnus

c/c++ programming lover said:
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?
 
J

Juha Nieminen

Michael said:
++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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top