UNIONS
A union is a collection of one or more variables, possibly of different types.
The only difference between structures and unions is in the terms of storage of
their members. In structures, separate memory is allocated to each member, while
in unions, all the members of an object share the same memory.
Operations on union objects: All the operations on union objects are applied in
the same way as they are applied on the structure objects.
Important points about unions:
1. Defining a union type: A union type is defined in the same way as a structure
type, with only difference that the keyword union is used instead of the
keyword struct .
2. Declaring union objects: Objects of a union type can be declared either at the
time of union type definition or after the union type definition in a separate
declaration statement.
The general form of declaring a union object is:
[storage class specifier] [type qualifier] union named_union_type identifier_name
[=intialization_list [,…]];
Important points about a union object declaration:
1. The terms enclosed with the square brackets are optional and might not be
present in a union variable declaration statement.
2. A union object declaration consists of:
•The keyword union for declaring union variables. It can also be used in
conjunction with const qualifier for declaring a union constant.
•The tag-name of the defined union type.
•Comma separated list of identifiers. The variables can optionally be initialized by
providing initialization lists. However, the initialization of constants is must.
•A terminating semicolon.
3. Size of a union object or union type: Upon the declaration of a union object,
the amount of memory allocated to it is the amount necessary to contain its
largest member.
4. Address-of a union object: The members of a union object are stored in the
memory in such a way that they overlap each other. All the members of a union
object start from the same memory location.
5. Initialization of a union object: Since the members of a union object share the
same memory, the union object can hold the value of only one of its member at a
time.
#include<stdio.h>
union variables
{
char a;
int b;
float c;} ;
main()
{union variables var={‘A’, 2, 2.5};
printf(“The values of the members are %c %d %f”, var.a, var.b, var.c); }
Output:
Compilation error “Declaration syntax error in
function main()”
ENUMERATIONS
Enumeration type is designed for the variables that can have a limited set of
values.
For example: We can create an enumeration type BOOL that have two values
FALSE and TRUE.
Important points about enumerations:
Definition of an enumeration type: The general form of enumeration type
definition is:
[storage_class_specifier][type_qualifier] enum [tag-name] {enumeration-list}
[identifier=initializer[,…]];
•An enumerator is an identifier that can hold an integer value. It is also known as
enumeration constant.
•The names of the enumerators in the enumeration list must be unique
•values assigned to enumerators in the enumeration list need not be unique e.g.
the enumeration definition enum COLORS {red=2, green=1, yellow=1}; is
perfectly valid.
•Each enumeration constant has a scope that begins just after its appearance in
the enumeration list. enum COLORS {red=2, green=red, yellow=green}; is
perfectly valid.
•The enumeration definition can optionally have the storage class specifier and
type qualifiers.
•The enumeration definition is a statement and must be terminated with a
semicolon.
#include<stdio.h>
enum CARS {alto, omni, esteem=3, wagonR, swift=1, dzire};
main()
{
printf(“The value of various enumeration constants are:n”);
printf(“%d %d %d %d %d %d”, alto, omni, esteem, wagonR, swift, dzire);
}
Output:
The values of various enumeration constants are:
0 1 3 4 1 2
Declaring objects of an enumeration type: There are two ways to declare
variables of an enumeration type:
•At the time of enumeration type definition: Objects of an enumeration type
can be declared at the time of enumeration type definition.
enum BOOL {false, true} flag1, flag2;
enum {false, true} flag1, flag2;
enum BOOL {false, true} const flag1=true, flag2=false;
const enum BOOL {false, true} flag1=true, flag2=false;
enum {false, true} const flag1=true, flag2=false;
const {false, true} flag1=true, flag2=false;
•After enumeration type definition in a separate declaration statement:
Objects of an enumeration type can be created after its definition only if it is
named or tagged.
general form of declaring the objects of defined enumeration type is:
[storage_class_specifier][type_qualifier]enum named_eumeration_type
identifier_name[=initializer[,…]];
VALID
#include<stdio.h>
enum SWITCH {off, on};
main()
{
enum SWITCH s1=on;
enum SWITCH s2=s1, s3=0;
printf(“The value of enumeration object s1 is %dn”,s1);
printf(“The value of enumeration object s2 is %dn”,s2);
printf(“The value of enumeration object s3 is %dn”,s3);
}
Operations on the objects of an enumeration type: The following operations
can be performed on the object of an enumeration type:
•Size of an enumeration object or enumeration type: An enumeration object
holds an enumerator, which in fact is an integer constant. It outputs the size of
an integer.
•Address-of an enumeration object: The address-of operator can be applied on
an enumeration object to find the address of the memory space allocated to it.
Output:
The value of enumeration object s1 is 1
The value of enumeration object s2 is 1
The value of enumeration object s3 is 0
•Assignment of an enumeration object to an enumeration variable: A
variable of an enumeration type can be assigned with another object of the
same enumeration type or with one of the enumerators present in the
enumeration list or with an integer value.
•Behavior of equality operator on the objects of an enumeration type: The
equality operator can be applied to check the equality of two objects of an
enumeration type.
#include<stdio.h>
enum SWITCH {off, on};
main()
{
enum SWITCH s1=on, s2;
s2=s1; //Assignment to an enumeration variable
if(s1==s2) //Testing the equality of two enumeration variables
printf(“Both the switches are in ON staten”);
else
printf(“Switches are in different statesn”);
}
Output:
Both the switches are in ON state
•Other operators: All the operators that work on integer objects can be applied
on the objects of an enumeration type and those which can be applied on
integer constants can be applied on enumerators.
•Type conversions: The objects of the enumeration type can participate in the
expressions and can be passed as arguments to functions.
•Limitation of enumeration type: The only limitation of an enumeration type is
that it is not possible to print the value of an enumeration object in the
symbolic form. The value of an enumeration object is always printed in the
integer form.
#include<stdio.h>
main()
{
enum BOOLEAN {false, true} var;
var=true;
printf(“The value of var is %d”,value);
//printf(“The value of var is %s”,value);
}
Output:
The value of var is 1
BIT-FIELDS
Bit-fields help in packing several objects into a single unit.
They can only be declared as a part of a structure or a union.
In a structure or union declaration list, it is possible to specify for a member,
the number of bits that it will take in the memory. Such a member is called a
bit-field.
The general form of a bit-field declaration is:
struct|union [tag_name]
{
type member_name : integer_constant_expression;
[type member_name : integer_constant_expression;]
……………
}[variable_name];
Important points about the bit-field declaration:
•The terms shown in square brackets are optional and might not be present. The
terms shown in bold are mandatory parts of the declaration. The symbol
| stands for OR i.e. either struct or union should be present.
•A bit-field declaration can only appear within a structure or a union declaration list
•Bit-fields must be of integral type. Thus, the type that can be specified in the bit-
field declaration can be char, int or unsigned int.
•A compile time integer constant expression specifies the width of bit-field in bits
struct receiver
{
unsigned int parity: 22;
unsigned int mode: 1;
unsigned int start_bits:2;
int data;
};
main()
{
//Statements…….
}
Output:
Compilation error “Bit field too large”
•If the value of the constant expression specifying the number of bits in a bit-
field is zero, then the declaration should have no declarator .
#include<stdio.h>
struct receiver
{
unsigned int parity: 1;
unsigned int mode: 0;
unsigned int start_bits:2;
int data;
};
main()
{
//Statements…….
}
Output:
Compilation error “Bit field must
contain at least one bit”
Operations that can be performed on the bit-fields:
•Referencing a bit-field: As bit-fields are a part of the structure or union
object, they are referenced in the same way as the other structure or union
members are referenced.
•Other operations: Bit-fields behave like an integer type and can participate in
expressions in exactly the same way as an object of the integer type would do,
regardless of how many bits are there in the bit-field.
Operations that cannot be performed on bit-fields
•Address-of a bit-field: It is not possible to obtain the address of a bit-field
member. Unary address-of operator cannot be applied to a bit-field object.
•Size-of a bit-field: Like address-of operator, it is not possible to apply sizeof
operator on a bit-field object
#include<stdio.h>
struct receiver
{
unsigned int parity: 1;
unsigned int mode: 1;
unsigned int start_bits: 2;
int data;
};
main()
{
struct receiver mobile_receiver={1, 1, 2, 200};
printf(“The size of bit-field object parity is %dn”,sizeof(mobile_receiver.parity));
//Other statements
}
Output:
Compilation error “sizeof may not be applied to a bit-field in function main()”
Important points about the application of sizeof operator on bit-
fields:
•An implementation may allocate an addressable storage unit large enough to
hold a bit-field.
•An unnamed bit-field (i.e. bit-field with width 0) indicates that no further bit-
field is to be packed in the unit in which the previous bit-field (if any) is placed.
#include<stdio.h>
struct receiver
{
unsigned int parity: 1;
int :0; //Unnamed bit-field
unsigned int mode: 1;
unsigned int start_bits: 2;
int data;
};
main()
{
struct receiver mobile_receiver;
printf(“The size of object mobile_receiver is %dn”, sizeof(mobile_receiver));
}
Output:
The size of object mobile_receiver
is 6
END

