0% found this document useful (0 votes)
13 views15 pages

CSE109 Week9

Uploaded by

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

CSE109 Week9

Uploaded by

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

Bit-Fields, Unions, Enumerations

Lecture: 20
Reference Book: Teach yourself C (3rd Ed.)
Chapter/Section: 10.4

Samin Rahman Khan


Lecturer (part-time), CSE, BUET
Lecturer, IICT, BUET
Bit-Fields
● C allows a variation on a structure member called bit-field. A bit
field is composed of one or more bits. Using a bit-field, you can
access by name one or more bits within a byte or word.
● To define a bit-field, we use the following form:
type name: size;
● Here, type is either int or unsigned. If we specify bit-field, then the
high-order bit is treated as a sign bit, if possible.
● The number of bits in the field is specified by size.
● Bit-fields are useful when you want to pack information into the
smallest possible space.
Bit-Fields
A structure that uses bit-fields to hold inventory information.

struct b_type {
unsigned department: 3; /* up to 7 departments */
unsigned instock: 1; /* 1 if in stock, 0 if out */
unsigned backordered: 1; /* 1 if backordered, 0 if not */
unsigned lead_time: 3; /* order lead time in months */
} inv[MAX_ITEM];

In this case one byte can be used to store information on an inventory item that
would normally have taken 4 bytes without use of bit-fields.

inv[9].department = 3;
Bit-Fields
● It is not necessary to completely define all bits within a byte
or word. For example, this is perfectly valid:

struct b_type {
int a: 2;
int b: 3;
};

● The C compiler is free to store bit-fields as it sees fit.


However, usually the compiler will automatically store bit-
fields in the smallest unit of memory that will hold them.
Bit-Fields
● We can mix bit-fields with other types of members in a structure’s definition.

struct b_type {
char name[40]; /* name of item */
unsigned department: 3; /* up to 7 departments */
unsigned instock: 1; /* 1 if in stock, 0 if out */
unsigned backordered: 1; /* 1 if backordered, 0 if not */
unsigned lead_time: 3; /* order lead time in months */
} inv[MAX_ITEM];

● Because the smallest addressable unit of memory is a byte, you cannot obtain
the address of a bit-field variable.
Bit-Fields
● It is not necessary to name every bit when using bit-fields.

struct b_type {
unsigned first: 1;
int: 6;
unsigned last: 1;
};

● Because the smallest addressable unit of memory is a byte, you cannot obtain
the address of a bit-field variable.
Unions
● In C, a union is a single piece of memory that is shared by two or more
variables.
● The variables that share the memory may be different types. However, only one
variable may be in use at any one time.
● A union is defined much like a structure. Its general form is
union tag-name {
type member1;
type member2;
type member3;
.
.
type memberN;
} variable-names;
● Either the tag-name or the variable-names may be missing.
● Members may be any valid data type.
Unions
union u_type {
int i;
char c[2];
double d;
} sample;

d
c[0] + c[1]

i
Example
union intParts {
int theInt;
char bytes[sizeof(int)];
};

int main() {
union intParts parts;
parts.theInt = 5968145; // arbitrary number > 255 (1 byte)

printf("The int is %d\nThe bytes are [%d, %d, %d, %d]\n",


parts.theInt, parts.bytes[0], parts.bytes[1],
parts.bytes[2], parts.bytes[3]);
}
Enumerations
● In C, we can define a list of named integer constants called an
enumeration.
● These constants can then be used any place an integer can.
● To define an enumeration, we use the following form:
enum tag-name { enumeration list } variable-list;
● Either the tag-name or the variable-list is optional. The tag-name is
essentially the type name of the enumeration.

enum color_type {red, green, yellow} color;


Enumerations
● By default, the compiler assigns integer values to enumeration
values, beginning with 0 at the far left sife of the list. Each constant
to the right is one greater than the constant that precedes it.
● enum color_type {red, green, yellow} color; red is 0,
green is 1, and yellow is 2.
● We can override the compiler’s default values by explicitly giving a
constant a value.
enum color_type {red, green=9, yellow} color; red is 0,
green is 9, and yellow is 10.
● The declared tag name can be used to declare enumeration variables.
enum color_type mycolor;
● An enumeration is essentially an integer type and an enumeration can
hold any integer value-not just those defined by the enumeration.
Example
#include <stdio.h>

enum computer {keyboard, CPU, screen, printer};

int main() {
enum computer comp;
comp = CPU;
printf("%d", comp);
return 0;
}
Example
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

enum transport {car, train, airplane, bus} tp;

int main() {
printf("Press a key to select transport:\n");
printf("1. car\n2. train\n3. airplane\n4. bus\n");
tp = getch() - '0' - 1;
switch(tp) {
case car: printf("car"); break;
case train: printf("train"); break;
case airplane: printf("airplane"); break;
case bus: printf("bus");
}
}
Example
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

enum transport {car, train, airplane, bus} tp;


char transport_names[][20] = {
"car", "train", "airplane", "bus"
};

int main() {
printf("Press a key to select transport:\n");
printf("1. car\n2. train\n3. airplane\n4. bus\n");
tp = getch() - '0' - 1;
if (0 <= tp && tp < 4) printf("%s", transport_names[tp]);
}
Example
● The names of enumerated constants are known only to the
program, not any library functions.

enum numbers {zero, one, two, three} num;

printf("Enter a number: ");


scanf(“%d”, &num);

● We cannot respond to scanf( ) by entering one.

You might also like