What is the behavior of the following C code fragment? int a[ ] = {1, 2, 3},…

2024

What is the behavior of the following C code fragment?

int a[ ] = {1, 2, 3}, *p;
printf("%d", *p);

Answer: A. Undefined behaviorThe pointer p is declared but never initialized with the address of a valid integer. The statement printf("%d", *p) dereferences p. Dereferencing an…

  1. A.

    Undefined behavior

  2. B.

    3

  3. C.

    Runtime error

  4. D.

    More than one of the above

  5. E.

    None of the above

Attempted by 1193 students.

Show answer & explanation

Correct answer: A

The pointer p is declared but never initialized with the address of a valid integer. The statement printf("%d", *p) dereferences p. Dereferencing an uninitialized pointer in C causes undefined behavior. The program may print an arbitrary value, crash, or appear to work on a particular system, but no output is guaranteed by the C standard. Therefore, the correct answer is undefined behavior.

Explore the full course: Gate Guidance By Sanchit Sir

Loading lesson…