Computer Programming in C - Pratiksha A.
STRUCTURE AND UNION
Introduction to Structure:
- The structure in C is a user-defined data type that can be used to group items of possibly
different types into a single type.
- 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 of Structure Declaration:
A.
struct tag_name
{
datatype member1;
datatype member2;
…
};
a
struct tag_name variable_name1, variable_name2… -> to access the
members of the structure
sh
OR
struct tag_name variable_name1, variable_name2;
ik
Accessing member variables:
variablename.member_name;
at
Example:
#include <stdio.h>
#include <string.h>
Pr
// Define the structure
struct Person {
char name[50];
int age;
float height;
};
int main() {
// Declare a variable of type struct Person
struct Person person1;
// Assign values to the members of the structure
strcpy(person1.name, "John Doe");
1
Computer Programming in C - Pratiksha A.
person1.age = 30;
person1.height = 6.1;
// Access and print the values
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Height: %.1f feet\n", person1.height);
return 0;
}
A.
Introduction to Union:
- The Union is a user-defined data type in C language that can contain elements of the
different data types just like structure.
- But unlike structures, all the members in the C union are stored in the same memory
location.
- Due to this, only one member can store data at the given instance.
a
-
Syntax of Union:
sh
union union_name
{
datatype member1;
datatype member2;
…
ik
};
OR
union union_name variable1, variable2…
at
Accessing member variables:
variablename.member_name;
Pr
Example:
#include <stdio.h>
// union template or declaration
union un {
int member1;
char member2;
float member3;
};
int main()
2
Computer Programming in C - Pratiksha A.
// defining a union variable
union un var1,var2;
// initializing the union member
var1.member1 = 15;
var2.member2 = 'a';
printf("The value stored in member1 = %d",
var1.member1);
printf("\nThe value stored in member2 = %c",var2.member2);
A.
return 0;
}
a
sh
ik
at
Pr
3
Computer Programming in C - Pratiksha A.
Practical Questions:
1. WAP to do complex arithmetic operations using structures.
2. WAP for creating student data using structures.
3. WAP to create a Library Automation System using structures.
University Asked Questions:
Question & Question Paper Marks
A.
May’2018:
Q.1. b) State whether True or False:
vii) In a union, space is allocated to every member 1
individually.
Q.2. a) How to create array of structure variables and 5
a
assign values to its members.
Q.2. b) Differentiate between structure and union. When 5
sh
is union preferred over struct? Give one example of
each.
Dec’2018: 10
Q.3. b) Define a structure consisting of following
ik
elements:
1. student roll_no
2. student name
3. student percentage
at
Write a program to read records of 5 students and
display names.
May’2019: 4
Pr
Q.1. d) Differentiate between Structure and Union
Q.3. b) Explain the concept of nested structure? Declare 10
a structure to enter employee
Information like name, id, salary, date of joining. Use
nested structure to get the address
of an employee. Write a program to read 10 records and
display them.
Dec’2019:
Q.1. a) MCQ
vi) Which of the following cannot be a structure member? 1
4
Computer Programming in C - Pratiksha A.
(a) Another structure (b) Function (c) Array (d) None of
the mentioned
Q.5. b) b) Declare a structure to store the information of 10
10 cricketers.
i. Cricketer name
ii. Matches Played
iii. Runs Scored
iv. Strike rate
Use a function to display the cricketer information having
the maximum strike rate.
A.
May’2022:
Q.1. MCQ:
2) Collection of variables of same or different data types 2
is called as:
Option A: Array
a
Option B: Structure
Option C: Function
Option D: String
sh
Q.3. F) Write a program to read Title, Author and Price of 4
5 books using array of structures.
Display the records in ascending order of Price.
Q.4.F) Distinguish between Structure and Union with 4
ik
proper example.
Dec’2022: 5
at
Q.1. D) Differentiate between Structure and Union.
Q.4. A) WAP to store information of 10 students using 6
Structure. Information include Roll name, name, marks of
a student.
Pr
May’2023:
Q.1. E) Define Structure and explain the syntax of 5
declaration of Structure with example.
Q.6. B) Write a program to design a structure Employee 10
with members Employee No, Employee Name,
Experience and salary. Read the information of 100
employees and display employee information that is
having 5 years or more experience and salary less than
Rs. 10,000.