tricky output ???

O

onkar

#include<stdio.h>
int main(void){
int i=5;
printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);
return 0;
}


This gives me : 4 5 5 5 5

How ???

Can anyone please give me some hint as to why I am getting this ??

Thanks & regards,
Onkar
 
R

Richard Heathfield

onkar said:
#include<stdio.h>
int main(void){
int i=5;
printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);
return 0;
}


This gives me : 4 5 5 5 5

How ???

Can anyone please give me some hint as to why I am getting this ??

What answer were you expecting, and why?
 
R

Richard Heathfield

onkar said:
I was expecting 4 5 5 4 5

From your reply, I deduce that you were expecting the compiler to evaluate
the printf arguments from right to left. But in fact C doesn't guarantee
any particular evaluation order for function arguments. To get the output
you require, do this:

#include <stdio.h>

int main(void)
{
int i = 5;
int z = i;
int y = --i;
int x = ++i;
int w = i--;
int v = i++;
printf("%d %d %d %d %d\n", v, w, x, y, z);
return 0;
}

This guarantees the output 4 5 5 4 5

It is always a bad idea to make a line of C code do too much. Break complex
expressions down into stuff you can actually understand. (If you thought
you understood printf("%d %d %d %d %d\n",i++,i--,++i,--i,i), it should be
evident from your surprise at the output that you were mistaken to think
that.)

In general, if you're using ++ or -- on any particular object (or indeed
modifying the object's value in any way), that should be the only place
you use that object within that whole statement. Otherwise, the behaviour
of the code is not defined by the language spec.
 
K

Keith Thompson

onkar said:
#include<stdio.h>
int main(void){
int i=5;
printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);
return 0;
}


This gives me : 4 5 5 5 5
[...]

The FAQ for this newsgroup is at <http://www.c-faq.com/>.

You desperately need to read section 3. (Then read the rest of it.)
 
I

Ian Collins

onkar said:
#include<stdio.h>
int main(void){
int i=5;
printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);
return 0;
}
What's brought all these daft UB questions out of the woodwork this week?
 
R

Richard Heathfield

Ian Collins said:
What's brought all these daft UB questions out of the woodwork this week?

What makes you think there's anything special about this week? :)
 
M

Martin Ambuhl

onkar said:
#include<stdio.h>
int main(void){
int i=5;
printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);
return 0;
}


This gives me : 4 5 5 5 5

How ???

Can anyone please give me some hint as to why I am getting this ??

Have you not been paying attention? How many times do we have to answer
the same half-assed question here? Not only is this covered in three
separate questions in the FAQ, but

This post, with the same stupidity,
From: "(e-mail address removed)" <[email protected]>
Subject: Funny results with increment decrement operators
Date: Fri, 15 Feb 2008 16:00:33 -0800 (PST)

was posted (and answered) only 6 hours before yours

From: onkar <[email protected]>
Subject: tricky output ???
Date: Fri, 15 Feb 2008 22:09:44 -0800 (PST)
 
B

Bartc

Martin Ambuhl said:
onkar wrote:
This post, with the same stupidity,
From: "(e-mail address removed)" <[email protected]>
Subject: Funny results with increment decrement operators
Date: Fri, 15 Feb 2008 16:00:33 -0800 (PST)

was posted (and answered) only 6 hours before yours

And in comp.programming a week ago.

It can only be a favourite assignment of C/programming classes.
 
R

Richard

Richard Heathfield said:
onkar said:


What answer were you expecting, and why?

it should be blatantly obvious why he doesn't think this is right. The
fact he would be wrong is besides the point.
 
S

santosh

onkar said:
#include<stdio.h>
int main(void){
int i=5;
printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);
return 0;
}


This gives me : 4 5 5 5 5

How ???

Can anyone please give me some hint as to why I am getting this ??

Thanks & regards,
Onkar

This guy is doubtless a troll. He keeps posting such stupid questions
even after months of being told about the FAQ.
 
K

Kenneth Brody

Richard said:
it should be blatantly obvious why he doesn't think this is right. The
fact he would be wrong is besides the point.

But, until one starts to think about "why" he thinks the answer
should be what he thinks it should be, you cannot explain to him
the flaws in his thinking.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Kenneth Brody

Ian said:
What's brought all these daft UB questions out of the woodwork this week?

Perhaps a new semester started a few weeks ago, and the instructors
keep on bringing out the same tired "let's invoke UB" questions?

What I want to know is why the instructors ask these questions? I
somehow doubt that most of them are trying to teach about UB, but
instead are assuming that "all the world's a VAX". (Or whatever is
the 21st century equivalent. Perhaps "all the world's a Windows
system using MSVC"?)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
R

Richard Heathfield

Kenneth Brody said:
But, until one starts to think about "why" he thinks the answer
should be what he thinks it should be, you cannot explain to him
the flaws in his thinking.

In fact, his answer to my question gave me a clue as to what he was
thinking. He might equally have said: 5 6 6 5 5, in which case my answer
would have been slightly different.

As usual, Richard Riley is sniping ignorantly, caustically, and
uncomprehendingly at people who are actually supplying help, instead of
taking the risk of supplying help himself to those who are asking for it.
He may safely be ignored, since he never contributes anything useful.
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top