What will be the output of the following C code snippet?
MathWorks technical mcq question, verified with a worked answer. Free to practise - no sign-up.
What will be the output of the following C code snippet?
int main(){
printf("%d\n", sizeof('P'));
}
Pick ONE option
Show answer & explanation
Answer: C. 4
In C, a character literal like 'P' is of type int, not char. The sizeof operator on an integer literal returns the size of an int, which is typically 4 bytes on most modern systems (32-bit or 64-bit). Therefore, the output is 4.
Step-by-step Derivation:
Step 1: Identify the expression: sizeof('P')
Step 2: Recognize that 'P' is a character literal (single quotes)
Step 3: In C, character literals have type int (unlike char variables)
Step 4: sizeof(int) on most systems = 4 bytes
Step 5: printf("%d\n", 4) outputs: 4