Structure in C programming

In this tutorial, we will learn about the ‘structure in C’  with detailed definition and example.

A structure is a collection of one or more variables of either same or different types grouped under common single name.  Structures help to organize complicated data, particularly in large programs as they allow related variable to be treated as a unit.

A structure is declared using the keyword struct.

Syntax of Structure

struct structure_name 
    data_type member1;
    data_type member2;
    data_type memeber;
};  // semicolon is must in ending line.

For example: Creating structure of a Student .

struct Student{
   int roll;
   char name[40];

Here, variable roll and name are called members of the structure Student.

We can also declare variable for student as :

struct Student{
   int roll;
   char name[40];
} s1, s2, s3; //variables of type Student.

Defining a Structure

Above defined Student structure can be defined as :

struct Student = { 10, "liveoncode" };

Here, roll is assigned value 10 and name is assigned liveoncode

Accessing Structure Variable

We can access variable of structure using member operator ( . ).

struct Student s1;
s1.roll = 10; = "liveoncdoe";

It is the alternative way of initializing structure.

Example of structure in C

#include <stdio.h>
#include <string.h>
struct Student 
           int roll;
           char name[40];
int main() 
           struct Student record = {0}; //Initializing to null
           strcpy(, "liveoncode");
           printf(" Roll is: %d \n",;
           printf(" Name is: %s \n",;
           return 0;


Roll is: 10
Name is: liveoncode

Typedefs in C

It is the means of creating new name for existing data type. For example:

typedef int Roll;

Here, by usind keyword typedef, the Roll  can be used in place of int.

Roll length;
Roll breadth;

Here, length and breadth are the variable of type int.

In structure,

struct Student{
   int roll;
   char name[40];
} std;

int main(){
    std s1;

Here, typedef keyword is used in creating a type s1. i.e struct Student.