gpt4 book ai didi

c - 将太多参数传递给 printf

转载 作者:太空狗 更新时间:2023-10-29 16:22:32 24 4
gpt4 key购买 nike

任何工作了一个多星期的 C 程序员都遇到过因使用比实际参数更多的格式说明符调用 printf 而导致的崩溃,例如:

printf("Gonna %s and %s, %s!", "crash", "burn");

但是,当您向 printf 传递太多参数时,是否会发生任何类似的坏事?

printf("Gonna %s and %s!", "crash", "burn", "dude");

我对 x86/x64 汇编的了解使我相信这是无害的,尽管我不相信我没有遗漏一些边缘条件,而且我对其他架构一无所知。这种情况是否保证无害,还是这里也存在潜在的引发崩溃的陷阱?

最佳答案

Online C Draft Standard (n1256) , 第 7.19.6.1 节,第 2 段:

The fprintf function writes output to the stream pointed to by stream, under control of the string pointed to by format that specifies how subsequent arguments areconverted for output. If there are insufficient arguments for the format, the behavior isundefined. If the format is exhausted while arguments remain, the excess arguments areevaluated (as always) but are otherwise ignored. The fprintf function returns whenthe end of the format string is encountered.

所有其他 *printf() 函数的行为与多余的参数相同,除了 vprintf()(很明显)。

关于c - 将太多参数传递给 printf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3578970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com