1. Home
  2. Computer Science MCQs
  3. C Programming
  4. In C, what does sizeof('a') evaluate to on a platform…

Added 30 Nov -0001

In C, what does sizeof('a') evaluate to on a platform where int is 4 bytes?

  1. It is a compile-time error in every C compiler
  2. 4, because character constants have type int Correct answer
  3. 1, because a character occupies one byte
  4. 2, because the value fits in a short

Correct answer

B. 4, because character constants have type int

13 views

MCQSEXAM

Explanation

In C a character constant such as 'a' has type int, not char, so sizeof('a') equals sizeof(int), typically 4. This surprises programmers coming from C++, where 'a' has type char and the same expression gives 1. The distinction matters when a character constant is passed to variadic functions or used in overload resolution.

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