#include<stdio.h>
int main (void)
{
int i=257;
int *ptr=&i;
printf("%d%d",*((char*)ptr),*((char*)ptr+1));
return 0;
}
Will the output of the above code implementation defined and the output will vary between little endian and big endian machine?
Yes, it will be implementation defined behaviour.
When you cast
ptrwith(char *)and then access the first memory location, the first byte stored in memory will be accessed, which will depend on if system is a big-endian or a little-endian system.Before calling the
printffunction the arguments are evaluated. And this evaluation as said above will get different values for different systems. Therefore it is implementation defined.