Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 32

STRUCTURES AND

UNIONS
Structures
Definition:
A structure is a collection of one or more
variables of different data types grouped
together under a single name.

It is an user defined data type

Features:
It is possible to copy all elements of one
structure variable to another.
Nesting of structure is possible
It is possible to pass structure elements to
a function
It is possible to create structure pointers
Need for Structures
Arrays are used to store large set of data and
manipulate them.
But the disadvantage is that all the elements
stored in an array are to be of the same data
type.
When we required to use a collection of different
data items of different data types we can use a
structure.
Structure is a method of packing data of
different types.
A structure is a convenient method of handling a
group of related data items of different data
types.
Defining a structure
The general form of a structure is
struct tag_name
{
data_type variable1;
data_type variable2;


};
Where tag_name is the name of the structure
Example:
Defining a structure
struct books
{
char title[20];
char author[15];
int pages;
float price;
};
the keyword struct declares a structure to holds the details
of four fields namely title, author, pages and price.

These are members of the structures.

Each member may belong to different or same data type.


Structure declaration &
Declaration: initialization
We can declare structure variables using
the tag name any where in the program.
For example the statement,
struct books b1,b2,b3;
declares b1,b2,b3 as variables of type
struct books

Initialization:
struct books b1={C
Programming,Balagurusamy,700,350.00);
We can also combine both definition and
declaration in one statement,
struct
struct books {
{ char title[20];
char author[15];
char title[20]; int pages;
char author[15]; float price;
int pages; } b1,b2,b3;
float price; is also valid.
} b1,b2,b3;

is valid.
Assigning values to members:

The link between a member and a variable is


established using the member operator . which
is known as dot operator or period operator.
Example:
b1.price
is the variable representing the price of b1 and
can be treated like any other ordinary variable.
We can use scanf statement to assign values
like
scanf(%s,book1.title);
scanf(%d,& book1.pages);
Structures
Or we can assign variables to the
members of b1
strcpy(b1.title, C Programming);
strcpy(b1.author,Balagurusamy);
b1.pages=700;
b1.price=350.00;
/* Example program for using a
#include <stdio.h>
struct student
structure*/
OUTPUT
{
int id_no; Enter the student information
char name[20]; Enter the student id_no:111
char address[20]; Enter the name of the
}s; student:Syed
void main() Enter the address of the
{
student:Salem
printf(Enter the student information\n);
printf(Enter the student id_no:);
STUDENT INFORMATION
scanf(%d,&s.id_no); Student id_number=111
Student name=Syed
printf(Enter the name of the student:);
scanf(%s,s.name); Student address=salem
printf(Enter the address of the student:);
scanf(%s,s.address);
printf(STUDENT INFORMATION\n);
printf(Student id_number=%d\n,s.id_no);
printf(Student name=%s\n,s.name);
printf(Student address=%s\n,s.address);
}
Structures within Structures
struct student
{
int id_no;
char name[20];
char dept[3];
int age;
struct date
{
int day;
int month;
int year;
}dob;
}s1;

printf(%d %s %s %d %d %d %d,s1.id_no,s1.name,s1.dept,s1.age,
s1.dob.day,s1.dob.month,
s1.dob.year);

Unions
Like structures union contain members whose individual data types may
differ from one another.

All the members of union shares the same storage area whereas each
member of a structure is assigned its own unique storage area.

Like structures union can be declared using the keyword union as


follows:
union item
{
int m;
float p;
char c;
} code;
this declares a variable code of type union item.

Major difference between structure and union is memory allocation. In


structure, size of structure refers combined size of all elements whereas
in union it is size of element whose size is maximum
Unions
However we can use only one element at a time, i.e., code.m or code.p
or code.c

During accessing we should make sure that we are accessing the


member whose value is currently stored. For example a statement such
as
code.m = 456;
printf(%d,code.m);
code.p = 456.78;
printf(%f,code.p);
code.c = A;
printf(%c,code.c);
Unions Vs Structures - Difference with
#include<stdio.h example
void main()
> {
struct s struct s s1;
{
union u u1;
char c;
long l;
char p[20]; s1.c = 1;
}; s1.l = 2L;
union u s1.p = "This is structure";
{
char c; u1.c = 1;
long l; u1.l = 2L;
char p[20]; u1.p = "This is union";
};
printf(s1: %d %ld
Output: %s\n,s1.c,s1.l,s1.p);
s1: 1 2 This is structure printf(u1: %d %ld
u1: -72 184 This is union %s\n,u1.c,u1.l,u1.p);
}
Storage Classes
The scope and lifetime depends on the storage class of the
variable in C language.

The area or block of the C program from where the variable


can be accessed is known as the scope of variable

