ANSI C (ISO C90):scanf 可以读取/接受无符号字符吗?
简单的问题: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
hh
不受 ISO C90 支持。那么在这种情况下可以使用什么scanf转换呢?
Simple question: Can scanf read/accept a "small integer" into an unsigned char in ANSI C?
example code un_char.c:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
unsigned char character;
scanf("%hhu", &character);
return EXIT_SUCCESS;
}
Compiled as:
$ 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
hh
isn't supported by ISO C90. So what scanf conversion can be used in this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
否:C89 (C90) 不支持
'%hhu'
将数字字符串读入 unsigned char。这是C99 中的一个功能。您必须读入无符号整数 (
'%u'
) 或无符号短整型 ('%hu'
),然后检查结果是否在无符号字符。No: C89 (C90) does not support
'%hhu'
to read a string of digits into an unsigned char. That is a feature in C99.You would have to read into an unsigned integer (
'%u'
) or unsigned short ('%hu'
) and then check that the result is with the range of an unsigned char.将其读入无符号短整型/整数,并在需要时进行一些范围检查。
Read it into an unsigned short/int and do some range checking after if you need to.