2

コード:

int main()
{
    unsigned int a = 0xfffffff7;
    char *b = (char *)&a;
    printf("%08x",*b);
}

出力は次のとおりfffffff7です。私のマシンはリトルエンディアンです。もちろん*bequals0xf7は知っていますが、なぜ の出力がprintf()このようになるのかはわかりません。

4

2 に答える 2

4

Since your system is small-endian, a is stored in memory as F7 FF FF FF.

b points to the first byte of a. (F7)

*b evaluates to a char. (F7)

*b is promoted to an int in order to pass it as a parameter, since it's of type char (which usually defaults to signed char) it is sign-extended to become FFFFFFF7.

于 2013-08-18T14:03:25.203 に答える