简体   繁体   English

允许%n $操作数用于自定义printf

[英]Allowing %n$ operand for custom printf

I have a custom implementation of printf which I use in my school projects. 我在学校项目中使用了printf的自定义实现。 Wishing to have the same warnings that printf, I use __attribute__((format (printf ...)) . That works fine, but using -Wall -Wextra -Werror -std=c11 -pedantic-errors , I get the error ISO C does not support %n$ operand number formats on gcc 7.2.0. (My implementation support that format). On clang it does not shout a warning if I use std=c11 , but does with std=c99 . 希望有与printf相同的警告,我使用__attribute__((format (printf ...)) 。可以正常工作,但是使用-Wall -Wextra -Werror -std=c11 -pedantic-errors ,我得到错误ISO C does not support %n$ operand number formats在gcc 7.2.0上ISO C does not support %n$ operand number formats (我的实现支持该格式)。在clang上,如果我使用std=c11不会发出警告,但对std=c99发出警告。

Is that format part of the c11 specification (as clang behavior let me think) or only the Singe Unix Specification (that is what my man page says) ? 该格式是c11规范的一部分(让我想一下是clang行为)还是仅Singe Unix规范(这就是我的手册页所说的)? If so, how can I use it ? 如果可以,该如何使用? In this page , I cannot find it as an option to std . 此页面中 ,我找不到它作为std的选项。

I would rather not disable any warnings, since they are quite useful and catches a lot. 我宁愿不禁用任何警告,因为它们非常有用并且广受欢迎。 But is there a way to allow that format in format strings, or specify only for my function ? 但是有没有办法允许格式字符串中的格式,或者只为我的函数指定?

Thanks a lot. 非常感谢。

PS: I use a custom implemenation of printf because in my school projects we must only use specific allowed functions (basically, system api like malloc , free , read etc), or the one we have done ourselves. PS:我使用printf的自定义实现,因为在我的学校项目中,我们必须仅使用特定的允许功能(基本上是系统api,如mallocfreeread等),或者我们自己完成的功能。

EDIT : for reference, the format %n$ , with n being string of digit for a non-zero number, allow to access the argument at that index (starting at one) in the list of variable arguments given to printf. 编辑:作为参考,格式为%n$ ,其中n为非零数字的数字字符串,允许访问给定printf的变量参数列表中该索引处的参数(从1开始)。 (And can be used for the conversion itself or for the precision or field widht with the * operand). (并且可用于转换本身,或用于带有*操作数的精度或字段宽度)。

您可以在属性中使用gnu_printf而不是printf来模拟更接近POSIX的printf的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM