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

what will be the value of n and i after the statement.
i = 0;
n = i++ + ++i + i++ + ++i;

2006-10-31 05:27:08 · 4 answers · asked by hari 2 in Science & Mathematics Mathematics

and can u explain the change in i in each step.,

2006-10-31 05:28:48 · update #1

4 answers

n=8 and i=4.

After the first i++, i=1, but because it's a post-increment, n has nothing being added to it yet.

After the first ++i, i=2, and because it's a pre-increment, 2 is contributed to the value of n.

After the second i++, i=3, but only 2 more is contributed to n, because it's a post-increment.

After the second ++i, i=4, and because it's a pre-increment, 4 more is added to n.

2006-10-31 05:32:29 · answer #1 · answered by James L 5 · 2 1

James L is being too clever. There is an internationally agreed C language standard, and according to it, the values of n and i after these statements are UNDEFINED. They can be different with different compilers on the same system, and on different systems. They do not even need to be the same for two successive uses of the same compiler on the same system. They are UNDEFINED.

If you look at the generated object code, or think about possible meanings, you may be able to explain (as James has) why it gives a particular result in a particular case, but the reasoning may not apply to any other case. The effect of the statements is UNDEFINED.

2006-10-31 16:36:08 · answer #2 · answered by bh8153 7 · 0 0

i=0

i++ is undefined and produces an error message

2006-10-31 13:29:15 · answer #3 · answered by fcas80 7 · 0 1

duh?/!! ask a HARD question...

2006-10-31 13:32:59 · answer #4 · answered by Marshall Lee 4 · 1 1

fedest.com, questions and answers