English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

main()
{
int i=5;
i= -i++ + ++i;
printf("%d",i);
}the output was 1.
main()
{
int i=5;
printf("%d",-i++ + ++i);
}
the output was 2.
tell me why the answer differs when the expression is put inside the printf and why it is different when it is outside.

2006-09-27 03:15:35 · 5 answers · asked by joe m 2 in Computers & Internet Programming & Design

5 answers

I got a better question:
Why are you using '-i++ + ++i' in the first place? Don't use the ++ and -- operators for basic arithmetic because it increases the chance of a typo or logic error.
Usually, ++ and -- are used in a loop.
In your first program 'i' gets translated like this:
i=-i++ + ++i
i=(NOT 5)+1 + (5+1)
i=(-6)+1 + 6
i= -5 + 6
Notice the the '-' in front of i is a NOT, not a negative.

CORRECTION:
Oops, thought that was a tilde you put in front of the i (my monitor needs cleaning).
In the first program, '-i' is treated as negetive 'i'. Remember that post-fix ++ is different from prefix ++. A post-fix operand doesn't increment immediately. If you put a post-fixed variable on a line and do other arithmetic on it, the increment won't happen. Try putting the 'i++' on a line of it's own, and then add i to '++i'.
Imagine a while loop with an 'i++;' in it, or a for loop. In those loops, the 'i++' does not get evaluated immediately. Only on the next line after 'i++' does the i variable get incremented.

So:
(-5) +(5+1)
(-5) + (6)
i=1

Second program:
-5+1 + 5+1
-4+6

In the second program, the second parameter to printf() is a single integer variable. With the post-fix and pre-fix operating on the same variable for a function parameter, the compiler gets confused with the order of operation.
It essentially goes from left to right.
In your first program, the '++i' was evaluated first and 'i++' was never evaluated.
I think this is a bug with your compiler.
'i++' within printf() shouldn't get evaluated.

P.S.
If you keep programming like this, you'll never make a good programmer.
Simplify, and don't calculate anything within a procedure. If you're doing a list of arithmetic instructions, use parenthesis so the compiler knows what to do.

2006-09-27 04:12:16 · answer #1 · answered by Balk 6 · 0 0

Its really a good question .... which adds one more mystery to C language.
R u using turbo C/C++ compiler ?
I think .. fundamentally, the output of second main() should be 0. but surprisingly it is giving 2.

but there is one more twist in this ..
try to run this program in VC++ compiler.
its is giving just opposit result ..
i.e. 2 for the first main()
& 1 for the second main.

well, ur question is in my watchlist and i m serching for the answer .. u can contact me .. i have some more such mysterious questions ..for u

2006-09-27 04:38:32 · answer #2 · answered by Digitally Й!Й 3 · 0 0

it's different from the outside to the inside. i thyinky wiht your superb brain. that you should get yourself sectioned. and spend your time solving the many disorders you will encounter there you will be famous

2006-09-27 03:28:54 · answer #3 · answered by briangimma 4 · 0 0

Try it to understand by yourself.
Debug and watch the code then u will understand.

2006-09-27 04:11:26 · answer #4 · answered by Answer Answer 4 · 0 0

your name is Crazy Joe isn't it?

2006-09-27 03:17:48 · answer #5 · answered by volksbank 4 · 0 1

fedest.com, questions and answers