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

Typedef Questions and Answers

1.

What will be the output of the program?

#include <stdio.h>

int main()
{
    typedef int LONG;
    LONG a=4;
    LONG b=68;
    float c=0;
    c=b;
    b+=a;
    printf("%d,", b);
    printf("%f\n", c);
    return 0;
}

 

Answer: A

Correct answer: 72, 68.000000

Take the two statements in order. c = b; copies 68 into the float before any addition, so c holds 68. Then b += a; makes b equal to 72. The output is therefore 72 from %d and 68.000000 from %f, which prints six decimal places by default. LONG is simply an alias for int and changes nothing.

Please log in or register to report this question.

2.

What is x in the following program?

#include <stdio.h>

int main()
{
    typedef char (*(*arrfptr[3])())[10];
    arrfptr x;
    return 0;
}

 

Answer: C

Correct answer: x is an array of three function pointers

Work outward from the name: arrfptr[3] makes it an array of three; the * before it makes each element a pointer; the () makes each a pointer to a function; and (*...)[10] with the leading char makes each function return a pointer to an array of ten char. The declaration is valid, which is exactly why the alias is worth having.

Please log in or register to report this question.

3.

Which of the following may create problem in the typedef program?

Answer: D

Correct answer: All of the mentioned

All three can cause trouble, though for different reasons. A stray or missing semicolon is the classic typedef error because the declaration must be terminated exactly like any other. An alias hides the underlying type, so the wrong printf or scanf conversion is easily chosen. And arithmetic on an aliased pointer or array type behaves according to the real type, not the name.

Please log in or register to report this question.

4.

In the following code, the P2 is Integer Pointer or Integer?

typedef int *ptr;
ptr p1, p2;

 

Answer: B

Correct answer: Integer pointer

The alias carries the asterisk, so ptr means int * and ptr p1, p2; declares two pointers. This is exactly where a pointer typedef behaves better than the raw type: int *p1, p2; would make p2 a plain int, because there the asterisk belongs to the declarator rather than to the base type.

Please log in or register to report this question.

Answer: C

Correct answer: Execution of the program is faster than that of static memory allocation

This is the incorrect statement. Dynamic allocation is slower, not faster: each request involves a run-time search of the heap for a suitable block, and freeing returns it for reuse, all of which costs time that a statically allocated object does not incur. The other three are correct - the heap is less structured, it suits unpredictable requirements, and the allocation can change as the program runs.

Please log in or register to report this question.

6.

The size of both stack and heap remains the same during run time.

Answer: B

Memory can be allocated or de-allocated during the run time in the heap region. Memory bindings (allocation and de-allocation) are established and destroyed during the execution of the program. Hence we can see that the size of heap does not remain same during run time. However, the size of stack remains the same.

Please log in or register to report this question.

7.

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.

8.

For the following expression to work, which option should be selected.
string p = “HELLO”;

Answer: B

Correct answer: typedef char * string;

The alias must be a pointer type, because a string literal in an expression yields the address of its first character. With typedef char *string; in place, string p = "HELLO"; simply stores that address. An array alias will not serve: an array type cannot be assigned to, and an unsized one is incomplete.

Please log in or register to report this question.

9.

What will be the output of the following C code? (If the name entered is: Sanfoundry)

#include <stdio.h>
#include <string.h>
typedef struct employee
{
    char  name[50];
    int   salary;
} e1;
void main( )
{
    printf("Enter Employee name");
    scanf("%s",e1.name);
    printf("\n%s",e1.name);
}

 

Answer: D

Correct answer: Error

The declaration ends } e1;, and because it begins with typedef, e1 is the name of a type, not a variable. Writing e1.name therefore applies the member operator to a type name, which the compiler rejects. Without the typedef keyword the same line would have declared e1 as a variable and the program would have worked.

Please log in or register to report this question.

Answer: D

Correct answer: All of the above.

A typedef aliases any type whatever, and all three cases named are ordinary uses: typedef struct node Node; for a compound type, typedef struct node *NodePtr; for a pointer to one, and typedef int (*Compare)(const void *, const void *); for a function pointer. That last is where typedef earns its keep, since the raw declarator is barely readable.

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