The lifetime of the variable retains a given value during the


execution of the program.

The variables can be any one of the four storage classes:


1. Automatic Variables
2. External variable
3. Static variable
4. Register variable.
Automatic Variables
Automatic variables are declared inside a
particular function and they are created
when the function is called and
destroyed when the function exits.

Automatic variables are local or private


to a function in which they are defined.

By default all variables declared without


any storage specification is automatic.
Automatic Variables Example
#include<stdio.h> void function2()
void function1(); {
void function2();
void main() int m=100;
{ function1();
int m=1000; printf(%d\n,m);
function2();
}
printf(%d\n,m);
} Output
void function1()
{ 10
int m=10; 100
1000
printf(%d\n,m);
}
External Variables
Global variables are usually declared in the
beginning of the main program ie., before
the main() program

However c provides a facility to declare any


variable as global and this is possible by
using the keyword storage class extern.

The external declaration does not allocate


storage space for the variables.

If it has to be used in other functions then


again it has to be re-declared in that
function also.
External Variables Example
Example:
File2 (add.h)
File1 (ex.c) extern int a;
#include add.h void add()
int a; {
void main() printf(XXX);
a+=150;
{ }
a=10;
printf(%d,a); Output
add();
10
printf(%d,a); XXX
} 160
Static Variables
The value given to a variable declared by
using keyword static persists until the
end of the program.

A static variable is initialized only once,


when the program is compiled. It is never
initialized again.
Static variables Example
#include<stdio.h>
void main()
Output:
{
int j; 1
for(j=1;j<=3;j++) 2
stat(); 3
}

void stat()
{
static int x;
x=x+1;
printf(x=
%d\n,x);
}
Preprocessor Directives
The preprocessor is a program that processes
the source program before it is passed to the
compiler.
The preprocessor directives are always
preferably initialised at the beginning of the
program before the main(). It is begins with a
symbol #(hash)

#define directive
#undef directive
#include directive
#ifdef directive Conditional compilation
#ifndef directive directives
#error directive
#define directive
The syntax of #define directive is as follows
#define identifier substitute
or
#define identifier(arg1argN) substitute
Ex:
#define PI 3.14
void main()
{ OUTPUT
float r,area;
printf(Enter radius:); Enter radius:7
scanf(%f,&r);
Area=153.86
area=PI*r*r;
printf(Area=%.2f,area);
}
#define directive example
#include<stdio.h>
#include<conio.h>
OUTPUT
#define PI 3.14 Enter the radius of the circle:7
void main() The circumference of the circle is
{ 43.96
int r; The area of the circle is 153.86
float cir,area;
printf(Enter the radius of the circle:");
scanf("%d",&r);
cir=2*PI*r;
area= PI *r*r;
printf("The circumference of the circle is %0.2f",cir);
printf(The area of the circle is %0.2f",area);
getch();
}
#undef directive
The syntax of #undef directive is as follows
#undef identifier_macro_template substitute
Ex:
#define PRINT printf(Hello\n)
void main()
{
int i;
PRINT;
OUTPUT
#undef PRINT printf(Hello\n)
PRINT; Error
}
#include directive
The #include directive loads the specified file in the
current program
The syntax of #include directive is as follows
#include filename
#include <filename>

The file is included in the double quotations marks


indicating that the search for the file is made in the
current directory and in the standard directories
#include abc.h
The file is included in the angle brackets indicating that
the search for the file is made only in the standard
directories
#include <stdio.h>
#ifdef directive
The syntax of #ifdef directive is as follows
#ifdef identifier
{
statement1;
statement2;
}
#else
{
statement1;
statement2;
}
#endif
#ifdef directive example
Ex: OUTPUT
#define LINE 1
void main() This is line number
{ one
clrscr();
#ifdef LINE
printf(This is line number one);
#else
printf(This is line number not one);
#endif
getch();
}
#ifndef directive
The syntax of #ifndef directive is as follows
#ifndef identifier
{
statement1;
statement2;
}
#else
{
statement1;
statement2;
}
#endif
#ifndef directive example
Ex: OUTPUT
#define LINE 1
void main() This is line number
{ one
clrscr();
#ifndef LINE
printf(This is line number not one);
#else
printf(This is line number one);
#endif
getch();
}
#error directive
The #error is used to display user defined message
during
compilation of the program

The syntax is as given below

#if !defined (identifier)


#error <ERROR MESSAGE>
#endif
#error directive
Ex:
OUTPUT
#define B 1
void main()
MACRO A IS NOT DEFINED
{
clrscr();
#if !defined (A)
#error MACRO A IS NOT DEFINED
#else
printf(Macro found);
#endif
getch();
}

You might also like