究竟什么是“无效的转换规范"? [英] What is exactly an "invalid conversion specification"?

查看:172
本文介绍了究竟什么是“无效的转换规范"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据C11,第§7.21.6.1,P9

As per C11, chapter §7.21.6.1, P9

如果转换规范无效,则行为未定义. 282)如果有任何自变量 不是对应转换规范的正确类型,其行为是 未定义.

If a conversion specification is invalid, the behavior is undefined.282) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

直到时间,我的理解是,

Till time, my understanding was, for

  char str [] = "Sourav";

  • printf("%S", str);这样的语句属于第一句话,%S不存在CS(大写)
  • 类似printf("%d", str);的语句属于第二句话(CS和参数类型之间不匹配,但是%d仍然不是不是一个无效的CS)
    • A statement like printf("%S", str); belong to the first sentence, there exist no CS as %S (UPPERCASE)
    • A statement like printf("%d", str); belongs to the second sentence (mismatch between CS and argument type, but the %d is not an "invalid" CS, anyway)
    • 除非最近的评论线程另有建议.

      我的理解错误吗?可以将第二条语句归类为无效"(PS-不是错误")转换说明符吗?

      Is my understanding wrong? Can the second statement also be categorized as "invalid" (PS- not "wrong") conversion specifier?

      更新:答案和评论主题已删除,这很简单适用于< 10,000用户.

      Update: The answer and the comment thread is deleted, here's a snap for <10K users.

      推荐答案

      转换规范的有效性"由您引用的上方的标准段落确定:

      The "validity" of a conversion specification is determined by the standard paragraphs above the one you quoted:

      7.21.6.1-从p4到p8

      每个转换规范都由字符%引入. %之后,依次显示以下内容:...

      Each conversion specification is introduced by the character %. After the %, the following appear in sequence: ...

      标志字符及其含义是:...

      The flag characters and their meanings are: ...

      转换说明符及其含义是:...

      The conversion specifiers and their meanings are: ...

      这里的意思是,任何由上述列表中的元素组成的转换规范都是有效的,而所有其他转换规范都不在该标准的范围之内.这就是为什么代码中的段落提到了UB的两个原因.一个是不符合语法的规范,另一个是规范和类型不匹配.

      This here means that any conversion specification that is composed from the elements in the above lists is valid, all others are not in the eyes of the standard. That's why the paragraph in your code mentions two causes of UB. One is a specification that is not according to the grammar, and the other is specification and type mismatch.

      您链接到的评论似乎在口语上使用了无效". IE.转换规范的两种用法都是无效的",因为它们会导致UB.但是从语言律师的角度来看,只有第一个是无效的".

      The comment you linked to seems to use "invalid" colloquially. I.e. both uses of the conversion specifications are "invalid", since they lead to UB. But only the first is "invalid" from a language lawyer standpoint.

      这篇关于究竟什么是“无效的转换规范"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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