sscanf打印格式问题 - %hhd [英] sscanf print format problem - %hhd

查看:351
本文介绍了sscanf打印格式问题 - %hhd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将无符号的8位int传递给sscanf。如果我尝试格式说明符%c(应该适合),我会在compliation期间遇到复杂的错误。但是,如果我使用%hhd而不是我没有收到任何错误。不管是出于什么原因,我的小快速修复是个坏主意吗?


谢谢!!

I am passing an unsigned 8-bit int into sscanf. If I try format specifier %c (which should fit) I get complicated errors during compliation. However, if I use %hhd instead I don''t get any errors. Is my little quick fix a bad idea for any reason?

thanks!!

推荐答案

如果格式说明符 %HHD"碰巧工作抛出你的C实现

,因为它非常不合规。


亲切的问候,


Jos
If the format specifier "%hhd" happens to work throw your C implementation away
because it''s extremely non-compliant then.

kind regards,

Jos


出于兴趣,使用%c时会出现什么错误?
Out of interest, what are the errors given when you use %c?


我可以看到三种情况,
  1. 您正在扫描字符串。您在格式说明符中使用%c并提供一个short int。

    然后将字符静默地强制转换为short int。您提供的short int将按预期分配字符的ASCII值。

  2. 您正在扫描字符串中的非数字字符。你在格式说明符中使用%hhd并提供一个短整数。

    可能%hhd被解释为%hd,这意味着短整数(因为h是短修饰符,大概是虽然这是不好的做法,但忽略了多个修饰符)。在这种情况下,scanf将找不到任何可以在提供的字符串中解释为short int的数字字符。没有任何东西会分配给short int,它将保留以前的值。

  3. 您正在扫描字符串中的数字字符。您在格式说明符中使用%hhd并提供一个短整数。

    假设%hhd表示%hd,则scanf会将该数字分配给short int。



在任何情况下,如果有错误,不应该直接因为scanf()。


In any case, if there are errors it shouldn''t be directly because of scanf().


这篇关于sscanf打印格式问题 - %hhd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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