Chapter-5 Struture, Union, Typedef, Enum (Certification)
Chapter-5 Struture, Union, Typedef, Enum (Certification)
5.7 Union
5.9 Typedef
It is somewhat similar to an Array, but an array holds data of similar type only whereas
structure can stores data of different data types, which is practical more useful.
Example: we need to store the data of students like student name, age, address, id etc.
One way of doing this would be creating a different variable for each attribute, which is
We use struct keyword to create a structure in C. The struct keyword is a short form
of structured data type.
Syntax:
struct struct_name {
DataType member1_name;
DataType member2_name;
DataType member3_name;
};
Here struct_name can be anything of your choice. Members data type can be same or different
data type. Once we have declared the structure we can use the struct name as a data type like
int, float etc.
Example of Structure
struct Student
{
char name[25];
int rollno;
float per;
};
Each member can have different datatype, like in this case, name is an array of char type
and age is of int type etc. Student is the name of the structure.
struct Student
{
char name[25];
int rollno;
float per;
};
struct Student S1, S2; //declaring variables of struct Student
struct Student
{
char name[25];
int rollno;
float per;
}S1, S2;
Here S1 and S2 are variables of structure Student.
C- Programming Unit-5 Structure & Union: Dr. Laxmikant Goud Page 3
7.3 Accessing Structure Elements/Members
To access any member of a structure, we use the member access operator (.). The member
access operator is coded as a period between the structure variable name and the structure
member that we wish to access.
Syntax:
struct var_name.member1_name;
struct var_name.member2_name;
Example:
#include<stdio.h>
#include<string.h>
struct Student
{
char name[25];
int rollno;
float per;
};
int main()
{
struct Student s1; //s1 is a variable of Student type and age is a member of Student
strcpy(s1.name, "Rahul"); // using string function to add name
s1.rollno = 18;
s1.per=80.50;
In above program we can use scanf() to input structure member values from user.
Syntax:
struct struct_name var_name = {value for memeber1, member2,……….., memebern}
Example:
struct Student
{
char name[25];
int rollno;
char branch[30];
};
s1.rollno = 18;
s1.per=80.50;
1. Write a program to declare a structure student having data members as rollno, name
and percentage. Accept and display data of student.
#include<stdio.h>
#include<string.h>
struct Student
{
char name[25];
int rollno;
float per;
};
2. Write a program to declare a structure book having data members as name,pages and
price. Accept this data for 3 books and display it.
#include<stdio.h>
#include<conio.h>
struct book
{
char name[30];
float price;
int pages;
};
void main()
{
struct book b1, b2, b3 ;
printf("\n Enter names, prices & no. of pages of book-1\n");
scanf("%s %f %d", &b1.name, &b1.price, &b1.pages);
We can also declare an array of structure variables. In which each element of the array will
represent a structure variable.
Consider a situation where details of 100 books need to store. At the time declaring 100
structure variable is very tedious task and not a good idea so we can use array of structure
variables:
Example:
1. Write a program to declare a structure book having data members as name,pages and
price. Accept this data for 3 books and display it.
#include<stdio.h>
#include<conio.h>
struct book
char name[30];
float price;
};
void main()
int i;
for(i=0;i<=2;i++) //0 1 2
for(i=0;i<=2;i++)
getch();
2. Write a program to declare a structure Employee having data members as ename and
salary. Accept this data for 5 employees and display it.
#include<stdio.h>
struct Employee
char ename[10];
int sal;
int main()
int i;
for(i=0;i<=4;i++)
for(i=0;i<=4;i++)
return(0);
Nested structures means, that one structure has another structure as member variable.
struct Student
char name[30];
int age;
char city[20];
}addr;
}s1;
Here structure Address declares inside the structure Student so members of Address can be
accessed using Student variable.
The structure Address holds city, pincode members and these will access by using student
structure variable as follows:
s1.Address.pincode=413512;
strcpy(s1.Address.city,”Latur”);
s1.age=20;
strcpy(s1.name,”Amey Joshi”);
7.7 Union
A union is a special data type that stores different data types in the same memory location. You
can define a union with many members, but only one member can contain a value at any given
time. So a Union variable can store only one data member at a time.
The memory space required to store one variable of union is equal to the memory space
required by the largest element in the union.
Syntax:
union name
datatype variablename;
datatype variablename;
-----
}union variables;
Example:
union Student
}S1;
Now, a variable S1 of union Student can store an integer, a floating-point number, or a string of
characters. It means a single variable, i.e., same memory location, can be used to store multiple
types of data. So it store only one value at a time.
Here memory space required for data members of union are rollno is requires 2 bytes, per
requires 4 bytes, and name requires 30 bytes.
Syntax:
Example:
#include<stdio.h>
#include<string.h>
union Student
{
char name[25];
int rollno;
float per;
};
int main()
{
union Student s1;
strcpy(s1.name, "Rahul Tiwale");
s1.rollno = 18;
s1.per=80.50;
printf("Name of Student: %s\n", s1.name);
printf("rollno of Student: %d\n", s1.rollno);
7.9 Typedef
typedef is a keyword used in C language to assign alternative names to existing
datatypes.
It is used to give new symbolic name for the existing name in c program.
It’s mostly used with user defined datatypes, when names of the datatypes become
slightly complicated to use in programs.
Syntax:
Example:
ulong i, j;
C- Programming Unit-5 Structure & Union: Dr. Laxmikant Goud Page 14
The above statement defines a term ulong for an unsigned long datatype. Now this ulong
identifier can be used to define unsigned long type variables. Here i and j are unsigned long
variables defined by using typedef name ulong.
typedef can be used to give a name to user defined data type as well. Lets see its use with
structures.
char name[50];
int salary;
}emp;
void main( )
emp e1;
printf("\nEmployee name:\t");
scanf("%s", e1.name);
scanf("%d", &e1.salary);
In above example emp represents the stucture definition associated with and this emp can be
used to declare a variable of this stucture type.
The list of values should be already known and cannot be input by the user.
The enum is used to define list of values that will be accessed by using enum data type.
Example:
enum months{jan,feb,march,april,may,june};
Here we defined enum months which contain 12 elements. These elements are stored as
integer constant.
The first element of months jan was assigned the integer value 0, second element feb
with integer value 1 and so on dec with integer value 11.
This ordering of enum elements can be changed with the help of equal to sign as
follows:
enum months{jan=1,feb=2,march=3,april,may,june};
The value of next element in the list is previous value plus one.
#include <stdio.h>
void main()
today = Wednesday;
printf("Day= %d",today+1);
C- Programming Unit-5 Structure & Union: Dr. Laxmikant Goud Page 16
getch();
OUTPUT:
Day=4
In above program enum week has 7 elements internally Sunday has value 0, Monday has value
1, so on Saturday has value 6.
In above program today is the variable of enum having value Wednesday i.e internally today
stores integer value 3, so in printf today+1 value is 4.