Checking Endianness of RISC-V machine using C-code

不想你离开。 提交于 2019-12-13 03:46:44

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!