![](/img/trans.png)
[英]Passing too few arguments to printf for the format specifier — is it undefined behavior?
[英]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
我不知道為什么這段代碼在 cygwin 中正常工作,因為我不在 printf 中寫 D
您通過使用太少的參數調用printf
來依賴未定義的行為。 你永遠不應該這樣做,因為程序可能會崩潰,如果它確實運行了,結果將基於特定的編譯器/系統隨機。
這里發生的事情是printf
從堆棧中彈出的變量比它預期的要多,並且正在從main
從堆棧中抓取一些東西。 這幾乎總是會在您的程序稍后導致某種堆棧/內存損壞。 當然,您的代碼很短,因此在這種簡單的情況下不會發生這種情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.