answersLogoWhite

0


Best Answer

There is no relationship other than that they are programming concepts. they each serve a different purpose.

Type Definition

A type definition is an alias; an alternate name for a type that already exists. Type definition help to make code easier to read. For instance, instead of using the type "unsigned long long int", we can alias it:

typedef unsigned long long int UINT;

Now we can use the UINT type wherever we would normally have used "unsigned long long int".

Union

A union is a type with two or more members of different types that all have the same start address. The length of a union is determined by the longest member of the union. Unions are useful when you have a variety of data types but do not need to maintain separate values for those types; only one member can be active at any one time. As an example, consider the following:

union pointer {

int* pint;

char* pchar;

};

Here we can assign the memory address of an integer to pint and then read back the first byte of that integer using pchar:

int i=0x12345678;

pointer x;

x.pint = &i;

std::cout<<"Integer: "<<*x.pint<

std::cout<<"Byte: "<<*x.pchar<

The output will be:

Integer: 305419896

Byte: 120

Unions are best avoided as they are often used to provide type-casts that are inherently unsafe.

Enum

An enum is an enumeration. When you define an enumeration you define a type for which some values of that type have names. Those names are constant members of the enum.

enum traffic_light {red, amber, green};

If a type is not specified, the type defaults to an int. Given that there are only three values in this enum, a char would suffice:

enum traffic_light : char {red, amber, green};

Enums are useful in that they allow a related set of constants to be grouped.

If no values are specified, the constants are valued according to the order in which they are declared. Thus the above is equivalent to:

enum traffic_light : char {red=0, amber=1, green=2};

If you assign any value to an enum member, all subsequent members are numbered in sequence:

enum traffic_light : char {red=1, amber, green};

The above is therefore equivalent to:

enum traffic_light : char {red=1, amber=2, green=3};

Enum members can also be given the same value if desired:

enum traffic_light : char {red=1, amber=2, yellow=2, green=3};

Using enums help eliminate coding errors:

void f (traffic_light colour) {/*...*/}

f (red); // ok

f (green); // ok

f (4); // error: 4 is not a member of traffic_light!

Enums are also useful for naming bits:

enum bit {

bit_0 = 1<<0, // = 0x01 = 00000001

bit_1 = 1<<1, // = 0x02 = 00000010

bit_2 = 1<<2, // = 0x04 = 00000100

bit_3 = 1<<3, // = 0x08 = 00001000

bit_4 = 1<<4, // = 0x10 = 00010000

bit_5 = 1<<5, // = 0x20 = 00100000

bit_6 = 1<<6, // = 0x40 = 01000000

bit_7 = 1<<7 // = 0x80 = 10000000

};

void f (bit b)

{

if (b & bit_1)

{

// bit_1 is set in b

}

else

{

// bit_1 is not set in b

}

if (b & (bit_1 | bit_2))

{

// bit_1 and/or bit_2 is set

}

else

{

// neither bit_1 nor bit_2 are set

}

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is relationship between type definition and union and enum?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Difference between enum and const in c?

members in union and enum both shares memory and the size of the union is the size of the highest element like if there is one int and one char in union then the size of union will be the size of an int. if an int and an float are there then the size of the union will be the size of the float. This is not the case in the enum. it has similar type of members. and the value of the members can be assign manually like this enum e{ZERO,ONE,TWO=20,THREE}; here ZERO will have value 0, ONE will have value 1, TWO will be having 20 , THREE will be having 21.. and so on Answer: they are no way similar, no point in comparing them.


What is similarity among structure union and enum in c language?

all the three are user defined datatypes


What is the ingredients in jav enum?

Java enum is not a consumable product. Java enum is a type of computer language that allows internet users to surf the internet with more ease and makes the transition between graphics and text more fluid.


What is meant by enum in C programming?

The enum keyword means enumeration.


How do you pass enum object as argument to function in c?

You can't pass an enum as an argument to a function. An enum in C isn't an object, it's a type. All you can do is pass a variable that is of the particular enum's type.


What is similarity between a union and enumeration?

Nothing, only the syntax (very little). Eg.: struct { int foo; double bar; } strtest; enum { monday=0, tuesday=1, ... } enumtest;


When was Kadhal Enum Nadhiyinile created?

Kadhal Enum Nadhiyinile was created in 1989.


What is enum type?

An enum type is a type whose fields consist of a fixed set of constants


What are Java enums?

An enum, short for enumerated type, is a variable type that can only take on the values that are declared inside the enum declaration. An enum is declared like a class, except the word "class" is replaced by the word "enum", and the class body is replaced by a list of values that a variable of that type can take on. You can also include methods, instance variables, and constructors in an enum.


What is difference between C enum and C plus plus enum?

In C++, enum signifies a slightly stronger type than in C. For example, in C, one could write: enum Direction { UP, DOWN }; Direction d = 1; In C++, this would be illegal, only UP or DOWN can be assigned to a variable of type Direction, though there is still implicit casting to integer, so one could still write: int i = UP; Another difference has to do with the way variable are declared in general in C++. In C, once the enum was declared as above, declaring variables of type Direction would have to be done through the enum keyword, like this: enum Direction d = UP; In C++, the name "Direction" becomes a type in itself, so you can write: Direction d = UP;


In c plus plus you cannot assign integer value to enum?

That is correct - In c plus plus you cannot assign integer value to enum - You can only assign an enum value to an enum. Even though an enum looks like an integer, it is not. It is an enum, and C++ implements strict type checking to reduce the probability of bad programming practices. enum ColorCode {black, brown, red, orange, yellow, green, blue, violet, grey, white}; ColorCode myColorCode; myColorCode = yellow; Even though yellow has an integer value of 4, you cannot say myColorCode = 4.


What is the difference between array and enum?

Array is collection of data items of same data type.Enum is collection of data items of different data type.