Professional Documents
Culture Documents
Once the new structure data type has been defined one or more variables can be declared
to be of that type. For example the variables book1,book2,book3 can be declared to be of
the data type struct book as follows
INITIALISATION
They should be linked to the structure variables in order to make them meaningful
members.
The link between a member and a variable is established using the member
operator which is also known as ‘dot operator’. For example
book1.price
is the variable representing the price of book1
We can also use scanf to give the values through the keyboard.
scanf(“%s”,book1.name);
scanf(“%s”,&book1.pages);
Example:
#include<stdio.h>
struct employee
{
char name[20];
int rollno;
float per;
};
void main()
{
struct employee e1,e2;
printf("Enter the employee 1 details\n");
scanf("%s%d %f",e1.name,&e1.rollno,&e1.per);
printf("Enter the employee 2 details\n");
scanf("%s%d%f",e2.name,&e2.rollno,&e2.per);
printf("The details of employees are\n");
printf("%s\t%d\t%f\n",e1.name,e1.rollno,e1.per);
printf("%s\t%d\t%f\n",e2.name,e2.rollno,e2.per);
}
USING STRUCTURES WITH FUNCTIONS
Example:
#include<stdio.h>
#include<conio.h>
struct book
{
char name[20];
char author[20];
float price;
};
UNIONS
Union is another data type with two or more members, similar to structure. The members
of a union can be referred using the dot operator as in the case of a structure.
The general form of union structure is
union name
{
data type member1;
data type member 2;
}var1,var2
But the major difference between structure and union is for structures the compiler
allocates memory for all members in adjacent memory allocation where as in unions it
allocates memory of size large enough to hold the largest variable type in the union.
e.g struct abc
{
char ch;
int a;
float b;
}x,y;
BITWISE-OPERATORS
Symbol Operation
------------------------------------------------
& Bitwise AND
| Bitwise Incluseive-OR
^ Bitwise Exclusive-OR
~ Ones Complement
<< Left Shift
>> Right Shift
--------------------------------------------------
BIT FIELDS
Def: Bitfield provides an ability to specify the no. of bits in which an unsigned or int
members of a structure or union is stored.
Bitfield members must be declared as int or unsigned int.
The general form of bit field definition is
struct name
{ variable
data-type name1 : bit-length;
data-type name2 : bit-length;
……………………
data-type name N : bit-length;
};
typedef
typedef is a keyword which provides the mechanism for creating synonyms(or aliases)
for already defined data types .
typedef struct student
{
int x;
float y;
char n[5];
}s; synonyms
ENUMERATION CONSTANTS
enum days{sun,mon,tue,wed,thu,fri,sat};
Eg:
enum days{sun=1,mon,tue,wed,thu,fri,sat};
#include<stdio.h>
void main()
{
enum days{sun,mon,tue,wed,thu,fri,sat};
printf(“\nsun=%d”,sun);
printf(“\nwed=%d”,wed);
printf(“\nsat=%d”,sat);
}
Output:
sun =0
wed =3
sat =6