简单问题:scanf 能否将“小整数”读取/接受到 ANSI C 中的无符号字符中?
示例代码un_char.c:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
unsigned char character;
scanf("%hhu", &character);
return EXIT_SUCCESS;
}
编译为:
$ gcc -Wall -ansi -pedantic -o un_char un_char.c
un_char.c: In function ‘main’:
un_char.c:8: warning: ISO C90 does not support the ‘hh’ gnu_scanf length modifier
ISO C90 不支持 hh
。那么在这种情况下可以使用什么scanf转换呢?
最佳答案
否:C89 (C90) 不支持 '%hhu'
将数字字符串读取到无符号字符中。这是 C99 中的一个功能。
您必须读取无符号整数 ('%u'
) 或无符号短整型 ('%hu'
),然后检查结果是否与无符号字符的范围。
关于c - ANSI C(ISO C90): Can scanf read/accept an unsigned char?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41050094/