0% found this document useful (0 votes)
8 views17 pages

Chapter-5 Struture, Union, Typedef, Enum (Certification)

Unit 5 covers structures, unions, typedefs, and enums in C programming. It explains how to declare and initialize structures, access their members, and the differences between structures and unions. Additionally, it discusses typedef for creating aliases for data types and enumerations for defining a list of constant values.

Uploaded by

f20220001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views17 pages

Chapter-5 Struture, Union, Typedef, Enum (Certification)

Unit 5 covers structures, unions, typedefs, and enums in C programming. It explains how to declare and initialize structures, access their members, and the differences between structures and unions. Additionally, it discusses typedef for creating aliases for data types and enumerations for defining a list of constant values.

Uploaded by

f20220001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Unit-5 Structure, Union, Typedef and Enum

5.1. Declaration of Structure

5.2 Declaration of Structure Variable

5.3 Accessing Structure Elements / Members

5.4 Initialization of Structure Members

5.5 Array of structure

5.6 Nested Structure

5.7 Union

5.8 Difference between Structure and Union

5.9 Typedef

7.10 Enum Data Type

7.1 Declaration of Structure

Definition / Feature of Structure

 Structure is a user-defined data type in C language which allows us to combine data of


different types together.

 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.

 Structure is a group of variables of different data types represented by a single name.

 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

C- Programming Unit-5 Structure & Union: Dr. Laxmikant Goud Page 1


such a big headache to store data in this way. We can solve this problem easily by using
structure. We can create a structure that has members for name, id, address and age and
then we can create the variables of this structure for each student.

Declaration of Structure / Defining a Structure / How to Create a Structure?

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

C- Programming Unit-5 Structure & Union: Dr. Laxmikant Goud Page 2


Here struct Student declares a structure to hold the details of a student which consists of 4 data
fields, namely name, age, branch and gender. These fields are called structure elements or
members.

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.

7.2 Declaration of Structure Variable

It is possible to declare variables of a structure, either along with structure definition or


separately after the structure is defined. Structure variable declaration is similar to the
declaration of any normal variable of any other datatype. Structure variables can be declared in
following two ways:

1) Declaring Structure variables separately after structure declaration.

struct Student
{
char name[25];
int rollno;
float per;
};
struct Student S1, S2; //declaring variables of struct Student

2) Declaring Structure variables along with structure definition

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

Structure members can be accessed and assigned values in a number of ways.

1. Using a dot . operator also called period or member access operator.

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;

C- Programming Unit-5 Structure & Union: Dr. Laxmikant Goud Page 4


printf("Name of Student: %s\n", s1.name); //displaying the stored values
printf("rollno of Student: %d\n", s1.rollno);
printf("Percentage of Student: %f\n", s1.per);
getch();
}
OUTPUT:
Name of Student: Rahul
rollno of Student: 18
Branch of Student: Computer

2. Using scanf() to take values from User.

In above program we can use scanf() to input structure member values from user.

scanf(" %s ", s1.name);

scanf(" %d ", &s1.age);

7.4 Initialization of Structure Members (Assigning Values to Members)


Structure Members can be initialized i.e assigning values to structure members by two ways:

1) All members assigned in one statement

Syntax:
struct struct_name var_name = {value for memeber1, member2,……….., memebern}

Example:
struct Student
{
char name[25];
int rollno;
char branch[30];
};

C- Programming Unit-5 Structure & Union: Dr. Laxmikant Goud Page 5


void main()
{
struct Student s1={“Rahul”,18,”Computer”}; // Initialization
printf("Name of Student: %s\n", s1.name); //displaying the stored values
printf("rollno of Student: %d\n", s1.rollno);
printf("Percentage of Student: %f\n", s1.branch);
getch();
}

2) Assigning/ Initializing Members by using dot operator

struct Student s1; //s1 is a variable of structure Student

s1.rollno = 18;

s1.per=80.50;

strcpy(s1.name, "Rahul"); // using string function to add name

Structure Examples Programs:

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

C- Programming Unit-5 Structure & Union: Dr. Laxmikant Goud Page 6


void main()
{
struct Student s1; //s1 is a variable of Student type and age is a member of Student
strcpy(s1.name, "Rahul Tiwale"); // using string function to add name
s1.rollno = 18;
s1.per=80.50;
printf("Name of Student: %s\n", s1.name); //displaying the stored values
printf("rollno of Student: %d\n", s1.rollno);
printf("Percentage of Student: %f\n", s1.per);
getch();
}

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

