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

Typedef Questions and Answers

51.

In the following code what is 'P'?

typedef char *charp;
const charp P;

 

Answer: A

Correct answer: P is a constant

Since charp is a single type meaning char *, the const qualifies that whole type, giving char * const P - a constant pointer to a modifiable character. It is not const char *, which is what textual substitution of the macro form would have produced. This is one of the clearest illustrations of why typedef and #define are not interchangeable.

Please log in or register to report this question.

52.

What will be the output of the following C code?

#include <stdio.h>
int main()
{
    typedef union a
    {
        int i;
        char ch[2];
    }hello;
    hello u;
    u.ch[0] = 3;
    u.ch[1] = 2;
    printf("%d, %d", u.ch[0], u.ch[1]);
    return 0;
}

 

Answer: B

Correct answer: 3, 2

The two writes go to ch[0] and ch[1], and the two reads come straight back from the same bytes, so the output is the values in the order they were assigned: 3 then 2. The union sharing storage with int i makes no difference here, because i is never read - had it been, the result would have depended on the machine’s byte order.

Please log in or register to report this question.

53.

What is the output of this C code?

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

Every element is used correctly. k is an alias for struct p, the variable named p shadows the tag p harmlessly because tags occupy a separate namespace, and k k1 = p; is an ordinary structure copy. So k1.x holds 1. Structure assignment copies every member, which is why no loop is needed.

Please log in or register to report this question.

54.

What will be the output of following program ?

#include <stdio.h>
int main()
{
    typedef int AAA,BBB,CCC,DDD;
    AAA aaa=10;
    BBB bbb=20;
    CCC ccc=30;
    DDD ddd=40;
    printf("%d,%d,%d,%d",aaa,bbb,ccc,ddd);
    return 0;
}

 

Answer: C

Correct answer: 10,20,30,40

A single typedef can declare several names, so AAA, BBB, CCC and DDD all become aliases for int. They are four names for one type, not four distinct types, so the four variables are ordinary independent ints holding 10, 20, 30 and 40. Sharing an alias creates no relationship between the variables at all.

Please log in or register to report this question.

55.

In a C program, following variables are defined:

float      x = 2.17;

double   y = 2.17;

long double z = 2.17;

Which of the following is correct way for printing these variables via printf.

Answer: A

Correct answer: printf("%f %lf %Lf",x,y,z);

A float passed to a variadic function is promoted to double, so %f serves for both float and double, and %lf is accepted as a synonym for the same. A long double is not promoted and needs its own specifier, %Lf with a capital L; using %f for it reads the wrong number of bytes.

Please log in or register to report this question.

56.

What will be the output of the program?

#include <stdio.h>

int main()
{
    enum color{red, green, blue};
    typedef enum color mycolor;
    mycolor m = red;
    printf("%d", m);
    return 0;
}

 

Answer: D

Correct answer: 0

Enumerators are numbered from zero unless given explicit values, so red is 0, green 1 and blue 2. An enumeration constant has integer type, so %d prints the numeric value rather than the name - C keeps no run-time record of the identifier. Printing red would require an array of strings indexed by the enumerator.

Please log in or register to report this question.

57.

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.

Answer: D

Correct answer: GeeksQuiz would be printed until stack overflow happens for this program.

C permits main to call itself, and each call adds a frame to the stack that is never popped, because the recursive call precedes the return. The program therefore prints until the stack is exhausted and it crashes - typically after a few hundred thousand iterations. It is not infinite, since the stack is finite, and no compiler error arises.

Please log in or register to report this question.

59.

In the following code snippet can we declare a new typedef named ptr even though struct employee has not been completely declared while using typedef?

typedef struct employee *ptr;
struct employee
{
    char name[20];
    int age;
    ptr next;
}

 

Answer: A

Correct answer: Yes

A pointer to an incomplete type is itself a complete type, because its size and representation do not depend on what it points at. So typedef struct employee *ptr; is legal before struct employee has been defined - the tag is merely declared by its mention. This is exactly the mechanism that lets a self-referential structure hold a ptr next member.

Please log in or register to report this question.

Answer: C

Correct answer: Does not create a new type, It merely adds a new name for some existing type

Both halves are right, which is why the combined option is correct. A typedef name is a synonym, fully interchangeable with the original: no new type is created, no type checking distinguishes them, and passing one where the other is expected raises no complaint. This is the essential difference between typedef and a genuine new type such as a distinct struct.

Please log in or register to report this question.

Loading…
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