LearnFrenzy
Sign in Take a free test

Strings · Predict the Output · Question 11

sizeof('A') in C – output (C vs C++) Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    printf("%zu %zu\n", sizeof 'A', sizeof ((char) 'A'));
    return 0;
}
  • A 1 1
  • B 4 1 Correct
  • C 4 4
  • D 1 4
Correct Answer: Option B

In C a character constant such as 'A' has type int, so its size is 4. Cast to char, it is 1. (In C++, 'A' is a char and its size is 1 – a favourite interview trap.)

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