In C, what does sizeof('a') evaluate to on a platform where int is 4 bytes?
Correct answer
B. 4, because character constants have type int
13 views
B. 4, because character constants have type int
13 views
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.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is B? Ask here and get an explanation.