页首Html代码

返回顶部

【没有注意过的细节】用scanf读一个unsigned char? %hhu 的用法

头段时间我的代码,有一个 unsigned char,我需要从sscanf 中读取字符串为这个值。但是一般char 是用%c的,我是要值得。

所以我使用了%d %u来读,结果报警告: 

unsigned char a;

sscanf("15","%u",&a);

warning: format ‘%u’ expects type ‘unsigned int*’, but argument 3 has type ‘unsigned char*’

警告挺烦人的,查了下,才发现自己没有注意过的细节:

I'm trying to use this code to read values between 0 to 255 (unsigned char).

#include<stdio.h>
int main(void)
{
    unsigned char value;

    /* To read the numbers between 0 to 255 */
    printf("Please enter a number between 0 and 255 \n");
    scanf("%u",&value);
    printf("The value is %u \n",value);

    return 0;
}
I do get the following compiler warning as expected.

warning: format ‘%u’ expects type ‘unsigned int *’, but argument 2 has type ‘unsigned char *’

And this is my output for this program.

Please enter a number between 0 and 255 45 The value is 45 Segmentation fault

I do get the segmentation fault while running this code.

What is the best way to read unsigned char values using scanf?

c
share|edit
asked Jan 3 at 20:09
 
user1293997
766
     
use %c to read a byte – TJD Jan 3 at 20:12
4          
Actually %hhu for unsigned char. – Joe Jan 3 at 20:15
     
@TJD. I don't want to read a char. I would like to read values between 0 to 255. – user1293997 Jan 3 at 20:15
     
@Joe. That worked great. Thanks a lot. – user1293997 Jan 3 at 20:17
     
user1293997: you might want to write an answer and accept it (assuming @Joe is not interested in doing so). The only currently standing answer is quite incorrect. – user4815162342 Jan 3 at 20:34
%hhu   指定signed char 或 unsigned char     char是1个字节

%h 指定short int 或者unsigned short int short 是2个字节
C中格式字符串printf()的一般形式为: %[标志][输出最小宽度][.精度][长度]类型, 其中方括号[]中的项为可选项。各项的意义介绍如下:
1.类型:

表示输出类型的格式字符       格式字符意义
a                                             浮点数、十六进制数字和p-计数法(C99)
A                                           浮点数、十六进制数字和p-计数法(C99)
c                  输出单个字符
d                  以十进制形式输出带符号整数(正数不输出符号)
e                  以指数形式输出单、双精度实数
E                  以指数形式输出单、双精度实数
f                  以小数形式输出单、双精度实数
g                以%f%e中较短的输出宽度输出单、双精度实数,%e格式在指数小于-4或者大   于等于精度时使用
G                以%f%e中较短的输出宽度输出单、双精度实数,%e格式在指数小于-4或者大于等于精度时使用
i                                            有符号十进制整数(与%d相同)
o                以八进制形式输出无符号整数(不输出前缀O)
p                                          指针
s                 输出字符串
x                 以十六进制形式输出无符号整数(不输出前缀OX)
X                以十六进制形式输出无符号整数(不输出前缀OX)
u                 以十进制形式输出无符号整数
2.标志
标志字符为-、+、#、空格和0五种,其意义下表所示:

标志格式字符                     标 志 意 义
-                                 结果左对齐,右边填空格
+                                输出符号(正号或负号)
空格                                                    输出值为正时冠以空格,为负时冠以负号
#                               对c,s,d,u类无影响;对o类,在输出时加前缀0;对x类, 在输出时加前缀0x或者0X;
                                      对g,G 类防止尾随0被删除;对于所有的浮点形式,#保证了即使不跟任何数字,也打印一个小数点字符
0                                          对于所有的数字格式,用前导0填充字段宽度,若出现-标志或者指定了精度(对于整数),忽略
3.输出最小宽度
用十进制整数来表示输出的最少位数。若实际位数多于定义的宽度,则按实际位数输出,若实际位数少于定义的宽度则补以空格或0。
4.精度
精度格式符以“.”开头,后跟十进制整数。本项的意义是:如果输出数字,则表示小数的位数;如果输出的是字符,则表示输出字符的个数;若实际位数大于所定义的精度数,则截去超过的部分。
5.长度
长度格式符为h,l两种,h表示按短整型量输出,l表示按长整型量输出。
     h和整数转换说明符一起使用,表示一个short   int 或者unsigned short int类型的数值 ,示例:
%hu,%hx,%6.4hd
     hh和整数转换说明符一起使用,表示一个short   int 或者unsigned short类型的数值 ,示例:
%hhu,%hhx,%6.4hhd
     j和整数转换说明符一起使用,表示一个intmax_t或者uintmax_t类型的数值 ,示例:
%jd,%8jx
     l和整数转换说明符一起使用,表示一个long int 或者unsigned long int类型的数值 ,示例:
%ld,%8lu
     ll和整数转换说明符一起使用,表示一个long int 或者unsigned long int类型的数值 (C99),示例:
%lld,%8llu
     L和浮点转换说明符一起使用,表示一个long double的值,示例:%Lf,%10.4Le
     t和整数转换说明符一起使用,表示一个ptrdiff_t值(两个指针之间的差相对应的类型)(C99),示例:
%td,%12ti
     z和整数转换说明符一起使用,表示一个size_t值(sizeof返回的类型)(C99),示例:%zd,%12zx

使用printf函数时还要注意一个问题,那就是输出表列中的求值顺序。不同的编译系统不一定相同,可以从左到右,也可从右到左。Turbo C是按从右到左进行的。

http://www.cppblog.com/zenliang/archive/2010/11/07/132858.html

http://m.blog.csdn.net/blog/kangear/8952033

http://www.cnblogs.com/ilegend/archive/2011/11/22/2258701.html

 

顺道 道一句:%uud 也是可以得,是有符号的

 

http://pubs.opengroup.org/onlinepubs/009695399/functions/scanf.html

http://connect.microsoft.com/VisualStudio/feedback/details/416843/sscanf-cannot-not-handle-hhd-format

 

 

posted @ 2013-12-16 18:36  ayanmw  阅读(13935)  评论(0编辑  收藏  举报

页脚Html代码