C Language

Enum, typedef and Bit field

Jan 10 • Resources • 3456 Views • 1 Comment on Enum, typedef and Bit field

Enum, typedef and bit field are the different commands that we use in a programing of the C and C++ Languages. Lets have a brief discussion over these commands and questions that are asked over them. Following are the questions with their answers on enum, typedef and Bit field-

Q1. What is enum in C?
Answer: Simply thinking, instead of using ‘int’ in ANSI C, we use enum, to give an integer or a set of them a restricted value. The value will automatically be set for the elements entered, but the user can set them manually too.
For example:-
enum
{a,b=2,c};
Here a, b and are taken as integer. No other data type is allowed. by default, value of a=0,b=1,c=2,,,but since manually we have given b=2, so the value of b is changed from 1 to 2.

C Programming

Enum, typedef and Bit field

Q2.  What is the default data type of elements entered in enum ?
Answer: The default data type of elements entered in enum is “int”,,,integers only..

Q3.  Can we keep values of two different elements in enum same?
Answer: Yes,of course..
For example:-
enum
{a,b=2,c};
a=0,b=1,c=2,,,but since manually we have given b=2, so the value of b is changed from 1 to 2. And hence b and c both are equal to 2.

Q4.  If given a code
enum
{
mon,tues,wed,thurs,fri,sat,sun;
} day;

then what is the default value assigned to fri?
Answer: By default, enum gives the leftmost element a ‘0’ value, and second from left ‘1’ and so on. Therefore, by simple calculations fri=4.

Q5. What is  typedef in C language?
Answer: typedef in C is used to rename a data type for the user’s ease. For example:
typedef int length;
length min,max;
In this example,,length can be thought of as a new name of ‘int’.. Done for feasibility of the coder  while designing big codes. So min and max are actually integers,but since ‘int’ has been renamed as ‘length’, so we use length while declaration of min and max.
Here is a video to help you
Video tutorial on Union and Bit field

Q6. What are the differences between enum and typedef in c?
Answer: There are two basic differences-
1. enum can be applied for integers only. While typedef can be applied for any data type.
2. enum sets the default values of elements entered within it, while typedef can rename any data type. Which is better? Depends on requirement.

Q7. How is typedef different from #define?
Answer: There are two basic differences although typedef and its counter part are more or less the same. These are-
1. #define is ‘pre-processor directive’, which means it runs before the actual C-compiler.
2. In some cases typedef might not work. For example-
let’s say you define a synonim for the int type with:

typedef int MYINT
Now you can declare an int variable either with

int a;
or
MYINT a;

But you cannot declare an unsigned int (using the unsigned modifier) with

unsigned MYINT a;
although
unsigned int a;

would be perfectly acceptable.

Q8. What is the necessity of using bit-fields in C?
Answer: Bit-fields uses struct in its declaration,but it labels each field and determined its width and size.So that any point of time, the coder cannot Bit fields are used in programs that must force a data structure to correspond to a fixed hardware representation.

Q9.  How do we declare bit-fields in C?
Answer: We declare bit-fields as follows:-

struct number {
int a : 12;
int b : 6;
};

Q10.  What are the restrictions imposed on bit-fields in C?
Answer: Following are the restrictions imposed on bit-fields. You cannot-

  • Define an array of bit-fields.
  • Have a pointer to a bit-field.
  • Have a reference to a bit-field.
  • Take the address of a bit-field.

Get the pdf of the questions on enum, typedef and bit field here QUESTIONS ON ENUM, TYPEDEF AND BIT FIELD IN PDF

Please give your answers in the comment section below.

Related Posts-
Important Questions on Control statements in C
Interview Questions on Operators in C

Tell us Your Queries, Suggestions and Feedback

Your email address will not be published.

One Response to Enum, typedef and Bit field

  1. Rachita Mishra says:

    he enum keyword is used to declare an enumeration, a distinct type consisting of a set of named constants called the enumerator list.typedef is a keyword in the C and C++ programming languages. The purpose of typedef is to assign alternative names to existing types.

« »