Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > a new question

Reply
Thread Tools

a new question

 
 
c/c++ programming lover
Guest
Posts: n/a
 
      11-13-2008
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.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      11-13-2008
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
 
Reply With Quote
 
 
 
 
Michael
Guest
Posts: n/a
 
      11-13-2008
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).
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-13-2008
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
 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      11-13-2008
On Nov 12, 10:23*pm, "c/c++ programming lover" <(E-Mail Removed)>
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);

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      11-13-2008
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.


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      11-13-2008
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?


 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      11-13-2008
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.)
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-13-2008
Juha Nieminen wrote:
> Does the standard really explicitly state that it's undefined
> behavior,


Yes. 5/4.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript new-new-new-new-newbee weblinkunlimited@gmail.com Javascript 2 03-11-2008 01:15 AM
A new hard drive question: move files to new HD Jer Computer Support 4 12-01-2005 09:10 AM
New computer, New OS, New Wireless Problem :-\ =?Utf-8?B?RGFu?= Wireless Networking 3 07-31-2005 02:11 PM
overloaded global operator new/new[] and corresponding deletes question Dodo C++ 1 08-26-2003 01:59 PM



Advertisments