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 behavior — The pointer p is declared but never initialized with the address of a valid integer. The statement printf("%d", *p) dereferences p. Dereferencing an…
- A.
Undefined behavior
- B.
3
- C.
Runtime error
- D.
More than one of the above
- 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.