size_t n = 10; printf("%d", n); is compiled on LP64. What is the problem?
Correct answer
B. The format expects int while size_t is an 8-byte unsigned type, so behaviour is undefined; %zu is correct
16 views
B. The format expects int while size_t is an 8-byte unsigned type, so behaviour is undefined; %zu is correct
16 views
Format specifier mismatch is undefined behaviour because default argument promotions do not convert to the format's type, and reading a 4-byte int from an 8-byte argument produces garbage or a crash. The right specifier is %zu for size_t, or %llu with a cast for unsigned long long. Compiling with -Wformat catches this without running the program.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is B? Ask here and get an explanation.