Structure And Union

PropellerAds

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 struct_name
 {
 data _type member_name1;
 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.
member_name;

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)

Try Now – Programming In C MCQs