You are on page 1of 5

DEFINITION: Structure is a user-defined data type which holds together logically

related elements of either same or different datatypes


The general format of structure definition is
struct <struct name>
{
datatype member_1;
datatype member-2;
……………………..
………………….
};

New data type

Example: struct book


{
char name;
float price;
int pages;
};

Structure elements or members

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

struct book book1, book2, book3;


Or
struct book
{
char name;
float price;
int pages;
} book1, book2, book3

INITIALISATION

Structure variables can also be initialized where they are


declared.The format is
struct book
{
char name[20];
char author[20];
int pages;
float price;
};
struct book b1={“C programming”,” Dennis Ritchie”,250,315.45};
struct book b2 ={ “How to C program”,”Dietel&Dietel”,360,546};

ACCESSING STRUCTURE ELEMENTS

 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

Here is how we would assign values to the


members of book1.
strcpy(book1.name,”C how to program”)
strcpy(book1.author,”Dietel&Dietel”);
book1.pages = 456;
book1.price =28.35;

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;
};

void display(struct book);


void main()
{
struct book b;
clrscr();
printf("Enter the values\n");
scanf("%s%s%f",b.name,b.author,&b.price);
display(b); /*passing complete structre to display */
}
void display(struct book a)
{
printf("The details are \n");
printf("%s\t%s\t%f",a.name,a.author,a.price);
}
OUTPUT: Enter the values
Abc pqr 34.50

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

C provides a userdefined datatype called enumeration. Keyword is enum. This is a set of


integer constant represented by identifiers or variables.
 Syntax:
enum identifier{value1,value2,………valuen};
Consider the example of 7days of a week.

enum days{sun,mon,tue,wed,thu,fri,sat};

Value in an enum starts with 0 by default.

Note:The variables must be unique.


Integer constants are not permitted.

Eg:
enum days{sun=1,mon,tue,wed,thu,fri,sat};

/*prog to create enumerated datatype for 7 days.


Display their values in integers */

#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

You might also like