Structure in C with Example6 min read

In C programming, the structure is user-defined data types that allow us to store the combination of different data types together. All the different data type variables are represented by a single name.

As we have studied earlier that Array can hold only a similar type of variable in contiguous memory locations. Similarly, a structure can hold several data of various kinds in a contiguous memory location.

Why to use Structure?

Suppose, let say I want to store different attributes of different data types. For example, i want to store the information about the student which contain name(string), roll_no(int), address(string), Score(float), etc. All these are of different types. So to store these various kinds of attributes you need to create all these again and again for each student. And that would be very time-consuming and a headache to create the same variables again and again.

That is when structure comes into action. With the help of structure now I can create the members for the structure (members refers to the name, roll_no, address, etc.). And create the variables for each student. Go through the example below, you will understand.

Structures are variables that have several parts, each part of the object can have different types. Each part of the structure is called a member of the structure.


Syntax:

The keyword used to define the structure is struct.

Consider basic data of student: name, roll_no, address, score.
student structure is defined in the following way:


Declaring Structure variable

A structure variable is required to access the members of the structure. There are two ways to declare structure variable in C,

1st way:

Let say there are the following members of structure student.

And now to create a structure variable for the above student structure, the following line needs to be written inside the main function.

struct student s1, s2;

s1 and s2 are used to access the member of the student. These are like the objects for the student just like an object in C++.

2nd Way:

Another method is to declare the objects right after defining the structure as shown below.

Which one to use 1st way or 2nd way?

  1. Use the 1st way, if the number of variables is not fixed because creating the variable in the main() function gives you more flexibility.
  2. Use the 2nd way, if the number of variables that you need to use is fixed.

Scope:
A structure type declaration can be local or global, depending upon where the declaration is made.


Access members of a structure

There are two ways to access the members of the structure.

  1. – (Member or Dot operator)
  2. -> – (Structure pointer operator), discussed in the next.

1. – (Member or Dot operator)

Let say that I want to access the member name of s1 by (.) Dot operator. This can be done in the following way:

s1.name;

C Structure program

1. Example to store information for more than one student in a structure.

Output:

Student 1 Name: Sherlock Holmes
Student 1 Roll No: 101
Student 1 Address: 221B Baker Street
Student 1 Score: 91.10

Student 2 Name: James Moriarity
Student 2 Roll No: 102
Student 2 Address: 13 Street, Ireland
Student 2 Score: 89.05


2. Example to store information by taking user input and adding two numbers in structure in c.

Output:

Enter Student 1 Name:
Sherlock Holmes
Enter Student 2 Name:
Jim Moriarty

Enter Student 1 marks on Maths, Science and Chemistry respectively:
80.05 70 88.15
Enter Student 2 marks on Maths, Science and Chemistry respectively:
79.15 72.35 82

Sherlock Holmes scored the highest: 238.20


C – Use of typedef keyword in Structure

typedef in Structure is used to simplify the code to improve readability and confusion. In other words, it simplifies the syntax of structure

structure without the use of typedef,

Now let us see the structure syntax with the use of typedef,

As you can see, now stud is declared as the variables of struct student and with the help of stud variable, we can create the variables of type struct student.

Example of structure using typedef keyword:

Output:

Enter the name of the student: James Sebastian
Enter the roll no student: 18
Name of the student: James Sebastian
Roll Number of the Student: 18


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More