discuss about the union & structure in python

  • 1.
    UNIONS A union isa collection of one or more variables, possibly of different types. The only difference between structures and unions is in the terms of storage of their members. In structures, separate memory is allocated to each member, while in unions, all the members of an object share the same memory. Operations on union objects: All the operations on union objects are applied in the same way as they are applied on the structure objects. Important points about unions: 1. Defining a union type: A union type is defined in the same way as a structure type, with only difference that the keyword union is used instead of the keyword struct . 2. Declaring union objects: Objects of a union type can be declared either at the time of union type definition or after the union type definition in a separate declaration statement.
  • 2.
    The general formof declaring a union object is: [storage class specifier] [type qualifier] union named_union_type identifier_name [=intialization_list [,…]]; Important points about a union object declaration: 1. The terms enclosed with the square brackets are optional and might not be present in a union variable declaration statement. 2. A union object declaration consists of: •The keyword union for declaring union variables. It can also be used in conjunction with const qualifier for declaring a union constant. •The tag-name of the defined union type. •Comma separated list of identifiers. The variables can optionally be initialized by providing initialization lists. However, the initialization of constants is must. •A terminating semicolon.
  • 3.
    3. Size ofa union object or union type: Upon the declaration of a union object, the amount of memory allocated to it is the amount necessary to contain its largest member. 4. Address-of a union object: The members of a union object are stored in the memory in such a way that they overlap each other. All the members of a union object start from the same memory location. 5. Initialization of a union object: Since the members of a union object share the same memory, the union object can hold the value of only one of its member at a time. #include<stdio.h> union variables { char a; int b; float c;} ; main() {union variables var={‘A’, 2, 2.5}; printf(“The values of the members are %c %d %f”, var.a, var.b, var.c); } Output: Compilation error “Declaration syntax error in function main()”
  • 4.
    ENUMERATIONS Enumeration type isdesigned for the variables that can have a limited set of values. For example: We can create an enumeration type BOOL that have two values FALSE and TRUE. Important points about enumerations: Definition of an enumeration type: The general form of enumeration type definition is: [storage_class_specifier][type_qualifier] enum [tag-name] {enumeration-list} [identifier=initializer[,…]]; •An enumerator is an identifier that can hold an integer value. It is also known as enumeration constant. •The names of the enumerators in the enumeration list must be unique •values assigned to enumerators in the enumeration list need not be unique e.g. the enumeration definition enum COLORS {red=2, green=1, yellow=1}; is perfectly valid. •Each enumeration constant has a scope that begins just after its appearance in the enumeration list. enum COLORS {red=2, green=red, yellow=green}; is perfectly valid.
  • 5.
    •The enumeration definitioncan optionally have the storage class specifier and type qualifiers. •The enumeration definition is a statement and must be terminated with a semicolon. #include<stdio.h> enum CARS {alto, omni, esteem=3, wagonR, swift=1, dzire}; main() { printf(“The value of various enumeration constants are:n”); printf(“%d %d %d %d %d %d”, alto, omni, esteem, wagonR, swift, dzire); } Output: The values of various enumeration constants are: 0 1 3 4 1 2
  • 6.
    Declaring objects ofan enumeration type: There are two ways to declare variables of an enumeration type: •At the time of enumeration type definition: Objects of an enumeration type can be declared at the time of enumeration type definition. enum BOOL {false, true} flag1, flag2; enum {false, true} flag1, flag2; enum BOOL {false, true} const flag1=true, flag2=false; const enum BOOL {false, true} flag1=true, flag2=false; enum {false, true} const flag1=true, flag2=false; const {false, true} flag1=true, flag2=false; •After enumeration type definition in a separate declaration statement: Objects of an enumeration type can be created after its definition only if it is named or tagged. general form of declaring the objects of defined enumeration type is: [storage_class_specifier][type_qualifier]enum named_eumeration_type identifier_name[=initializer[,…]]; VALID
  • 7.
    #include<stdio.h> enum SWITCH {off,on}; main() { enum SWITCH s1=on; enum SWITCH s2=s1, s3=0; printf(“The value of enumeration object s1 is %dn”,s1); printf(“The value of enumeration object s2 is %dn”,s2); printf(“The value of enumeration object s3 is %dn”,s3); } Operations on the objects of an enumeration type: The following operations can be performed on the object of an enumeration type: •Size of an enumeration object or enumeration type: An enumeration object holds an enumerator, which in fact is an integer constant. It outputs the size of an integer. •Address-of an enumeration object: The address-of operator can be applied on an enumeration object to find the address of the memory space allocated to it. Output: The value of enumeration object s1 is 1 The value of enumeration object s2 is 1 The value of enumeration object s3 is 0
  • 8.
    •Assignment of anenumeration object to an enumeration variable: A variable of an enumeration type can be assigned with another object of the same enumeration type or with one of the enumerators present in the enumeration list or with an integer value. •Behavior of equality operator on the objects of an enumeration type: The equality operator can be applied to check the equality of two objects of an enumeration type. #include<stdio.h> enum SWITCH {off, on}; main() { enum SWITCH s1=on, s2; s2=s1; //Assignment to an enumeration variable if(s1==s2) //Testing the equality of two enumeration variables printf(“Both the switches are in ON staten”); else printf(“Switches are in different statesn”); } Output: Both the switches are in ON state
  • 9.
    •Other operators: Allthe operators that work on integer objects can be applied on the objects of an enumeration type and those which can be applied on integer constants can be applied on enumerators. •Type conversions: The objects of the enumeration type can participate in the expressions and can be passed as arguments to functions. •Limitation of enumeration type: The only limitation of an enumeration type is that it is not possible to print the value of an enumeration object in the symbolic form. The value of an enumeration object is always printed in the integer form. #include<stdio.h> main() { enum BOOLEAN {false, true} var; var=true; printf(“The value of var is %d”,value); //printf(“The value of var is %s”,value); } Output: The value of var is 1
  • 10.
    BIT-FIELDS Bit-fields help inpacking several objects into a single unit. They can only be declared as a part of a structure or a union. In a structure or union declaration list, it is possible to specify for a member, the number of bits that it will take in the memory. Such a member is called a bit-field. The general form of a bit-field declaration is: struct|union [tag_name] { type member_name : integer_constant_expression; [type member_name : integer_constant_expression;] …………… }[variable_name];
  • 11.
    Important points aboutthe bit-field declaration: •The terms shown in square brackets are optional and might not be present. The terms shown in bold are mandatory parts of the declaration. The symbol | stands for OR i.e. either struct or union should be present. •A bit-field declaration can only appear within a structure or a union declaration list •Bit-fields must be of integral type. Thus, the type that can be specified in the bit- field declaration can be char, int or unsigned int. •A compile time integer constant expression specifies the width of bit-field in bits struct receiver { unsigned int parity: 22; unsigned int mode: 1; unsigned int start_bits:2; int data; }; main() { //Statements……. } Output: Compilation error “Bit field too large”
  • 12.
    •If the valueof the constant expression specifying the number of bits in a bit- field is zero, then the declaration should have no declarator . #include<stdio.h> struct receiver { unsigned int parity: 1; unsigned int mode: 0; unsigned int start_bits:2; int data; }; main() { //Statements……. } Output: Compilation error “Bit field must contain at least one bit”
  • 13.
    Operations that canbe performed on the bit-fields: •Referencing a bit-field: As bit-fields are a part of the structure or union object, they are referenced in the same way as the other structure or union members are referenced. •Other operations: Bit-fields behave like an integer type and can participate in expressions in exactly the same way as an object of the integer type would do, regardless of how many bits are there in the bit-field. Operations that cannot be performed on bit-fields •Address-of a bit-field: It is not possible to obtain the address of a bit-field member. Unary address-of operator cannot be applied to a bit-field object. •Size-of a bit-field: Like address-of operator, it is not possible to apply sizeof operator on a bit-field object
  • 14.
    #include<stdio.h> struct receiver { unsigned intparity: 1; unsigned int mode: 1; unsigned int start_bits: 2; int data; }; main() { struct receiver mobile_receiver={1, 1, 2, 200}; printf(“The size of bit-field object parity is %dn”,sizeof(mobile_receiver.parity)); //Other statements } Output: Compilation error “sizeof may not be applied to a bit-field in function main()”
  • 15.
    Important points aboutthe application of sizeof operator on bit- fields: •An implementation may allocate an addressable storage unit large enough to hold a bit-field. •An unnamed bit-field (i.e. bit-field with width 0) indicates that no further bit- field is to be packed in the unit in which the previous bit-field (if any) is placed. #include<stdio.h> struct receiver { unsigned int parity: 1; int :0; //Unnamed bit-field unsigned int mode: 1; unsigned int start_bits: 2; int data; }; main() { struct receiver mobile_receiver; printf(“The size of object mobile_receiver is %dn”, sizeof(mobile_receiver)); } Output: The size of object mobile_receiver is 6
  • 16.