this is the code
#include<stdio.h>
void main()
{
int i;
printf("%d, %d",++i,i++);
}
and following is the output

8429558, 8429556

Recommended Answers

All 8 Replies

Two instances of undefined behavior, three if your compiler does not support void main.

  1. The i variable is uninitialized. Anything could be in that memory space, and you are not allowed even to read it.
  2. A sequence point is only guaranteed after all of the function arguments are evaluated. Modifying a variable more than once between sequence points is undefined.

In other words, anything could happen and there is no way to explain your output that is consistent.

That looks like a homework assignment I've seen one of my kids do!

thanks for explaining

i accidently came across this stuff when i compiled my program without initializing it.
i woundered why it returned such a junk value and tried series of other programs like tht and thts it and no homework .i m no kid man

They started with garbage.

The flip side is that items are pushed right to left.
The second argument did a post increment meaning the <i> was pushed first then incremented.
The first argument did a pre-incrment then pushed meaning the <i> was incremented then pushed.
If I had been initialized to 0
then the printout would have been

2 0
thus
i+2 i

Oh, and sorry for the wide meaning of the word kid. My kids range from 24 to 9, I was referring to the 19 year old!

The flip side is that items are pushed right to left.

In the compilers I have worked with, that is true. But the compiler is free to do it the most convenient way for internal handling, which could just as easily be any other order. That is why even if the compiler does something sensible for undefined behavior, you still cannot explain the output consistently.

thankyou for ur replies but after like researching and brainstorming for some time i figured out this thinggs that u guys told and i have explained the same in post of salem and once again thanks for all the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.