简体   繁体   中英

why do I need type casting when printf negative interger value

The following code prints a very large integer value, not -1.

int64_t num = -1
printf("%lld",num);

I need to type casting to print -1.

printf("%lld",(int64_t) num);

Does any one know why?? Is it standard? or undefined behavior that can vary depending on the system platform?

I use x86 intel processor and intel icc compiler.

I believe your code is not strictly well-defined.

In order to output a int64_t type (which, if your compiler support it, must be a 2's complement 64 bit signed type), you need to first write

#include <cinttypes>

Then use PRId64 as the format specfier:

printf("%" PRId64, num);

If you have the comparitive luxury of C++, you can use the considerably simpler std::cout which will have an appropriate overload for int64_t , assuming your platform implements that type.

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