C Questions and Answers – Typedef

«
»

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Typedef”.

Pre-requisite for C Typedef MCQ set: Video Tutorial on C Typedef.

advertisement

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
View Answer

Answer: c
Explanation: 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;
View Answer

Answer: a
Explanation: The syntax of the keyword typedef is: keyword <existing_name> <alias_name>;
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;
advertisement
advertisement

3. What will be the output of the following C code?

#include<stdio.h>
main()
{
    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
View Answer

Answer: a
Explanation: 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.
advertisement

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

advertisement
#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);
}

a) Sanfoundry.name
b) nSanfoundry
c) Sanfoundry
d) Error
View Answer

Answer: d
Explanation: 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.
advertisement

5. The keyword typedef cannot be used to give alias names to pointers.
a) True
b) False
View Answer

Answer: b
Explanation: 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
View Answer

Answer: d
Explanation: 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
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: c
Explanation: 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?

#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;
}

a) 2, 3
b) 3, 2
c) 32
d) error
View Answer

Answer: b
Explanation: 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

Sanfoundry Global Education & Learning Series – C Programming Language.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter

Check his free tutorial videos on Advanced C Programming. Join his Sanfoundry-Classes telegram channel for all videos and queries on Programming, Data-Structures, Kernel, Personality development and Mentoring.