C Multiple Choice Questions & Answers (MCQs) on “Typedef”.
1. Which of the following keywords is used to define an alternate name for an already existing data type?
a) default
b) volatile
c) typedef
d) static
Answer: c
Clarification: The keyword typedef is used to define an alternate name for an already existing data type. It is mostly used for used defined data types.
2. We want to create an alias name for an identifier of the type unsigned long. The alias name is: ul. The correct way to do this using the keyword typedef is ____________
a) typedef unsigned long ul;
b) unsigned long typedef ul;
c) typedef ul unsigned long;
d) ul typedef unsigned long;
Answer: a
Clarification: The syntax of the keyword typedef is: keyword
Hence if we want to create an alias name (ul) for an identifier of type unsigned long, the correct way to do this would be: typedef unsigned long ul;
3. What will be the output of the following C code?
#includemain() { typedef int a; a b=2, c=8, d; d=(b*2)/2+8; printf("%d",d); }
a) 10
b) 16
c) 8
d) error
Answer: a
Clarification: In the code shown above, the keyword typedef is used to give an alias name (a) to an identifier of the type int. The expression on evaluation gives the answer 10. Hence the output of the code shown above is 10.
4. WWhat will be the output of the following C code? (If the name entered is: )
#include#include typedef struct employee { char name[50]; int salary; } e1; void main( ) { printf("Enter Employee name"); scanf("%s",e1.name); printf("n%s",e1.name); }
a) .name
b) n
c)
d) Error
Answer: d
Clarification: The code shown above will result in an error because we have used the data type e1 (defined using the keyword typedef) in the form of an identifier.
5. The keyword typedef cannot be used to give alias names to pointers.
a) True
b) False
Answer: b
Clarification: The statement given in the question is incorrect. The keyword typedef can be used to give an alias name to all data types as well as pointers.
6. What is the size of myArray in the code shown below? (Assume that 1 character occupies 1 byte)
typedef char x[10]; x myArray[5];
a) 5 bytes
b) 10 bytes
c) 40 bytes
d) 50 bytes
Answer: d
Clarification: The size of myArray will be equal to 50 bytes. In the code shown above, we have defined a character array x, of size 10. Hence the output of the code shown above is 10*5 = 50 bytes.
7. We want to declare x, y and z as pointers of type int. The alias name given is: intpt The correct way to do this using the keyword typedef is:
a)
int typedef* intptr; int x,y,z;
b)
typedef* intptr; int x,y,z;
c)
int typedef* intptr; intptr x,y,z;
d)
typedef int* intptr; intptr x,y,z;
View Answer
Answer: d
Clarification: It shows the correct way to declare x, y and z as pointers of type int using the keyword typedef. The advantage of using typedef with pointers is that we can declare any number of pointers in a single statement.
8. Consider this statement: typedef enum good {a, b, c} hello; Which of the following statements is incorrect about hello?
a) hello is a typedef of enum good
b) hello is a structure
c) hello is a variable of type enum good
d) the statement shown above is erroneous
Answer: a
Clarification: The keyword typedef is used to give an alternate name to an existing data type. Hence hello is the new name for enum good.
9. One of the major difference between typedef and #define is that typedef interpretation is performed by the _________________ whereas #define interpretation is performed by the _____________
a) pre-processor, compiler
b) user, pre-processor
c) compiler, pre-processor
d) compiler, user
Answer: c
Clarification: The major difference between typedef and #define is that the typedef interpretation is performed by the compiler whereas #define interpretation is performed by pre-processor.
10. What will be the output of the following C code?
#includeint 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; }
a) 2, 3
b) 3, 2
c) 32
d) error
Answer: b
Clarification: In the code shown above, we have defined hello, which is the instance variable of a union (a) using typedef. On the execution of the code shown above, we obtain the output: 3, 2