dimanche 28 juin 2015

Strange Endianness Behaviour in C [duplicate]

This question already has an answer here:

Here's the C code:

/*Creates an integer of size 4 bytes (on my computer)
  and the value of each byte is equal to the ascii
  values of the characters 'A', 'B', 'C', 'D'*/

int num = ('A' << 24) | ('B' << 16) | ('C' << 8) | 'D';
char *pNum = &num;
printf("%c %c %c %c\n", pNum[0], pNum[1], pNum[2], pNum[3]);
printf("%c %c %c %c", *pNum, *pNum++, *pNum++, *pNum++);

The output is:

D C B A

A B C D

Why is there a difference in the output?

Aucun commentaire:

Enregistrer un commentaire