Machine-translated by Google. Questions are authored in English.
Top

Typedef Questions and Answers

11.

What will be the output of the following C code?

#include <stdio.h>
    typedef struct student
    {
        char *a;
    }stu;
    void main()
    {
        struct student s;
        s.a = "hey";
        printf("%s", s.a);
    }

 

Answer: D

Correct answer: hey

Here the declaration uses the tag rather than the alias - struct student s; - and the tag student does exist, so this compiles. The member a is a char *, assigning a string literal to it is legal, and %s prints it: the output is hey. Compare the previous question, which used struct stu and failed for exactly this reason.

Please log in or register to report this question.

12.

What will be the output of the following C code?

#include <stdio.h>
    typedef int integer;
    int main()
    {
        int i = 10, *ptr;
        float f = 20;
        integer j = i;
        ptr = &j;
        printf("%d\n", *ptr);
        return 0;
    }

 

Answer: D

Correct answer: 10

typedef int integer; makes integer an alias for int - the same type, not a distinct one. So integer j = i; copies 10 into an ordinary int, and ptr = &j; is a perfectly ordinary int * assignment with no conversion involved. Dereferencing it prints 10. The unused float f is there only as a distraction.

Please log in or register to report this question.

13.

What will be the output of the following C code?

#include <stdio.h>
    int (*(x()))[2];
    typedef int (*(*ptr)())[2] ptrfoo;
    int main()
    {
        ptrfoo ptr1;
        ptr1 = x;
        ptr1();
        return 0;
    }
    int (*(x()))[2]
    {
        int (*ary)[2] = malloc(sizeof*ary);
        return &ary;
    }

 

Answer: A

Correct answer: Compile time error

The line typedef int (*(*ptr)())[2] ptrfoo; is malformed. A typedef declares the new name inside the declarator, in the position a variable name would occupy - here that position is already taken by ptr, so the trailing ptrfoo is a second declarator with nothing joining it. The compiler rejects the declaration before any of the rest matters.

Please log in or register to report this question.

14.

What will be the output of the following C code?

#include <stdio.h>
    int *(*(x()))[2];
    typedef int **(*ptrfoo)())[2];
    int main()
    {
        ptrfoo ptr1;
        ptr1 = x;
        ptr1();
        return 0;
    }
    int *(*(x()))[2]
    {
        int (*ary)[2] = malloc(sizeof * ary);
        return &ary;
    }

 

Answer: B

Correct answer: Compile time error

The typedef line is syntactically invalid: typedef int **(*ptrfoo)())[2]; has unbalanced parentheses, the () and the [2] both trying to attach after the declarator has already been closed. Compiling it produces an error at that line before execution is ever reached, and a second error follows on the incompatible assignment ptr1 = x;.

Please log in or register to report this question.

15.

What will be the output of the following C code?

#include <stdio.h>
    typedef struct p
    {
        int x, y;
    };
    int main()
    {
        p k1 = {1, 2};
        printf("%d\n", k1.x);
    }

 

Answer: A

Correct answer: Compile time error

Two things go wrong. The declaration typedef struct p { int x, y; }; supplies no new name at all, so the typedef declares nothing; and p k1 then uses the struct tag as though it were a type name, which C does not allow. Writing struct p k1, or giving the typedef a name, would fix it.

Please log in or register to report this question.

16.

What will be the output of the following C code?

#include <stdio.h>
    typedef struct p
    {
        int x, y;
    }k = {1, 2};
    int main()
    {
        p k1 = k;
        printf("%d\n", k1.x);
    }

 

Answer: A

Correct answer: Compile time error

Because the declaration begins with typedef, the k after the closing brace is a type name, and a type name cannot be given an initialiser - = {1, 2} is meaningless there. The later p k1 = k; compounds the error by using the tag p as a type. Remove the typedef and k becomes an initialised variable.

Please log in or register to report this question.

17.

What will be the output of the following C code?

#include <stdio.h>
    typedef struct p
    {
        int x, y;
    }k;
    int main()
    {
        struct p p = {1, 2};
        k k1 = p;
        printf("%d\n", k1.x);
    }

 

Answer: B

Correct answer: 1

Here everything is used correctly. k is an alias for struct p; struct p p = {1, 2}; declares and initialises a structure, the variable name p shadowing the tag harmlessly since tags live in their own namespace; and k k1 = p; copies it, structure assignment being a member-by-member copy. So k1.x is 1.

Please log in or register to report this question.

18.

Which option should be selected to work the following C expression?

string p = "HELLO";

 

Answer: B

Correct answer: typedef char *string;

A string in C is a pointer to its first character, so the alias must be a pointer type: typedef char *string; lets string p = "HELLO"; assign the literal’s address to p. The array form is not merely wrong syntax - even typedef char string[]; would give an incomplete type that cannot be initialised this way.

Please log in or register to report this question.

19.

Which of the given option is the correct method for initialization?

typedef char *string;

 

Answer: B

Correct answer: string p = “Hello”;

Once string means char *, a variable of that type is declared exactly as any other: string p = "Hello";, with no extra asterisk. Writing *string p would apply the dereference operator to a type name, which is not valid syntax, and assigning a single character constant would be a type mismatch as well.

Please log in or register to report this question.

Answer: B

Correct answer: typedef defined substitutes can be redefined again. (Eg: typedef char a; typedef int a;)

This is the false statement, and hence the answer. A typedef name may be repeated only if every declaration gives it the same type; redefining it as a different type in the same scope is an error, because typedef is handled by the compiler and subject to its rules. A #define macro, being pure text substitution, can be redefined freely.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Typedef Questions and Answers || Typedef MCQ Questions and Answers || Typedef GK Questions and Answers || Typedef GK MCQ Questions || Typedef Multiple Choice Questions and Answers || Typedef GK || GK on Typedef || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming