Or in other words: Can an incorrect printf / fprintf string of a decimal integer ( %d , %u , %ld , %lld ) lead to a program crash or undefined behavior?
Cosine following lines of code:
#include <iostream> #include <cstdio> int main() { std::cout << sizeof(int) << std::endl << sizeof(long) << std::endl; long a = 10; long b = 20; std::printf("%d, %d\n", a, b); return 0; }
The result for 32-bit architecture:
4 4 10, 20
Result in architecture with 64-bit architecture:
4 8 10, 20
In any case, the program prints the expected result. I know if the long value exceeds the range of int , the program prints the wrong numbers - which is ugly, but does not affect the main purpose of the program - but can anything unexpected happen besides this?
source share