저는 C의 초보자입니다. 왜 이렇게 이상한 행동을하는지 궁금합니다.C가 16 진수 값을 잘못 인쇄하는 이유는 무엇입니까?
한 번에 16 비트 파일을 읽으며 다음과 같이 인쇄합니다.
#include <stdio.h>
#define endian(hex) (((hex & 0x00ff) << 8) + ((hex & 0xff00) >> 8))
int main(int argc, char *argv[])
{
const int SIZE = 2;
const int NMEMB = 1;
FILE *ifp; //input file pointe
FILE *ofp; // output file pointer
int i;
short hex;
for (i = 2; i < argc; i++)
{
// Reads the header and stores the bits
ifp = fopen(argv[i], "r");
if (!ifp) return 1;
while (fread(&hex, SIZE, NMEMB, ifp))
{
printf("\n%x", hex);
printf("\n%x", endian(hex)); // this prints what I expect
printf("\n%x", hex);
hex = endian(hex);
printf("\n%x", hex);
}
}
}
결과는 다음과 같이 보일 :
ffffdeca
cade // expected
ffffdeca
ffffcade
0
0 // expected
0
0
600
6 // expected
600
6
각 블록의 마지막 라인이 초와 같은 값을 인쇄하지 않는 이유 누군가가 나에게 설명 할 수 있습니까?