0% found this document useful (0 votes)
9 views

Union, Bitfield, Enumeration

The document discusses unions, bitfields, and enumerations in C. Unions allow storing different data types in the same memory location. Bitfields allow defining bit-level data types. Enumerations allow assigning names to integer constants for readability.

Uploaded by

niloypandit.1408
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Union, Bitfield, Enumeration

The document discusses unions, bitfields, and enumerations in C. Unions allow storing different data types in the same memory location. Bitfields allow defining bit-level data types. Enumerations allow assigning names to integer constants for readability.

Uploaded by

niloypandit.1408
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Union, Bitfield, Enumeration

Prepared By : Mashiat Mustaq


Union

● A user-defined data type that contain elements just like structure.


● Unlike structures, all the members share the same memory location.
● So Only one member can store data at the given instance.

union union_name {
datatype member1;
datatype member2;
...
};
Union : Declaring variables

union union_name { union union_name var1, var2,


datatype member1; var3...;

datatype member2;
...
} var1, var2, ...;
Union : Size

#include <stdio.h> int main( ) {


#include <string.h>
union Data data;
union Data {
int i; printf( "Memory size occupied
by data : %d\n", sizeof(data));
float f;
char str[20];
return 0;
};
}

Equal to the size of the largest member


Union : Example

int main() printf("str = %s\n\n",data.str);


{ data.i = 5;
Data data; printf("i = %d\n",data.i);
data.f = 9.7;
printf("The size of the union
data : %ld\n\n",sizeof(data)); printf("f = %f\n",data.f);
data.i = 5; strcpy(data.str, "something");
data.f = 9.7; printf("str = %s\n",data.str);
strcpy(data.str, "something"); }
printf("i = %d\n",data.i);
printf("f = %f\n",data.f);
Union : Resources

● https://www.tutorialspoint.com/cprogramming/c_unions.htm
● https://www.geeksforgeeks.org/c-unions/
Bitfield

struct
{
data_type member_name : width_of_bit-field;
};

struct date
{
// month has value between 0 and 15,
// so 4 bits are sufficient for month variable.
int month : 4;
};
Bitfield : Example
struct date {

// d has value between 0 and 31, so 5 int main()


bits
{
// are sufficient
printf("Size of date is %lu bytes\n",
unsigned int d : 5;
sizeof(struct date));
// m has value between 0 and 15, so 4
struct date dt = { 31, 12, 2014 };
bits
printf("Date is %d/%d/%d", dt.d, dt.m,
// are sufficient
dt.y);
unsigned int m : 4;
return 0;
// let's we want to support up to
}
3000, so 12 bits will suffice

unsigned int y : 12;

};
Enumeration : Assign names to integer constants

enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};

int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
}
Enumeration

enum year{Jan, Feb, Mar, Apr, May, Jun, Jul,


Aug, Sep, Oct, Nov, Dec};

int main()
{
int i;
for (i=Jan; i<=Dec; i++)
printf("%d ", i);

return 0;
}
Enumeration

enum State {Working = 1, Failed = 0, Freezed = 0};

int main()
{
printf("%d, %d, %d", Working, Failed, Freezed);
return 0;
}
Enumeration

enum day {sunday = 1, monday, tuesday = 5,


wednesday, thursday = 10, friday, saturday};

int main()
{
printf("%d %d %d %d %d %d %d", sunday, monday, tuesday,
wednesday, thursday, friday, saturday);
return 0;
}

You might also like