473,801 Members | 2,240 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

the question about: printf("%d\t%d\ t%d\t%d\t%d\t%d \n",i,++i,--i,i--,i++,-i--);

int i=8;
printf("%d\t%d\ t%d\t%d\t%d\t%d \n",i,++i,--i,i--,i++,-i--);
printf("%d\n", i);

why the results is:

8 8 7 8 8 -8
7

How to explain?

Mar 9 '07 #1
6 8804
WeiWangJi wrote:
int i=8;
printf("%d\t%d\ t%d\t%d\t%d\t%d \n",i,++i,--i,i--,i++,-i--);
printf("%d\n", i);

why the results is:

8 8 7 8 8 -8
7

How to explain?
There is no explaination.

Because you modify the same varaible more than once in the same function
call your code is illegal, and it's behaviour is undefined.

If you tried the same code on a differnt compiler you would probably get
a different result.

You could even see you program crash, because your code is illegal.

john
Mar 9 '07 #2
On Mar 9, 11:20 am, "WeiWangJi" <WeiWan...@gmai l.comwrote:
int i=8;
printf("%d\t%d\ t%d\t%d\t%d\t%d \n",i,++i,--i,i--,i++,-i--);
printf("%d\n", i);

why the results is:

8 8 7 8 8 -8
7

How to explain?
I guess you want to someone to explain the reason behind this output..
if so, here is some help for you.

cut down the sample to analyze the things:

#include <stdio.h>

int main()
{
int i =8;
printf("%d\t %d\t %d\n", i,--i, i--);
printf("%d \n", i);
return 0;
}

Output is:
% ./a.out
7 7 8
6

printf calculates the arguments from right to left and prints them
from left to right.. so i-- is computed first, then --i and then i.

Cheers
-Vallabha
S7 Software Solutions
http://www.s7solutions.com/

Mar 9 '07 #3
Vallabha wrote:
On Mar 9, 11:20 am, "WeiWangJi" <WeiWan...@gmai l.comwrote:
>>int i=8;
printf("%d\t% d\t%d\t%d\t%d\t %d\n",i,++i,--i,i--,i++,-i--);
printf("%d\n" , i);

why the results is:

8 8 7 8 8 -8
7

How to explain?


I guess you want to someone to explain the reason behind this output..
if so, here is some help for you.

cut down the sample to analyze the things:

#include <stdio.h>

int main()
{
int i =8;
printf("%d\t %d\t %d\n", i,--i, i--);
printf("%d \n", i);
return 0;
}

Output is:
% ./a.out
7 7 8
6

printf calculates the arguments from right to left and prints them
from left to right.. so i-- is computed first, then --i and then i.

Cheers
-Vallabha
S7 Software Solutions
http://www.s7solutions.com/
printf might be calculating things left to right on your compiler, but
there is no requirement for it to do so

I ran the OP code on my compiler and got this

7 7 7 8 7 -8
7

I ran it with optmisation turned on and got this

8 8 8 8 8 -8
7

I tried your code on my compiler and got this

6 6 8
6

but with optmisation turned on this

7 7 8
6

All this results are correct, none of the compilers have bugs, because
quite simply the behaviour of the OP's program, and your program, is
undefined by the C++ standard. One can of course provide an explaination
why one particular compiler produces one particular output, but I don't
think it's particularly useful to do so.

john
Mar 9 '07 #4
John Harrison a écrit :
Vallabha wrote:
>On Mar 9, 11:20 am, "WeiWangJi" <WeiWan...@gmai l.comwrote:
>>int i=8;
printf("%d\t% d\t%d\t%d\t%d\t %d\n",i,++i,--i,i--,i++,-i--);
printf("%d\n" , i);

why the results is:

8 8 7 8 8 -8
7

How to explain?


I guess you want to someone to explain the reason behind this output..
if so, here is some help for you.

cut down the sample to analyze the things:
[snip]

