can anyone please tell me why following output is generated ...

int i=10;
printf(" %d ",i++/i); // output=0
i=10;
printf("%d",i/++i); //output=1
i=10;
printf("%d ",i++/++i); //output =0
i=10;
printf("%d",++i/i); //output=1

Recommended Answers

All 9 Replies

can anyone please tell me why following output is generated ...

int i=10;
printf(" %d ",i++/i); // output=0

i=10;
printf("%d",i/++i); //output=1
i=10;
printf("%d ",i++/++i); //output =0
i=10;
printf("%d",++i/i); //output=1

euhh.. you might be using a broken computer

In all the cases, your code always prints 1.

1) i++/i = 10/10, and afterwards i=11

2) i/++i = 11/11 and afterwards i=11

3) i++/++i = 11/11, and afterwards i=12

4) ++i/i = 11/11 and afterwards i=11.

euhh.. you might be using a broken computer

In all the cases, your code always prints 1.

1) i++/i = 10/10, and afterwards i=11

2) i/++i = 11/11 and afterwards i=11

3) i++/++i = 11/11, and afterwards i=12

4) ++i/i = 11/11 and afterwards i=11.

thts what i thought but compiler is giving different outputs ...
i guess if u think outputs are incorrect ..u can check on compiler and please let me know about the outputs ...

there is nothing wrong with your compiler. It does not work as you expect because the code you posted as undefined behavior.

I agree with Ancient Dragon, the code is broken.

You basically have variations of the problems listed here
http://c-faq.com/expr/index.html

hi salem ...
thanks for such a gud site ..
so according to you it depends on compiler ...
but this question appears in IBM interview ..so what to answer there ???

can anyone please tell me why following output is generated ...

int i=10;
printf(" %d ",i++/i); // output=0
i=10;
printf("%d",i/++i); //output=1
i=10;
printf("%d ",i++/++i); //output =0
i=10;
printf("%d",++i/i); //output=1

in 1st 10/10 will be 1
after that i will be 11
in 2nd 11/11 is 1 thats why o/p is 1
in 3rd11/12 is 0.91 ant u have declared it as integer type thats why 0 is o/p
in 4th 11/10 is always 1 if it is declared as int type

sagar: you are attempting to explain away something that has unspecified behavior -- the standards say it is unspecified, not me. For more information see this thead.

> so according to you it depends on compiler ...
No, it's undefined.
http://c-faq.com/ansi/undef.html

You need to understand these cases.

1. Defined - every compiler does the same thing.
Eg. that && and || are short-circuited.

2. Implementation defined - compilers must document what they do
Eg. what right-shift >> does on signed negative integers.
Some implementations shift in zero, others shift in the sign bit. Both are correct, yet can cause problems for your code.

3. Unspecified - like implementation-defined, but undocumented.
Eg. the order of evaluation of sub-expressions.

4. Undefined - absolutely anything can happen.
You may get the answer you expect, you may get an unexplainable answer, you may get a crash in your program.
You may even get a crash (or worse) in your OS.
There is no point in even trying to understand what your compiler does, because whatever it does, it doesn't have to be consistent about it.
And everyone elses compiler is going to be different anyway.

Code which relies only on case 1 is good code.
Case 2 is OK, so long as it's well documented, but it would be much better avoided if at all possible.
Code which relies on 3 or 4 is on very shaky ground, and is best avoided.

i completely agree with salem ...i have checked at many other sites too ...it always depend on compiler ....

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.