4th unit pdf
4th unit pdf
Define structure : structure is a user – defined data type in c which allows yo to combine different data
types to store a particular type of record.
Structure help o construct a complex data type in more meaningful way it is somewhat similar to any
array. The only difference is that array is used to store collection of similar data types while structure
can store collection of any type of data.
Structure is used to represent a record. suppose you want to store record of student which consists of
student name, address,, roll number and age you can define a structure to hold this information.
The struct keyword is used to define the structure in the C programming language. The items in the
structure are called its member and they can be of any valid data type.
Syntax:
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
};
Example :
Struct Book
Char name[15];
Int price;
Int pages;
};
Method of declaring a structure variable: Its is possible to declare variables of a
structure,after the structure is defined a structure variable declaration is similar to the
declaration o variables of any data types.
Char[20] name;
Int age;
};
Char[20] name;
Int age;
}s1,s2;
Like any other data type,sturcure variabke can also be initialized at compile time there are two
ways to initialize a structure.
Struct patient
Float height;
Int weight;
Int age;
};
P1.height=180.75;
P1.weight = 79;
P1.age = 23;
To access members of a structure member access operator is used which is also called as dot
operator represented as. The structure variable will access the structure members with help of
this member access operator.
Syntax:
<structure_variable>.<structure_member>;
Example:
Struct Book;
Char name[15]
Int price;
Int pages;
}b1,b2;
Printf(“%d”,b1.price);
Here is b1 is a variable of type books and b1 is used to access the structure variable using dot
operator.b1.price refer to price value of structure variable b1.
We can also use printf() to print values of structure variable to the console.
Print(“%s”,b1.name);
Printf(“%d”,b1.price);
Structure with a program to read and print a book database consisting of title of
book,Author,No.of.pages,Price as fields.
#include<stdio.h>
#include<string.h>
#define SIZE 20
Strcutbookdetail
Char name[20];
Char author[20];
Int pages;
Float price;
};
Void main()
Structbookdetail b[SIZE];
Int num,I;
Scanf(“%d”,&num);
Printf(“\n”);
For(i=0;i<num;i++)
{
Printf(“\t=:Bookd %d Detail:=\n”,i+1);
Scanf(“%s”,b[i].name);
Scanf(“%s”,b[i].author);
Scanf(“%d”,&b[i].pages);
Scanf(“%f”,&b[i].price);
Output(b,num);
Int I,t=1;
for(i=0i<n;i++,t++)
Printf(“\n”);
Printf(“Books No.%d\n”,t);
Output:
Size of a Structure :
As structures contain different data types in it size of every structure will vary. To know the size
of a structure, the programmer can use the size of operator to find its size the size of operator
will not only determine the size of a structure but it can determine size of any variable such as
of built in data types. size of is not a function but it is an operator in c The size of structure in
different computer, may vary depending upon its representation.
Syntax :
Sizeof(<variable_name>);
Union
Union is derived data type contains collection of different data type or dissimilar elements. All definition
declaration of union variable and accessing member is similar to structure, but instead of keyword struct
the keyword union is used, the main difference between union and structure is
Each member of structure occupy the memory location, but in the unions members share memory.
Union is used for saving memory and concept is useful when it is not necessary to use all members of
union at a time. Where union offers a memory treated as variable of one type on one occasion where
(struct), it read number of different variables stored at different place of memory
Syntax of union:
union student
datatype member1;
datatype member2;
};
Like structure variable, union variable can be declared with definition or separately such as
Datatype member1;
}var1;
Struct Union
The struct keyword is used to define a structure. The union keyword is used to define union.
When the variables are declared in a structure, When the variable is declared in the union, the
the compiler allocates memory to each variables compiler allocates memory to the largest size
member. The size of a structure is equal or variable member. The size of a union is equal
greater to the sum of the sizes of each data to the size of its largest data member size.
member.
Each variable member occupied a unique Variables members share the memory space
memory space. of the largest size variable.
Changing the value of a member will not affect Changing the value of one member will also
other variables members. affect other variables members.
Each variable member will be assessed at a Only one variable member will be assessed at
time. a time.
We can initialize multiple variables of a structure In union, only the first data member can be
at a time. initialized.
All variable members store some value at any Exactly only one data member stores a value
point in the program. at any particular instance in the program.
The structure allows initializing multiple variable Union allows initializing only one variable
members at once. member at once.
It is used to store different data type values. It is used for storing one at a time from
different data type values.
It allows accessing and retrieving any data It allows accessing and retrieving any one
member at a time. data member at a time.