Question

C supports user-defined data types. Explain the following data type features of the C language and provide a sample for each

0 0
Add a comment Improve this question Transcribed image text
Answer #1

[1] Renaming data types

Instead of using the predefined keywords for a certain type of data, we can customize its name according to the requirement of the program. It is achieved using the typedef keyword.

Eg.         typedef int number;

Now all integer type variable can be declared using number x instead of int x.

[2] Enumeration

An enumeration is a data type that contains integral constants. In other words, a variable declared as enum type can only hold a limited amount of values as specified during declaration. The keyword enum is used.

Eg.         enum week {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};

              enum week x;

Now, x can only hold values which correspond to the seven days of the week.

[3] Structures

A structure is a user-defined data type that comprises of (or encapsulates) multiple data types and can be used as a singular entity. The struct keyword is used.

Eg.         struct struct_name{

              int x;

              char y;

} var1, var2;

Here, var1 and var2 are variables of type struct_name and can hold one integer and one character each.

[4] Union

A union allows us to reuse the same memory location to store different types of data. It can have multiple data members but only one can hold a value a time. The union keyword is used to declare the union and its members are accessed using the . (dot) operator.

Eg.         union union_name {

              int i;

              float f;

}var1;

Now var1 can either hold I or f but not both. If we assign multiple values, only the latest will be stored in memory.

[5] Bit-fields

Bit-fields are used to specify the maximum number of bits that can be occupied by a given variable. These are used to make a program more memory efficient.

Eg.         int x:3;

Here x can only hold values that can be represented using three bits, ie, 000 to 111 (0 to 7 in decimal).

Add a comment
Know the answer?
Add Answer to:
C supports user-defined data types. Explain the following data type features of the C language and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT