Short Notes: Structure & Union | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE) PDF Download

Download, print and study this document offline
Please wait while the PDF view is loading
 Page 1


STRUCTURE AND UNION
Definition
A Structure is a user defined data type that can store related information together. The variable 
within a structure are of different data types and each has a name that is used to select it from the 
structure. C arrays allow you to define type of variables that can hold several data items of the 
same kind but structure is another user defined data type available in C programming, which 
allows you to combine data items of different kinds.
Structures are used to represent a record, Suppose you want to keep track of your books in a 
library. You might want to track the following attributes about each book:
• Title
• Author
• Subject
• Book ID
Structure Declaration
It is declared using a keyword struct followed by the name of the structure. The variables of the 
structure are declared within the structure.
Example:
Struct struct-name
{
data type var-name; 
data type var-name;
};
Page 2


STRUCTURE AND UNION
Definition
A Structure is a user defined data type that can store related information together. The variable 
within a structure are of different data types and each has a name that is used to select it from the 
structure. C arrays allow you to define type of variables that can hold several data items of the 
same kind but structure is another user defined data type available in C programming, which 
allows you to combine data items of different kinds.
Structures are used to represent a record, Suppose you want to keep track of your books in a 
library. You might want to track the following attributes about each book:
• Title
• Author
• Subject
• Book ID
Structure Declaration
It is declared using a keyword struct followed by the name of the structure. The variables of the 
structure are declared within the structure.
Example:
Struct struct-name
{
data type var-name; 
data type var-name;
};
Structure Initialization
Assigning constants to the members of the structure is called initializing of structure.
Syntax:
struct structname 
{
data type membernamel;
data type member_name2;
} struct_var={constant1,constant2};
Accessing the Members of a structure
A structure member variable is generally accessed using a ‘.’ operator.
Syntax: strcut_var. 
membername;
The dot operator is used to select a particular member of the structure. To assign value to the 
individual
Data members of the structure variable stud, we write,
stud.roll=01;
stud.name= ” Rahul” ;
To input values for data members of the structure variable stud, can be written as,
scanf(“%d” , &stud.roll); 
scanf(‘ ’ %s” , &stud.name);
To print the values of structure variable stud, can be written as: 
printf(“%s” ,stud. roll); 
printf(“%f” ,stud. name);
Read More
90 docs

Top Courses for Computer Science Engineering (CSE)

Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

mock tests for examination

,

past year papers

,

Exam

,

Viva Questions

,

Free

,

Short Notes: Structure & Union | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

Important questions

,

Extra Questions

,

Short Notes: Structure & Union | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

practice quizzes

,

study material

,

video lectures

,

pdf

,

Short Notes: Structure & Union | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

MCQs

,

Summary

,

shortcuts and tricks

,

ppt

,

Semester Notes

,

Sample Paper

,

Objective type Questions

,

Previous Year Questions with Solutions

;