Difference between Structure and Union
Structure Union
For defining structure use
1 For defining union we use union keyword
struct keyword.
Structure occupies more
2 Union occupies less memory space than Structure.
memory space than union.
In Structure we can access
3 all members of structure at a In union we can access only one member of union at a time.
time.
Structure allocates separate Union allocates one common storage space for its all
4 storage space for its every members. Union find which member need more memory
members. than other member, then it allocate that much space
Bit‐Fields
Syntax
struct {
type [member_name] : width ;
};
The following table describes the variable elements of a bit field −
Elements Description
type An integer type that determines how a bit-field's value is interpreted.
The type may be int, signed int, or unsigned int.
member_name The name of the bit-field.
width The number of bits in the bit-field. The width must be less than or equal
to the bit width of the specified type.
C PROGRAMMING Page 261
,The variables defined with a predefined width are called bit fields. A bit field can hold more
than a single bit; for example, if you need a variable to store a value from 0 to 7, then you can
define a bit field with a width of 3 bits as follows −
struct {
unsigned int age : 3;
} Age;
The above structure definition instructs the C compiler that the age variable is going to use only
3 bits to store the value. If you try to use more than 3 bits, then it will not allow you to do so. Let
us try the following example –
#include <stdio.h>
#include <string.h>
struct {
unsigned int age : 3;
} Age;
int main( ) {
Age.age = 4;
printf( "Sizeof( Age ) : %d\n", sizeof(Age) );
printf( "Age.age : %d\n", Age.age );
Age.age = 7;
printf( "Age.age : %d\n", Age.age );
Age.age = 8;
printf( "Age.age : %d\n", Age.age );
return 0;
}
Output
Sizeof( Age ) : 4
C PROGRAMMING Page 262
, Age.age : 4
Age.age : 7
Age.age : 0
typedef
The typedef is a keyword that allows the programmer to create a new data type name for an
existing data type. So, the purpose of typedef is to redefine the name of an existing variable type.
Syntax
typedef datatype alias_name;
Example of typedef
#include<stdio.h>
#include<conio.h>
typedef int Intdata; // Intdata is alias name of int
void main()
{
int a=10;
Integerdata b=20;
typedef Intdata Integerdata; // Integerdata is again alias name of Intdata
Integerdata s;
clrscr();
s=a+b;
printf("\n Sum:= %d",s);
getch();
}
Output
Sum: 20
C PROGRAMMING Page 263
Structure Union
For defining structure use
1 For defining union we use union keyword
struct keyword.
Structure occupies more
2 Union occupies less memory space than Structure.
memory space than union.
In Structure we can access
3 all members of structure at a In union we can access only one member of union at a time.
time.
Structure allocates separate Union allocates one common storage space for its all
4 storage space for its every members. Union find which member need more memory
members. than other member, then it allocate that much space
Bit‐Fields
Syntax
struct {
type [member_name] : width ;
};
The following table describes the variable elements of a bit field −
Elements Description
type An integer type that determines how a bit-field's value is interpreted.
The type may be int, signed int, or unsigned int.
member_name The name of the bit-field.
width The number of bits in the bit-field. The width must be less than or equal
to the bit width of the specified type.
C PROGRAMMING Page 261
,The variables defined with a predefined width are called bit fields. A bit field can hold more
than a single bit; for example, if you need a variable to store a value from 0 to 7, then you can
define a bit field with a width of 3 bits as follows −
struct {
unsigned int age : 3;
} Age;
The above structure definition instructs the C compiler that the age variable is going to use only
3 bits to store the value. If you try to use more than 3 bits, then it will not allow you to do so. Let
us try the following example –
#include <stdio.h>
#include <string.h>
struct {
unsigned int age : 3;
} Age;
int main( ) {
Age.age = 4;
printf( "Sizeof( Age ) : %d\n", sizeof(Age) );
printf( "Age.age : %d\n", Age.age );
Age.age = 7;
printf( "Age.age : %d\n", Age.age );
Age.age = 8;
printf( "Age.age : %d\n", Age.age );
return 0;
}
Output
Sizeof( Age ) : 4
C PROGRAMMING Page 262
, Age.age : 4
Age.age : 7
Age.age : 0
typedef
The typedef is a keyword that allows the programmer to create a new data type name for an
existing data type. So, the purpose of typedef is to redefine the name of an existing variable type.
Syntax
typedef datatype alias_name;
Example of typedef
#include<stdio.h>
#include<conio.h>
typedef int Intdata; // Intdata is alias name of int
void main()
{
int a=10;
Integerdata b=20;
typedef Intdata Integerdata; // Integerdata is again alias name of Intdata
Integerdata s;
clrscr();
s=a+b;
printf("\n Sum:= %d",s);
getch();
}
Output
Sum: 20
C PROGRAMMING Page 263