다음 프로그램이 있습니다
#include <stdio.h>
int main(void)
{
unsigned short int length = 10;
printf("Enter length : ");
scanf("%u", &length);
printf("value is %u \n", length);
return 0;
}
사용하여 컴파일 gcc filename.c하면 다음 경고가 발생합니다 ( scanf()줄에서).
warning: format ‘%u’ expects argument of type ‘unsigned int *’, but argument 2 has type ‘short unsigned int *’ [-Wformat]
나는 다음을 참조 C99 specification - 7.19.6 Formatted input/output functions길이 수정 (같이 사용할 때 올바른 형식 지정자를 이해할 수 없었다 short, long등)과을unsignedint 데이터 유형에 대해 .
이다 %u 올바른 지정은 unsigned short int? 그렇다면 위에서 언급 한 경고가 표시되는 이유는 무엇입니까?!
편집 : 대부분의 시간 동안 노력 %uh하고 있었고 여전히 경고를하고있었습니다.
printf("%u\n", (unsigned int)length); //당신이 읽은 C99 사양은 항상 작동합니다sizeof(short) <= sizeof(int)(그러나 아래 질문에 대한 실제 답변은 물론 훨씬 좋습니다)