C Structures

In this tutorial you will learn about the C Structures and its application with practical example.

In C Programing Language we can use arrays when we want to hold multiple elements of the homogeneous data type in a single variable, but what if we want to hold heterogeneous data type elements, using the c structures you can wrap one or more variables that may be in different data types into one.

Syntax:

To define a structure, you use the struct keyword followed by the structure name. The variables which are declared inside the structure are called ‘members of structure’.

Example:

The student structure contains id as a positive integer, name as a string, percentage code as afloat.

Declaring structure

The above example only defines a student structure without creating any structure variable. To declare a structure variable, you can do it in two ways:

Declaring structure with variables together:

Declaring the C structures variable after you define the structure:

Note:– When declaring structure following points need to be kept in mind –

  • The structure is always terminated with semicolons (;).
  • Structure name as struct_name can be later used to declare c structures variables of its type in a program.

Accessing Structure Members

Member operator ‘.’ is used to accessing the individual structure members. It is also known as ‘dot operator’ or ‘period operator’.

Syntax:

Example Program:

In this tutorial we have learn about the C Structures and its application with practical example. I hope you will like this tutorial.