The printf
format string is wrong: %d
is used to print integers and you are giving it a pointer, so the program has undefined behavior. So as written, none of the 4 options are correct (although all are possible except c
which is meaningless). If the printf
format is fixed to use %p
, then the question is what is a
, can its value be accessed, and what does the standard say will be the result.
a
points to an object that's reached the end of its lifetime, so its value is indeterminate. An indeterminate value may be an unspecified value or a trap representation. In the former case some pointer value will be output, and in the latter case it's undefined behavior as before.
See the C standard (here I'm quoting from the C99 standard):
- §7.9.6.1.9 (on
fprintf
, but it also applies to printf
): "If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined."
- §6.2.4.2 "The value of a pointer becomes indeterminate when the object it points to reaches the end of its lifetime."
- §3.17.2.1 "indeterminate value: either an unspecified value or a trap representation"
So, the least wrong answer to the question is d
, but you have to fix the code, and then assume that your compiler produces code that never generates trap representations, and understand "garbage" to mean "unspecified".