Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > tricky output ???

Reply
Thread Tools

tricky output ???

 
 
onkar
Guest
Posts: n/a
 
      02-16-2008
#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

 
Reply With Quote
 
 
 
 
onkar
Guest
Posts: n/a
 
      02-16-2008
On Feb 16, 11:17 am, Richard Heathfield <(E-Mail Removed)> wrote:
> 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?
>
> --
> Richard Heathfield <http://www.cpax.org.uk>
> Email: -http://www. +rjh@
> Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
> "Usenet is a strange place" - dmr 29 July 1999



I was expecting 4 5 5 4 5
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      02-16-2008
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?


--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      02-16-2008
onkar said:

> On Feb 16, 11:17 am, Richard Heathfield <(E-Mail Removed)> wrote:
>> 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?

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

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-16-2008
onkar <(E-Mail Removed)> writes:
> #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.)

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-16-2008
onkar wrote:
> #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?

--
Ian Collins.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      02-16-2008
Ian Collins said:

> onkar wrote:
>> #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?


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

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      02-16-2008
onkar wrote:
> #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 Removed)" <(E-Mail Removed)>
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 <(E-Mail Removed)>
Subject: tricky output ???
Date: Fri, 15 Feb 2008 22:09:44 -0800 (PST)
 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      02-16-2008

"Martin Ambuhl" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> onkar wrote:


>> printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);


> This post, with the same stupidity,
> From: "(E-Mail Removed)" <(E-Mail Removed)>
> 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.

--
Bart


 
Reply With Quote
 
Randy Howard
Guest
Posts: n/a
 
      02-16-2008
On Sat, 16 Feb 2008 01:04:31 -0600, Ian Collins wrote
(in article <(E-Mail Removed)>):

> onkar wrote:
>> #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?


New semester started recently.



--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw





 
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
a tricky if else(maybe not tricky but impossible) nirkheys@gmail.com C Programming 9 04-25-2006 06:13 PM
Tricky NAT config on a 1721 Dan Rice Cisco 2 08-01-2005 03:43 PM
Tricky AUTOLOAD behavior Jim Schueler Perl 1 08-25-2004 08:00 AM
OSPF & BGP interaction.. Tricky problem? or just stupid.. Kris Cisco 2 06-20-2004 04:01 PM
Tricky Question: Page_Load VB Programmer ASP .Net 3 06-30-2003 08:03 PM



Advertisments