OA. free
Free
MathWorks Core Computer Science Core Computer Science Medium

Given that 1 char = 1 byte, 1 int = 2 bytes and 1 float = 4 bytes, What is the output of...

MathWorks technical mcq question, verified with a worked answer. Free to practise - no sign-up.

Given that 1 char = 1 byte, 1 int = 2 bytes and 1 float = 4 bytes, What is the output of the following code?

#include <stdio.h>
int main(void)
{
    char *a = (char *) malloc(12);
    printf("%d ", sizeof(a));
    printf("%d\n", sizeof(*a));
    return 0;
}

Pick ONE option

Choose one option.
Show answer & explanation
Answer: A. 4 1

sizeof(a) returns the size of the pointer itself, not the allocated memory. On a typical 32-bit system, a pointer is 4 bytes. sizeof(*a) dereferences the pointer to get the type it points to (char), which is 1 byte. The malloc size (12 bytes) is irrelevant to sizeof operations.

Step-by-step Derivation:
Step-by-step execution:

  1. char *a = (char *) malloc(12); — declares a pointer to char and allocates 12 bytes
  2. sizeof(a) — returns the size of the pointer variable itself. Pointers are typically 4 bytes on 32-bit systems (or 8 bytes on 64-bit systems; this question assumes 4 bytes)
  3. sizeof(*a) — dereferences a to get the underlying type (char), which is 1 byte
  4. Output: 4 1

Key insight: sizeof() is a compile-time operator that determines the size of types and variables. It does NOT look at runtime values like malloc size. The pointer size is fixed by the architecture, regardless of how much memory was allocated.