LearnFrenzy
Sign in Take a free test

Error Handling · Find the Error · Question 3

Error: static_assert assumes a 64-bit int Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

Why does this program fail to compile on a typical 64-bit system?

main.c
#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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links