Structures in C Programming
Structure is a collection of heterogeneous (i.e. of different types) data items which are referenced by a single name.
In C programming language, using structure, we can create and use data types other than the basic or fundamental data types. These are known as user defined data types. Structures are created by using struct
keyword and can be defined as user defined data types.
Declaration of Structure & Structure Variables
Syntax for Declaring Structure
struct structure_name
{
data_type member1;
data_type member2;
data_type member3;
.
.
.
data_type memberN;
};
Where structure_name and member can be any valid C identifier, data type can be any valid C data types. Structure name is also known as tag. When we declare a structure, new data type is defined. After declaring structure, structure variables can be created by using keyword struct and structure_name.
Syntax for Creating Structure Variables After Declaring Structure
struct structure_name var1, var2, var3, …., varN;
Example: Declaration & Creation of Structure Variables
Let's create structure named student with member name, roll & marks:
struct student
{
char name[30];
int roll;
float marks;
};
In this example, student is name of structure and it has three members, namely, name, roll and marks. Now struct student
is name of new data type which can be used to create new structure variables.
For Example:
struct student s1, s2, s3;
In the above example s1, s2, s3 are structure variables.
Structure variables can also be created at the time of structure declaration.
General syntax for declaring structure variables at the time of structure declaration is:
struct structure_name
{
data_type member1;
data_type member2;
data_type member3;
.
.
.
data_type memberN;
} var1, var2, var3, …., varN;
For Example
struct student
{
char name[30];
int roll;
float marks;
}s1, s2, s3;
Initialization of Structure Variables
Assigning value to the structure variable at the time declaration of structure variable is known as initialization of structure variables. Structure variables can be initialized by using the notation used for array initialization i.e. using {value 1, value 2, ..., value N}
.
Example: Initialization of Structure Variables
If structure is declared like this:
struct student
{
char name[30];
int roll;
float marks;
};
Then structure variables can be created and initialized like this:
struct student s1 = {“Parijat”, 13, 80.5};
struct student s2 = {“Suyogbir”, 17, 60};
struct student s3 = {“Sakambari”, 19, 50.5};
Accessing Structure Elements
Individual structure elements can be accessed by using the .
(dot) operator along with structure variables.
The general syntax for accessing structure variables is:
structure_variable.member_name
Example: Accessing Structure Elements
If structure is declared like this:
struct student
{
char name[30];
int roll;
float marks;
};
And, structure variables are created and initialized like this:
struct student s1 = {“Parijat”, 13, 80.5};
struct student s2 = {“Suyogbir”, 17, 60};
struct student s3 = {“Sakambari”, 19, 50.5};
Then, individual value can be obtained as:
s1.name gives “Parijat” s1.roll gives 13 s1.marks gives 80.5 similarly, s2.name gives “Suyogbir” s2.roll gives 17 s2.marks gives 60
Structure Examples
So far so good! Now let's look at the complete C program for structures.
Example 1: C program to illustrate declaration and initialization of structure
#include<stdio.h>
/* Declaration of structure */
struct student
{
char name[30];
int roll;
float marks;
};
int main()
{
/* Declaration and initialization of structure variable */
struct student s1 = {"Parijat", 17, 80.5};
printf("Student detail is:\n");
printf("Name : %s\n", s1.name);
printf("Roll : %d\n", s1.roll);
printf("Marks : %f\n", s1.marks);
return 0;
}
The output of the above program is:
Student detail is: Name : Parijat Roll : 17 Marks : 80.500000
Example 2: C program to illustrate reading data in structure variables and displaying the content
#include<stdio.h>
/* Declaration of structure */
struct student
{
char name[30];
int roll;
float marks;
};
int main()
{
/* Declaration of structure variable */
struct student s1;
printf("Enter name, roll and marks of student:\n");
scanf("%s%d%f",s1.name, &s1.roll, &s1.marks);
printf("Student detail is:\n");
printf("Name : %s\n", s1.name);
printf("Roll : %d\n", s1.roll);
printf("Marks : %f\n", s1.marks);
return 0;
}
The output of the above program is:
Enter name, roll and marks of student: Suyogbir ↲ 18 ↲ 56.5 ↲ Student detail is: Name: Suyogbir Roll: 18 Marks: 56.500000 Note: ↲ represents ENTER key is pressed.
Recommended Readings
Never stop learning! See these C programming examples to know more about structures.