简体   繁体   English

为什么参数太少的 printf 在 Cygwin 上工作?

[英]Why does printf with too few arguments work on Cygwin?

int main()
int a = 1 , b=2 ,c=3 ,d=4 ;
a= ++b;
c= d++;
print f("a = %d , b = %d , c = %d, d= %d" , a , b ,c);
return 0;}
a=3 b=3 c=4 d=5

I don't know why works this code nomally in cygwin because I dont write about D in printf我不知道为什么这段代码在 cygwin 中正常工作,因为我不在 printf 中写 D

You are relying on undefined behavior by calling printf with too few arguments.您通过使用太少的参数调用printf来依赖未定义的行为。 You should never do this as the program will likely crash, and if it does run, the results will be randomly based on the specific compiler/system.你永远不应该这样做,因为程序可能会崩溃,如果它确实运行了,结果将基于特定的编译器/系统随机。

What is happening here is that printf is popping more variables from the stack than it is supposed too and is grabbing something from the stack from main .这里发生的事情是printf从堆栈中弹出的变量比它预期的要多,并且正在从main从堆栈中抓取一些东西。 This is almost always going to cause some sort of stack/memory corruption later on in your program.这几乎总是会在您的程序稍后导致某种堆栈/内存损坏。 Of course your code is short and so this is not happening in this simple case.当然,您的代码很短,因此在这种简单的情况下不会发生这种情况。

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

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