Professional Documents
Culture Documents
Unit - V
Unit - V
UNIONS
Structures
Definition:
A structure is a collection of one or more
variables of different data types grouped
together under a single name.
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.
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:
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.
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>