In C, what are sizeof(c) and sizeof('a') for the declaration char c = 'a'; ?
Correct answer
B. sizeof(c) is 1 and sizeof('a') is 4, because a character constant has type int in C
16 views
B. sizeof(c) is 1 and sizeof('a') is 4, because a character constant has type int in C
16 views
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.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is B? Ask here and get an explanation.