printf calculates the arguments from right to left and prints them
from left to right.. so i-- is computed first, then --i and then i.
>>

printf might be calculating things left to right on your compiler, but
there is no requirement for it to do so
[snip]

All this results are correct, none of the compilers have bugs, because
quite simply the behaviour of the OP's program, and your program, is
undefined by the C++ standard. One can of course provide an explaination
why one particular compiler produces one particular output, but I don't
think it's particularly useful to do so.
No since it is certainly performance related and dependant on strategies
but also compiler flags.

OP: Google for "sequence point c++"
It will explain what is a sequence point and may tell why modifying a
value multiple time in a sequence point is UB.

Michael
Mar 9 '07 #5
On Mar 9, 3:20 pm, "WeiWangJi" <WeiWan...@gmai l.comwrote:
int i=8;
printf("%d\t%d\ t%d\t%d\t%d\t%d \n",i,++i,--i,i--,i++,-i--);
printf("%d\n", i);

why the results is:

8 8 7 8 8 -8
7

How to explain?
This would help you :)
http://www.google.com/search?hl=en&r...ox&q=C+puzzles

Mar 9 '07 #6
On 2007-03-08 23:02:19 -0800, "Vallabha" <vs*********@gm ail.comsaid:
On Mar 9, 11:20 am, "WeiWangJi" <WeiWan...@gmai l.comwrote:
>int i=8;
printf("%d\t%d \t%d\t%d\t%d\t% d\n",i,++i,--i,i--,i++,-i--);
printf("%d\n ", i);

why the results is:

8 8 7 8 8 -8
7

How to explain?

I guess you want to someone to explain the reason behind this output..
if so, here is some help for you.
You aren't helping; you're confusing the issue. The result of that code
is completely undefined.
cut down the sample to analyze the things:

#include <stdio.h>

int main()
{
int i =8;
printf("%d\t %d\t %d\n", i,--i, i--);
printf("%d \n", i);
return 0;
}

Output is:
% ./a.out
7 7 8
6
.... or anything else.
printf calculates the arguments from right to left and prints them
This is simply not true; there is no defined order in which arguments
to a function are evaluated.
from left to right.. so i-- is computed first, then --i and then i.

--
Clark S. Cox III
cl*******@gmail .com

Mar 9 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2861
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew story is so funny that eScrew will have to take break from time to time because eScrew needs some rest from laughing. Oh boy, here it comes... eScrew funny laugh laughing screaming crying must stop can not take any more this is killing eScrew...
220
19184
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
8
2466
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew story is so funny that eScrew will have to take break from time to time because eScrew needs some rest from laughing. Oh boy, here it comes... eScrew funny laugh laughing screaming crying must stop can not take any more this is killing eScrew...
77
5697
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a database from SQL Server to Oracle or DB2 or vice versa... and that's it.... And a lot of these enterprises don't need it as they already know what database they are going to use and they don't plan on switching in and out database in the first...
125
14865
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
5
3006
by: eScrewDotCom | last post by:
www.eScrew.com eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew story is so funny that eScrew will have to take break from time to time because eScrew needs some rest from laughing. Oh boy, here it comes... eScrew funny laugh laughing
81
7360
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there be any advantage in having strcat and strcpy return a pointer to the "end" of the destination string rather than returning a
0
2461
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew story is so funny that eScrew will have to take break from time to time because eScrew needs some rest from laughing. Oh boy, here it comes... eScrew funny laugh laughing screaming crying must stop can not take any more this is killing eScrew...
7
3158
by: Edward Yang | last post by:
A few days ago I started a thread "I think C# is forcing us to write more (redundant) code" and got many replies (more than what I had expected). But after reading all the replies I think my question about local variable initialization is still not solved. And some of the replies forked into talking about out parameters. And the thread is becoming way too deep. So I open a new thread here. My question in the previous thead has turned...
0
9698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9558
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10298
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10277
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9105
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6833
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2963
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.