问题
Can someone please help me out with this. There is a C-code which most of you are familiar with, it checks the endian-ness of a machine.
What would be the result if it runs on a RISC-V machine?
Code is mentioned as below:
#include <cstdio>
int main()
{
int x = 1;
char* p = (char*)&x;
printf("%d\n",(int)*p);
return 0;
}
回答1:
The program is valid regardless of the platform.
The output is 1
for a little-endian computer or a computer where sizeof (int)
== sizeof (char)
. It will be 0
for all other platforms.
Since RISC-V is little-endian then the output should be 1
.
来源:https://stackoverflow.com/questions/54677118/checking-endianness-of-risc-v-machine-using-c-code