What is the output of the following C program? #include <stdio.h> int main() {…
2024
What is the output of the following C program?
#include <stdio.h>
int main()
{
typedef int *i;
int j = 10;
i a = &j;
printf("%d", *a);
getchar();
return 0;
}Answer: A. 10 — Concept: A typedef does not create a new type — it creates a new NAME (an alias) for an existing type. typedef int *i; makes the identifier i an alternate…
- A.
10
- B.
0
- C.
1
- D.
error
Attempted by 289 students.
Show answer & explanation
Correct answer: A
Concept: A typedef does not create a new type — it creates a new NAME (an alias) for an existing type. typedef int *i; makes the identifier i an alternate spelling for the type “pointer to int”. Once declared, i can be used in a variable declaration exactly where the keyword sequence int * would be used; dereferencing any valid int pointer with the * operator then yields the value stored at the address it holds.
Application:
typedef int *i;definesias an alias for the typeint *(pointer to int) — it does not declare any variable, only a new type name.int j = 10;declares an integer variablejand initializes it with the value 10.i a = &j;declares a variableaof typei, i.e. of typeint *, and initializes it with the address ofj(&j). Substituting the alias, this line is exactly equivalent toint *a = &j;— perfectly legal C, not a variable named with the alias as a keyword mismatch.*adereferences the pointera, accessing the value stored at the address it holds, which is the value currently held byj.printf("%d", *a);therefore prints that value.The program contains no syntax or runtime error;
getchar()simply waits for a keypress beforereturn 0;ends the program normally.
Cross-check: Textually replacing every occurrence of the alias i with int * turns the program into the completely ordinary int *a = &j; printf("%d", *a);, whose output is unambiguously the value assigned to j at declaration. This confirms the output is 10, and that the program compiles and runs without any error.