CPC - Module - 5
CPC - Module - 5
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
variablename.member_name;
at
Example:
#include <stdio.h>
#include <string.h>
Pr
int main() {
// Declare a variable of type struct Person
struct Person person1;
1
Computer Programming in C - Pratiksha A.
person1.age = 30;
person1.height = 6.1;
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
variablename.member_name;
Pr
Example:
#include <stdio.h>
int main()
2
Computer Programming in C - Pratiksha A.
A.
return 0;
}
a
sh
ik
at
Pr
3
Computer Programming in C - Pratiksha A.
Practical Questions:
A.
May’2018:
Q.1. b) State whether True or False:
vii) In a union, space is allocated to every member 1
individually.
a
assign values to its members.
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
May’2019: 4
Pr
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.
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.
Dec’2022: 5
at
May’2023:
Q.1. E) Define Structure and explain the syntax of 5
declaration of Structure with example.