简体   繁体   中英

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

You are relying on undefined behavior by calling printf with too few arguments. 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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