0% found this document useful (0 votes)
42 views6 pages

FYBCA Sem 2 C Lang Unit 3 - Structure and Union

Structures and unions allow users to define custom data types that can group different data types together. Structures define data types that allocate separate memory for each member, while unions only allocate memory for one member at a time. Both allow defining custom data types with members of various data types. They are declared with keywords like struct and union followed by the data type name and members. Structures are commonly used to group related data like student records with name, ID, marks etc, while unions provide an efficient way to use memory by allowing different interpretations of the same memory location.
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)
42 views6 pages

FYBCA Sem 2 C Lang Unit 3 - Structure and Union

Structures and unions allow users to define custom data types that can group different data types together. Structures define data types that allocate separate memory for each member, while unions only allocate memory for one member at a time. Both allow defining custom data types with members of various data types. They are declared with keywords like struct and union followed by the data type name and members. Structures are commonly used to group related data like student records with name, ID, marks etc, while unions provide an efficient way to use memory by allowing different interpretations of the same memory location.
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/ 6

Unit 3 –Structure and Union

What is a structure (struct)?


Structure (struct) is a user-defined data type in a programming language
that stores different data types' values together. The struct keyword is used
to define a structure data type in a program. The struct data type stores one
or more than one data element of different kinds in a variable.
Structure definition syntax:
struct_structure_name
{
data_type_variable_name;
data_type_variable_name;
........
data_type_variable_name;
};
Ex.
struct Empl
{
int emp_id;
float salary;
char designation[20];
}Empl e1;

Ex.
structmyStructure {
int myNum;
char myLetter;
}S1;

int main() {
structmyStructure S2;
return 0;
}
Example program:
//To show the memory allocation
#include<stdio.h>
struct student
{
int rollno;
int marks;
char name[10];
};
int main()
{
int size;
struct student s; //Declaring a structure variable
size = sizeof(s);

Prashant M. Savdekar Page 1


printf("\nSize of the Structure : %d", size);
return 0;
}
Output:
Size of the structure: 14
Point to understand:
Here,
Size of Structure 'S' = sizeof(roll) + sizeof(name) + sizeof(mark)
= 2 + 10 + 2 = 14
Example programs:
//To store the details of a student
#include<stdio.h>
struct student1
{
int rollno;
char name[10];
int marks;
};
void main()
{
int size;
struct student1 s={110,"Sarika",568};
printf("Roll no : %d\n", s.roll);
printf("Name : %s\n", s.name);
printf("Marks : %d\n", s.marks);
}
Output:
Roll no : 110
Name :Sarika
Marks : 568
//2. Input from user:
#include <stdio.h>
struct stud
{
int roll;
char name[50];
float marks;
} s;
void main()
{
printf("Enter the name of student: ");
scanf("%s", s.name);
printf("Enter his/her roll number: ");
scanf("%d", &s.roll);
printf("Enter his/her marks: ");
scanf("%f", &s.marks);

Prashant M. Savdekar Page 2


printf("Marks: %f\n", s.marks);
}
Output:
Enter the name of the student: Sagari
Enter his/her roll number: 6
Enter his/her marks: 780
Displaying the information:
Name: Sagari
Roll number: 6
Marks: 780

What Is a Union?
A Union is a type of data that is user-defined. It is just like the
structure. The Union combines various objects of different sorts and sizes
together. A user can define a Union using many members, but only one of
them holds a value at any given time.
It provides you with an efficient way of using a single memory location
for various purposes. Thus, varying objects can share a similar location.

Defining a Union – A user must deploy the union statement for defining a
Union. It determines a new data type that has more than one member for
the intended program. Here’s the format of a union statement:
Syntax of Declaring a Union
union [union name]
{
type member_1;
type member_2;
...
typemember_n;
};
union employee
{
string name;
int phone;
string email;
}e1;

Functions and Similarities Between Union and Structure in C


• They are both user-defined data types, and they store different sorts of
data together as a single unit.
• Both of their members can be any type of object. It may include
different structures and unions/ arrays. Its members can also contain
a bit field.

Prashant M. Savdekar Page 3


• A Union or a Structure can easily pass by value to functions and also
return to the value by functions. Every argument must possess the
same parameters as that of the function parameter.
• A Union or Structure passes by the value just like any scalar variable
in the form of a corresponding parameter.
• You can use the “.” operator for accessing the members.
Difference Between Structure and Union in C
Parameter Structure Union
Keyword A user can deploy the A user can deploy the
keyword struct to define a keyword union to define a Union.
Structure.
Internal The implementation of In the case of a Union, the
Implementation Structure in C occurs memory allocation occurs for only
internally- because it contains one member with the largest size
separate memory locations among all the input variables. It
allotted to every input member. shares the same location among
all these members/objects.
Accessing A user can access individual A user can access only one
Members members at a given time. member at a given time.
Syntax The Syntax of declaring a The Syntax of declaring a Union
Structure in C is: in C is:
struct [structure name] union [union name]
{ {
type element_1; type element_1;
type element_2; type element_2;
. .
. .
} variable_1, variable_2, …; } variable_1, variable_2, …;
Size A Structure does not have a A Union does not have a separate
shared location for all of its location for every member in it. It
members. It makes the size of a makes its size equal to the size of
Structure to be greater than or the largest member among all the
equal to the sum of the size of data members.
its data members.
Value Altering Altering the values of a single When you alter the values of a
member does not affect the single member, it affects the
other members of a Structure. values of other members.
Storage of Value In the case of a Structure, there In the case of a Union, there is an
is a specific memory location for allocation of only one shared
every input data member. Thus, memory for all the input data
it can store multiple values of members. Thus, it stores one
the various members. value at a time for all of its
members.
Initialization In the case of a Structure, a In the case of a Union, a user can

Prashant M. Savdekar Page 4


user can initialize multiple only initiate the first member at a
members at the same time. time.

C Nested Structure example

Let's see a simple example of the nested structure in C language.

#include <stdio.h>
#include <string.h>
struct Employee
{
int id;
char name[20];
struct Date
{
int dd;
int mm;
int yyyy;
}doj;
}e1;
void main( )
{
//storing employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
e1.doj.dd=10;
e1.doj.mm=11;
e1.doj.yyyy=2014;

//printing first employee information


printf( "employee id : %d\n", e1.id);
printf( "employee name : %s\n", e1.name);
printf( "employee date of joining (dd/mm/yyyy) : %d/%d/%d\n", e1.doj.dd
,e1.doj.mm,e1.doj.yyyy);
getch();
}

Output:

employee id : 101
employee name : SonooJaiswal
employee date of joining (dd/mm/yyyy) : 10/11/2014

Prashant M. Savdekar Page 5


Let's see an example of an array of structures that stores information
of 5 students and prints it.

#include<stdio.h>
#include <string.h>
struct student{
int rollno;
char name[10];
};
void main(){
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++){
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++){
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
getch();
}
Output:
Enter Records of 5 students
Enter Rollno:1
Enter Name:Sonoo
Enter Rollno:2
Enter Name:Ratan
Enter Rollno:3
Enter Name:Vimal
Enter Rollno:4
Enter Name:James
Enter Rollno:5
Enter Name:Sarfraz

Student Information List:


Rollno:1, Name:Sonoo
Rollno:2, Name:Ratan
Rollno:3, Name:Vimal
Rollno:4, Name:James
Rollno:5, Name:Sarfraz
===============================END===========================

Prashant M. Savdekar Page 6

You might also like