Question 3
Back to Exercise
Why does this program fail to compile on a typical 64-bit system?
#include <assert.h>
#include <stdio.h>
static_assert(sizeof(int) == 8, "this code needs a 64-bit int");
int main(void) {
printf("%zu\n", sizeof(int));
return 0;
}
- A static_assert cannot be at file scope
- B sizeof cannot be used in static_assert
- C There is no bug
- D int is 4 bytes, so the static_assert condition is false and compilation stops with the message Correct
Correct Answer: Option D
That is static_assert doing its job: catching a wrong assumption at build time. If the code really needs 64 bits, use int64_t from <stdint.h>.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!