开发者

Output prediction

开发者 https://www.devze.com 2023-03-28 15:01 出处:网络
what shall be the output of: (and why?) printf(\"%d\",2.37); Apparently, printf is a variadic function and we can never 开发者_C百科know the type of a variable argument list. so we always have to s

what shall be the output of: (and why?)

printf("%d",2.37);

Apparently, printf is a variadic function and we can never 开发者_C百科know the type of a variable argument list. so we always have to specify the format specifiers manually. so, 2.37 would be stored as double according to IEEE standards would be fetched and printed in integer format. But the output is 0. What is the reason?


It is undefined behavior. You're passing a double argument to a function that expects to retrieve an int from its varargs macros, and there's no telling at all what that is going to lead to. In theory, it may even crash (with a calling convention that specifies that variadic arguments of different types are passed in different ways or on different stacks).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号