C- Programming Unit-5 Structure & Union: Dr. Laxmikant Goud Page 7


printf("\n Enter names, prices & no. of pages of book-1\n");
scanf("%s %f %d", &b2.name, &b2.price, &b2.pages);
printf("\n Enter names, prices & no. of pages of book-1\n");
scanf("%s %f %d", &b3.name, &b3.price, &b3.pages);
printf("\nBook Details are As Follows: \n");
printf("\n%s %f %d", b1.name, b1.price, b1.pages);
printf("\n%s %f %d", b2.name, b2.price, b2.pages);
printf("\n%s %f %d", b3.name, b3.price, b3.pages);
getch();
}

7.5 Array of Structure Variable

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;

C- Programming Unit-5 Structure & Union: Dr. Laxmikant Goud Page 8


int pages;

};

void main()

struct book b[3];

int i;

for(i=0;i<=2;i++) //0 1 2

printf("\n Enter names, prices & no. of pages of book\n");

scanf("%s %f %d", &b[i].name, &b[i].price, &b[i].pages);

printf("\nBook Details are As Follows: \n");

for(i=0;i<=2;i++)

printf("\n%s %f %d", b[i].name, b[i].price, b[i].pages);

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;

C- Programming Unit-5 Structure & Union: Dr. Laxmikant Goud Page 9


};

int main()

struct Employee emp[5];

int i;

for(i=0;i<=4;i++)

printf("Enter Employee name and Salary:\n");

scanf("%s %d", emp[i].ename,&emp[i].sal);

for(i=0;i<=4;i++)

printf("%s %d\n", emp[i].ename,emp[i].sal);

return(0);

7.6 Nested Structure (Structure within Structure)

Nested structures means, that one structure has another structure as member variable.

A Structure can be declared within another structure as follows:

struct Student

char name[30];

int age;

C- Programming Unit-5 Structure & Union: Dr. Laxmikant Goud Page 10


struct Address

char city[20];

long int pincode;

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

C- Programming Unit-5 Structure & Union: Dr. Laxmikant Goud Page 11


1. Union Declaration:

Syntax:

union name

datatype variablename;

datatype variablename;

-----

}union variables;

Example:

union Student

int rollno; // 2 bytes

float per; // 4 bytes

char name[30]; //30 bytes

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

Memory allotted for union is equal to the space required by the


largest member of the union.
C- Programming Unit-5 Structure & Union: Dr. Laxmikant Goud Page 12
The Memory space required by the union variable S1 is 30 bytes as the space of its largest data
member i.e the member name which requires 30 bytes.

2. Union Variable Declaration

Syntax:

Union unionname variablename;

Example:

Union student S1;

3. Union Program 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);

C- Programming Unit-5 Structure & Union: Dr. Laxmikant Goud Page 13


printf("Percentage of Student: %f\n", s1.per);
getch();
}

7.8 Difference between Structure and Union

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.

Following is the general syntax for using typedef,

Syntax:

typedef <existing_name> <alias_name>

Example:

typedef unsigned long ulong;

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.

Use of typedef with structure

typedef can be used to give a name to user defined data type as well. Lets see its use with
structures.

typedef struct employee

char name[50];

int salary;

}emp;

void main( )

emp e1;

printf("\nEnter Employee record:\n");

printf("\nEmployee name:\t");

scanf("%s", e1.name);

printf("\nEnter Employee salary: \t");

scanf("%d", &e1.salary);

printf("\nstudent name is %s", e1.name);

printf("\nroll is %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.

C- Programming Unit-5 Structure & Union: Dr. Laxmikant Goud Page 15


7.10 Enum Data Type
 Enumeration (or enum) is a user defined data type in C. It is mainly used to assign list
of values to a data 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.

 These list of values are called as elements of enum.

 Internally Elements are stored as integer constant.

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.

 i.e jan=0, feb=1,march=2,april=3,may=4,june=5.

 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.

Program on Enum Data Type:

#include <stdio.h>

void main()

enum week {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

enum week today; // creating today variable of enum week type

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.

C- Programming Unit-5 Structure & Union: Dr. Laxmikant Goud Page 17

You might also like