Skip to content

Instantly share code, notes, and snippets.

@paraboul
Created May 27, 2011 15:20
Show Gist options
  • Save paraboul/995466 to your computer and use it in GitHub Desktop.
Save paraboul/995466 to your computer and use it in GitHub Desktop.
C VS PHP
uint x[3] = {0x42, 0x69, 0x13};
int i = 0;
printf("Res : %x\n", x[i++] << 16 | x[i++] << 8 | x[i++]);
/* Output 0x424242 */
int i = 1;
printf("Res : %d\n", i++ + i++);
Result with llvm clang and gcc :
para@Bohr:~/dev/libmpqsc2/$ clang x.c
para@Bohr:~/dev/libmpqsc2/$ ./a.out
Res : 3
para@Bohr:~/dev/libmpqsc2/$ gcc x.c
para@Bohr:~/dev/libmpqsc2/$ ./a.out
Res : 2
<?php
$x = array(0x42, 0x69, 0x13);
$i = 0;
printf("Res : %x\n", $x[$i++] << 16 | $x[$i++] << 8 | $x[$i++]);
/* Output 0x426913 */
?>
@paraboul
Copy link
Author

Different "++" execution order

@H-Max
Copy link

H-Max commented May 27, 2011

Different | versus << / >> operators precedence maybe ?

@paraboul
Copy link
Author

http://c-faq.com/expr/seqpoints.html

Since there's no good way to define it, the Standard declares that it is undefined, and that portable programs simply must not use such constructs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment