1. Home
  2. Computer Science MCQs
  3. C Programming
  4. In C, what are sizeof(c) and sizeof('a') for the…

Added 30 Nov -0001

In C, what are sizeof(c) and sizeof('a') for the declaration char c = 'a'; ?

  1. sizeof(c) is 4 and sizeof('a') is 1, because the constant is narrower than the variable
  2. sizeof(c) is 1 and sizeof('a') is 4, because a character constant has type int in C Correct answer
  3. Both are 1, since a character literal always has type char
  4. Both are 4, since integer promotion also applies to the variable

Correct answer

B. sizeof(c) is 1 and sizeof('a') is 4, because a character constant has type int in C

16 views

MCQSEXAM

Explanation

Character constants in C have type int, not char, which is why sizeof('a') equals sizeof(int), normally 4. The variable c was declared char, so sizeof(c) is 1; the constant is narrowed only at initialisation. C++ changes this rule and gives character literals type char, so the same expression evaluates to 1 there, a favourite cross-language trap.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs