scanf函数(QUOT;%D&QUOT ;,的char *) - 炭作为-INT格式字符串? [英] scanf("%d", char*) - char-as-int format string?

查看:220
本文介绍了scanf函数(QUOT;%D&QUOT ;,的char *) - 炭作为-INT格式字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是炭作为号码的格式字符串修改?

What is the format string modifier for char-as-number?

我想在一些从未使用sscanf的超过255(实际上要少得多)成一个无符号字符类型变量来读取。

I want to read in a number never exceeding 255 (actually much less) into an unsigned char type variable using sscanf.

使用典型

 char source[] = "x32";
 char separator;
 unsigned char dest;
 int len;

 len = sscanf(source,"%c%d",&separator,&dest);
 // validate and proceed...

我得到预期的警告:sscanf的参数的4类型char *,为int *预期

I'm getting the expected warning: argument 4 of sscanf is type char*, int* expected.

据我了解的规格,没有修饰符字符(如%SD短,或%LLD适用于64位长)

As I understand the specs, there is no modifier for char (like %sd for short, or %lld for 64-bit long)


  • 是危险的? (会溢出只是溢出(翻转)变量或将其写入分配的空间之外?)

  • 有一个prettier的方式来实现这一比分配一个临时的int变量?

  • ...或者你干脆提出一个完全不同的方法?

推荐答案

下的glibc的的 scanf()的 ,MSVC似乎并不直接支持整数存储到一个char(请参阅MSDN scanf函数宽度规格上支持转换的详细信息

You can use %hhd under glibc's scanf(), MSVC does not appear to support integer storage into a char directly (see MSDN scanf Width Specification for more information on the supported conversions)

这篇关于scanf函数(QUOT;%D&QUOT ;,的char *) - 炭作为-INT格式字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