By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

why the line "int n =++i+ ++i+ ++i;" gives an compile time error in c language

P: 1
int n =++i+ ++i+ ++i;
this above line give the error in c language why?
Sep 13 '10 #1
Share this Question
Share on Google+
6 Replies

P: 1
Is not giving error....
but i don't understand why n is 22 when I initialized i to 5. I think n should be 21.
Sep 13 '10 #2

P: 39
And why do I get 24, when I run with i=5.

The next code gives 21. as it should
Expand|Select|Wrap|Line Numbers
  1. n = ++i;
  2. n+= ++i;
  3. n+= ++i;
Sep 13 '10 #3

P: 542
Take a look through the next few forum pages where this subject of undefined behavior has been discussed ad nausium.
Sep 13 '10 #4

Expert 100+
P: 542
In my case its 22, when i = 5.
It depends upon compiler and even their versions.

I am using gcc 4.1.2

Here, in my case it happens like this.

when you say, n = ++i+ ++i+ ++i;
Compiler treats it as n = a + b + c;
Now it tries to play with two operands(again order can vary,In this example it doesn't matter)

in this case, it has taken a + b;
so it will try to evaluate a and b first, so
a = ++i and b = ++i, 'i' gets incremented twice.

so a + b = i + i = 7 + 7 = 14

At last it will try to evaluate n = 14 + ++i (remember i is 7)

so, n = 14 + 8 = 22.

Here is the assembly code:

Expand|Select|Wrap|Line Numbers
  1. 0x0804837a <sub+6>:     movl   $0x5,-0x8(%ebp)
  2. 0x08048381 <sub+13>:    addl   $0x1,-0x8(%ebp)
  3. 0x08048385 <sub+17>:    addl   $0x1,-0x8(%ebp)
  4. 0x08048389 <sub+21>:    mov    -0x8(%ebp),%eax
  5. 0x0804838c <sub+24>:    add    -0x8(%ebp),%eax
  6. 0x0804838f <sub+27>:    addl   $0x1,-0x8(%ebp)
  7. 0x08048393 <sub+31>:    add    -0x8(%ebp),%eax
  8. 0x08048396 <sub+34>:    mov    %eax,-0x4(%ebp)
As mentioned by other member that he has got 24.
compiler has certainly evaluated all pre increment operator first to output n = 8 + 8 + 8 = 24
Sep 13 '10 #5

Expert Mod 5K+
P: 8,916
It depends upon compiler and even their versions.
It also depends on the day of the week the weather the colour of the shirt you are wearing and what you had for breakfast this morning be it is undefined behaviour.

When undefined behaviour is invoked the compiler (and program) can do anything it wants and is not constrained to do the same thing each time it is run including
  • Giving the result you are expecting
  • Giving a result close to the result you are expecting
  • Outputting random text
  • Exiting with no output or explanation
  • Raising some sort of exception (SEGV)
  • Formatting your hard disk
  • Emailing your electricity supplier and cancelling your contract
  • Making demons fly out of your nose

The compiler is under NO constraints, undefined behaviour should be avoided at all costs.
int n =++i+ ++i+ ++i;
Is undefined behaviour because the C (and C++) standard states that you may only access a variable once between sequence points if you are accessing it to alter its value (you may of course access it to read any number of times you like if you are not altering its value).

The above line of code only has 1 sequence point, at the ; so it is undefined behaviour surrounding the multiple accesses to i for write.

Other things can cause undefined behaviour, are writing to an invalid memory location or a memory location outside the bounds of an object, main not returning int (in C++) .

Wikipedia has introductory articles to both Undefined behaviour and Sequence Points
Sep 13 '10 #6

Dheeraj Joshi
Expert 100+
P: 1,123
Hi thehimanshu,

Does not C text book explains this undefined behavior?
Banfa is right.

Dheeraj Joshi
Sep 13 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.