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. 10Concept: 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…

  1. A.

    10

  2. B.

    0

  3. C.

    1

  4. 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:

  1. typedef int *i; defines i as an alias for the type int * (pointer to int) — it does not declare any variable, only a new type name.

  2. int j = 10; declares an integer variable j and initializes it with the value 10.

  3. i a = &j; declares a variable a of type i, i.e. of type int *, and initializes it with the address of j (&j). Substituting the alias, this line is exactly equivalent to int *a = &j; — perfectly legal C, not a variable named with the alias as a keyword mismatch.

  4. *a dereferences the pointer a, accessing the value stored at the address it holds, which is the value currently held by j.

  5. printf("%d", *a); therefore prints that value.

  6. The program contains no syntax or runtime error; getchar() simply waits for a keypress before return 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.

Explore the full course: Isro

Loading lesson…