LearnFrenzy
Sign in Take a free test

Operators and Expressions · Predict the Output · Question 9

sizeof c, sizeof(c + 1) and sizeof(c = 'B') Answer, explanation & discussion

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

What is the output of the following C program?

main.c
#include <stdio.h>

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

c + 1 is promoted to int (4 bytes). The assignment expression has the type of c (1 byte) – and since sizeof never evaluates its operand, c is still 'A'.

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