• No results found

sizeof() of an Union: The size of an union is equal to the number of bytes occupied in RAM by the largest data member in the union. Consider the following example

In document C NOTES (Page 78-82)

Method I: In this method, one structure is written inside another structure

4.13.1 sizeof() of an Union: The size of an union is equal to the number of bytes occupied in RAM by the largest data member in the union. Consider the following example

data_type member _name;

data_type member _name;

--- --- }union_variable;

The union declaration starts with the union header that consists of the keyword union followed by a tag name. The individual members of the union are inclosed within curly braces. The closing brace is followed by a semicolon.

Example: Suppose we want to store the database of student having roll number, name and height.

The union declaration for this will be as:

union student {

int rno;

char name[10];

float height;

};

Initialization of Union: The initialization of the data members in a union are similar to the methods used in structure, except that only one data member can be assigned value at a time.

Example: A variable of the union student can be initialized as follows:

student boy.name=“Naveen”;

4.13.1 sizeof() of an Union: The size of an union is equal to the number of bytes occupied in RAM by the largest data member in the union. Consider the following example.

union student {

int roll_no;

char name[15];

};

The size of this union is equal to 15 bytes because the largest data member name occupies 15 bytes in RAM. The sizeof() function is written as follows to print the size of the union.

printf(“%d”,sizeof(union student);

Imagine the following how the details of a union are stored.

name roll_no

15 bytes Difference between Structure and Union

Structure Union

1. The keyword strut is used to declare the structure.

The keyword union is used to declare the union.

2. All data members in a structure are active at time.

Only one data member is active at a time.

3. All members of structure have unique memory space.

All members of union Share the same storage area.

Prepared by Mr. K. G. Page 79 4. It is useful to declare a compound data type

to group data members related to a person or item.

It is useful to certain cases where the user will select any one data member in the group of data members.

5. It is commonly used in most of the applications.

It is not commonly used.

4.14 typedef Declaration: typedef refers to definition of data types which can be used by the users to declare their own data definition names. For example, int may be declared as integer, float may be declared as real, a structure may be given another name and so on.

(i) typedef int integer (ii) typedef float real;

(iii) typedef struct {

int roll_no;

char name[15];

}student;

Using these new data definitions, variables are declared as follows.

integer a, b, c, big;

real x, y, z;

student s1, s2, s[10];

As usual, values to these variables are assigned as shown below.

a=5;

x=2.54;

s1.roll_no=140;

s[i].name=”naveen”;

typedef is useful to assign a new name to the data type or structure. It does not have any other advantage and is not commonly used in programming.

4.15 enum Data Type: An enumeration is a user defined type with values ranging over a finite set of identifiers called enumeration constant. For example, days in week may be declared as enumerated data type as follows.

enum days{SUN,MON,TUE,WED,THU,FRI,SAT};

where enum is the keyword for enumerated declaration.

days is the name of enumerated data type.

SUN,MON,…..,SAT are the values in the enumerated list.

Using this data type, variables are declared as follows.

enum days holiday,salary_day;

The variables declared here may be assigned any one of the values given in the enumerated list.

holiday=SUN;

salary_day=SAT;

It is to be noted that only the values which are given in the enumerated list are accepted. For example, the following assignments are not accepted.

holiday=SUNDAY;

holiday=”SUNDAY”;

salary_day=SATURDAY;

It is also possible to assign integer value to the identifiers in the enumerated list. C will consider the first data in enum list as 0, second as 1, and so on, if it has not been assigned the value.

enum days{SUN=1,MON=2,TUE=3,WED=4,THU=5,FRI=6,SAT=7};

When the value of one identifier is specified in this manner, the value of the next element is the next higher integer. For example, if the definition of day is

Prepared by Mr. K. G. Page 80 enum days{SUN=1,MON,TUE,WED,THU,FRI,SAT=99};

then the statement day=SUN will assign the value 1 to day, day=MON will assign the value 2 to day, day=TUE will assign the value 3 to day and day=SAT will assign the value 99 to day. The variables of enumerated data type may be assigned as follows:

holiday=1;

salary_day=7;

Enumerations are a convenient way to associate constant integers with meaningful names. They have the advantage of generating values automatically. The enum data type is not commonly used in programming and it may not be supported by some of the C compilers.

Note:

1. Names in different enums must be distinct. The following example is invalid.

enum emotion {happy,hot,cool};

enum weather{hot,cold,wet};

It is not difficult to see why the above declarations are invalid because, the name hot has the value 1 in enum emotion and the value 0 in weather.

2. Values need not be distinct in the same enumeration. For example, the following declaration is perfectly valid.

enum weather{hot,warm=0,cold,wet};

The names hot and warm can be interchangeably used, since both represent the value 0. Example:

#include<stdio.h>

#include<conio.h>

void main() {

enum Days{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};

enum Days Day;

int n;

clrscr();

printf("Enter the day of the week (0 to 6): ");

scanf("%d",&n);

Day = n;

if(Day == Sunday || Day == Saturday) printf("Hurray it is the weekend\n");

else

printf("Curses still at work\n");

getch();

}

Output:

Enter the day of the week (0 to 6): 5 Curses still at work

4.16 Bit Field: In C language, the user can decide the number of bits in memory (RAM) to be allotted to a variable. It overcomes the prescribed number of bytes allocated to a variable by the compilers.

In some of the cases, a variable can have either 0 or 1, but if it is declared as int which occupies 16 bits in RAM. In order to overcome this 16 bit default allocation and to allow just one bit, the variable may be defined as a size 1 bit as shown below:

unsigned int sex:1;

Prepared by Mr. K. G. Page 81 The bit field definition is very useful when a structure has more data members which normally refer small values. Consider the following data members in a student structure.

struct student {

int roll_no;

char name[15];

int sex;

struct date {

int day;

int month;

int year;

}jdate;

int mark;

};

The size of this structure is 32 bytes, which is equal to 216 bits, which may be reduced by considering the exact range of values for the data members as given below.

data member sex may be declared with 1 bit which is either 0 or 1 day with 5 bits for the range 1 to 32

month with 4 bits for the range 1 to 12 year with 7 bits for the range 1 to 99 mark with 7 bits for the range 0 to 100

struct student {

int roll_no;

char name[15];

unsigned int sex:1;

struct date {

int day:5;

int month:4;

int year:7;

}jdate;

int mark:7;

};

Now the size of the structure is 20 bytes, which is equal to 160 bits, which leads to saving of 7 bytes (56 bits). Suppose we store the details of 100 students then we save about 700 bytes of memory space in RAM in the storage device.

Uses of Bit Fields: The following are the uses of bit fields.

1. The storage space required to store the details in a structure is economically used by using the bit fields.

2. The saving of storage space is considerably high when a large quantity of such data is stored.

Prepared by Mr. K. G. Page 82 Unit-5

Pointer Data type and its Application

5.1 Pointer: A pointer is variable that points to or reference a memory location in which data is

In document C NOTES (Page 78-82)