ikbal's blog

By ikbal, 12 years ago, In English
#include <stdio.h>     
#include <string.h>     

int main(){
   
	int y = 7 , z = 8 ; 
	int *p;
	
	p = &y ; 
	
	*p = (++z)+(y++);
	
	printf("%d\n",*p);
   
	return 0;	
}

This program's output is 8 Anyone know how is this?

  • Vote: I like it
  • +2
  • Vote: I do not like it

| Write comment?
»
12 years ago, # |
Rev. 2   Vote: I like it +16 Vote: I do not like it

hahaha XD.

MS C++ outputs 17 and GNU C++ outputs 8 :)

»
12 years ago, # |
  Vote: I like it +15 Vote: I do not like it

I think that's undefined behaviour

  • »
    »
    12 years ago, # ^ |
    Rev. 2   Vote: I like it +6 Vote: I do not like it

    According to Kernighan & Ritchie's book The C Programming Language, 2nd Edition (ANSI C), by Prentice Hall, page 52:

    "C, like most languages, does not specify the order in which the operands of an operator are evaluated. (The exceptions are &&, ||, ?: and ','.)"

    Right on the next page (53) it continues with an example:

    "printf("%d %d\n", ++n, power(2, n)); /* WRONG */ ... The solution, of course, is to write ++n; printf("%d %d\n", n, power(2, n));"

    and at last, the expression "a[i] = i++;" is mentioned as a "one unhappy situation".

    • »
      »
      »
      12 years ago, # ^ |
      Rev. 2   Vote: I like it -8 Vote: I do not like it

      -

      • »
        »
        »
        »
        12 years ago, # ^ |
          Vote: I like it +10 Vote: I do not like it

        Wrong. The order of evaluation of the function arguments is not specified.

        The C99 standard, paragraph 6.5.2.3.10:

        The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call.
»
12 years ago, # |
  Vote: I like it +42 Vote: I do not like it

I just leave it here.

  • »
    »
    12 years ago, # ^ |
      Vote: I like it -10 Vote: I do not like it

    if 10=1+2+3+4 and 16=4+4+4+4 then where did "11" come from?

    • »
      »
      »
      12 years ago, # ^ |
        Vote: I like it +13 Vote: I do not like it

      If it helps, this is how compiler translate the code above:

         0x0000000000400e58 <+8>:	movl   $0x0,-0x4(%rbp)
         0x0000000000400e5f <+15>:	addl   $0x1,-0x4(%rbp)
         0x0000000000400e63 <+19>:	addl   $0x1,-0x4(%rbp)
         0x0000000000400e67 <+23>:	mov    -0x4(%rbp),%eax
         0x0000000000400e6a <+26>:	lea    (%rax,%rax,1),%edx
         0x0000000000400e6d <+29>:	addl   $0x1,-0x4(%rbp)
         0x0000000000400e71 <+33>:	mov    -0x4(%rbp),%eax
         0x0000000000400e74 <+36>:	add    %eax,%edx
         0x0000000000400e76 <+38>:	addl   $0x1,-0x4(%rbp)
         0x0000000000400e7a <+42>:	mov    -0x4(%rbp),%eax
         0x0000000000400e7d <+45>:	add    %edx,%eax
         0x0000000000400e7f <+47>:	mov    %eax,-0x8(%rbp)
         0x0000000000400e82 <+50>:	mov    -0x8(%rbp),%eax
      

      11 in %eax.

    • »
      »
      »
      12 years ago, # ^ |
      Rev. 2   Vote: I like it +9 Vote: I do not like it

      1.step i = 1 then z = i + ++i + ++i + ++i

      2.step i = 2 then z = 2 + 2 + ++i + ++i

      3.step i = 3 then z = 4 + 3 + ++i ;

      4.step i = 4 then z = 7 + 4

      final i = 4 , z = 11

»
12 years ago, # |
  Vote: I like it 0 Vote: I do not like it

*p=y=7; (y++)-->(y=y+1); *p=y=8;