sequence point

D

Debajyoti Sarma

#include<stdio.h>
int main()
{
int i=1;
printf("\n%d",i++*++i); //4
i=1;
printf("\n%d",i++*++i*i++); //8
i=1;
printf("\n%d",i++*++i*++i); //12
i=1;
printf("\n%d",++i*++i*++i*++i); //180
i=1;
printf("\n%d",++i*++i*++i*i++); //144
}
please explain how sequence point concept works ...
when i m not able to trace longer expressions.
 
A

Alan Curry

#include<stdio.h>
int main()
{
int i=1;
printf("\n%d",i++*++i); //4
i=1;
printf("\n%d",i++*++i*i++); //8
i=1;
printf("\n%d",i++*++i*++i); //12
i=1;
printf("\n%d",++i*++i*++i*++i); //180
i=1;
printf("\n%d",++i*++i*++i*i++); //144
}
please explain how sequence point concept works ...
when i m not able to trace longer expressions.

Sorry, the correct answers are:

i++*++i : evaluate to 4 and reload inetd.conf

i++*++i*i++ : evaluate to 8 and clear Freecell win/loss history

i++*++i*++i : evaluate to 12 and rewind SCSI tape drive

++i*++i*++i*++i : evaluate to 180 and toggle the state of the "enable
Javascript" checkbox in any installed Mozilla-derived
products

++i*++i*++i*i++ : evaluate to 144 and launch stealth background process
to take control of the CD drive, which will use its
tray to grab you by the balls the next time you walk
by the server rack, and then having got your attention
the internal speaker will yell "WHY CAN'T YOU JUST
WRITE REASONABLE CODE IN THE FIRST PLACE? YOU THINK
PACKING IN MORE SIDE EFFECTS PER STATEMENT MAKES YOU
COOL?"
 
B

Barry Schwarz

#include<stdio.h>
int main()
{
int i=1;
printf("\n%d",i++*++i); //4
i=1;
printf("\n%d",i++*++i*i++); //8
i=1;
printf("\n%d",i++*++i*++i); //12
i=1;
printf("\n%d",++i*++i*++i*++i); //180
i=1;
printf("\n%d",++i*++i*++i*i++); //144
}
please explain how sequence point concept works ...
when i m not able to trace longer expressions.

The only sequence points in your code are at the end of each
statement, just prior to the call to printf, and just after printf
returns. In particular, there are no sequence points in the
evaluation of the arguments to be passed to printf.

Consequently, everyone of your function calls invokes undefined
behavior. See section 6.5-2 for the details.
 
D

Denis McMahon

++i*++i*++i*++i : evaluate to 180 and toggle the state of the "enable
Javascript" checkbox in any installed Mozilla-derived
products

I thought that was 72? Surely 180 is: "Transmit my location to the
targeting module of the nearest hellfire equipped UAV."

Rgds

Denis McMahon
 
N

Nick Keighley

Sorry, the correct answers are:

i++*++i : evaluate to 4 and reload inetd.conf

<snip>

Alan Curry is attempting to be funny. According to the C standard the
expression
i++ * ++i
is undefined (BTW is your space key broken?). This means the
implementor (the compiler writer) is permitted to anything he likes.
Alan Curry is merely picking some interesting possibilities for the
implementor. Apparently there *have* been cases of undefined behaviour
trying to delete files, though I find it unlikely the incrmenting a
variable multiple times will do something like that.
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top