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

main()
int i=,g;
g=++i*i+++(--i);
printf g //1
printf(%d, =++i*i+++(--i)); //2

output of both statements 1 and 2 are differnt why

2006-12-30 06:21:58 · 6 answers · asked by nikki 2 in Computers & Internet Programming & Design

6 answers

When you write ++i, it increments values of i by 1. In your first statement you are changing values of "i" so it won't be the same for next "printf" statement.

2006-12-30 06:28:26 · answer #1 · answered by Dharmesh 2 · 0 0

The output is different because the pre and post incremental operators (++) have already operated on the variable "i" once, when you used them to assign a value to "g". Then you operate on "i" a second time in the printf statement, so you change the value of "i" yet again.

This, by the way, is totally obfuscated code -- anybody who worked for me that actually wrote code like this would be fired :)

2006-12-30 06:25:34 · answer #2 · answered by Anonymous · 0 0

I answered your previous question, but here it is again...

main() == wrong. It's int main(). Get it right. Now onto the real problem.

++i*i+++(--i) --> (++i) * (i++) + (--i)

Anyone see the problem here? If you don't, see: http://c-faq.com/expr/ieqiplusplus.html...

i++ causes a side effect. It returns i, and then increments i later on. The problem is the exact moment that side effect happens is undefined.

The standard doesn't say precisely when the side effect must occur. In short, undefined behavior. If this is a homework question, and I was the teacher, I would only allow "undefined" as the correct answer.

Before you say I'm nitpicking, take a look at

a[i] = i++; If you're at a competent level, you should see the immediate danger here.

2006-12-30 07:28:36 · answer #3 · answered by csanon 6 · 0 0

Undefined behaviour. The order of assessment of function arguments isn't unique. So something might desire to take place. you're enhancing a two times between sequence factors. I quote: "between consecutive "sequence factors" an merchandise's value may be changed basically as quickly as by potential of an expression. The c programming language defines right here sequence factors ... Left operand of the comma operator. The left operand of the comma operator is punctiliously evaluated and all area outcomes finished in the previous persevering with. the two operands of the comma operator are continuously evaluated. be conscious that the comma operator in a function call does not assure an order of assessment. ... "function-call operator. All arguments to a function are evaluated and all area outcomes finished in the previous get entry to to the function. No order of assessment between the arguments is unique. "

2016-12-31 07:58:52 · answer #4 · answered by valaria 4 · 0 0

well provide with the intial value atleast...................
suppose it is i.e. i=1:

g=(++i)*(i++)+(--i);
2 * 2 + 2
output g=4+2 = 6
value of i=2 at this moment....

again (++i)*(i++)+(--i)
3 * 3 + 3
output=9+3=12
value of i...

funda :l
(1) left to right associativity of aritmetic operators
(2) after the first statment involving i operations the final value of i changes...if u again intialize i=1 inbetween u would get the same output........

i hope its correct....and is similar to your previous question

2006-12-30 06:35:01 · answer #5 · answered by AM 3 · 0 0

Looks like your homework has some syntax errors... but you'd know that if you tried to compile it.

Obviously the output are different; you're printing different values.

2006-12-30 06:27:28 · answer #6 · answered by BigRez 6 · 0 0

fedest.com, questions and answers