问题描述
我注意到,有大量的C编译器发出警告时,在的的printf / sprintf函数,格式字符串中的转换说明不匹配的类型或相应参数的数量。
I have noticed that a large number of C compilers issue warnings when the conversion specifiers in the format string of the printf/sprintf functions do not match the type or the count of the corresponding arguments.
在我看来,像一个概念性的突破,因为C没有根据语言规范有内置的功能。
That seems to me like a conceptual break since C doesn't have built-in functions according to the language specification.
全部编译器应该知道的printf / sprintf的是他们的原型,而不是它们的语义。我知道的printf / sprintf的是标准的C函数,但但他们驻留在独立的库libc,你必须包括stdio.h中导入他们的原型。
All the compiler should know about printf/sprintf is their prototypes and not their semantics. I know that printf/sprintf are standard C functions, but yet they reside in a separate library, libc, and you have to include stdio.h to import their prototypes.
然而,很多编译器做的却是分析这可能和在运行时提供的格式字符串。
What many compilers do instead is analyze the format string which could as well be supplied at runtime.
请问上述有意义吗?
推荐答案
所有的编译器应该知道的printf / sprintf的是他们的原型,而不是它们的语义。
"All the compiler should know about printf/sprintf is their prototypes and not their semantics".
这是不正确的部分。至于标准而言,C实现的任何部分被允许知道任何其他部分,并出具诊断可能有帮助给用户。编译器内在函数不是标准要求,而且这也不是特定的诊断,但他们肯定不禁止的。
That's the part that isn't true. As far as the standard is concerned, any part of a C implementation is "allowed" to know about any other part, and to issue diagnostics that may be helpful to the user. Compiler intrinsics aren't required by the standard, and neither is this particular diagnostic, but they certainly aren't forbidden.
注意(只要标准而言)的标准库是特殊的,它不只是任何旧的链接库。如果特定的实现/编译器甚至为用户提供对抗不同版本的标准库的链接的机制,标准肯定不会要求其工作时,替代库具有不同于什么是在布局语义标准的。
Note that (as far as the standard is concerned) the standard library is special, it's not just any old linked library. If a particular implementation/compiler even provides a mechanism for the user to link against a different version of the standard library, the standard certainly doesn't require it to "work" when that alternative library has different semantics from what is laid out in the standard.
因此,在这个意义上,一切都在标准库是bult项。这是C语言规范的一部分。编译器作用于它的行为与标准要求的假设。
So in that sense, everything in the standard library is "bult-ins". It's part of the C language specification. Compilers are allowed to act on the assumption that it behaves as the standard requires.
当然,如果格式说明是不知道,直到运行时,编译器不能做的可变参数的静态检查。但是,当它在编译时是已知的,编译器可以假定中的printf
的行为一样有效,因为它可以假定的memcpy $的行为C $ C>,或整数加法的。
Of course, if the format specifier isn't known until runtime, then the compiler can't do a static check of the varargs. But when it is known at compile time, the compiler can assume the behaviour of printf
just as validly as it can assume the behaviour of memcpy
, or of integer addition.
这篇关于是的printf / sprintf的编译器警告的概念突破?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!