Nested structure in C programming

We can declare a structure inside another structure which is called nested structure in C language. C provides the facility to nesting a structure inside another. In this guide we will learn more about nested structure in C.

We can declare nested structure in C by 2 way.

  • Creating separate structure
  • Creating embedded structure

Creating separate structure in C

Let’s have a look at the following structure declaration where we have written the dependent structure inside the main structure as a member of independent structure.

// Creating separate structure in C

struct date_of_joining{
  int dd;
  int mm;
  int yyyy;

struct employee_struct{
  char name[20];
  int id;
  struct date_of_joining dt;   // nested structure

Here, dt is a variable of type date_of_joining and we can use it as a member of other structure. Like this we can use date_of_joining structure inside many structure.

Creating embedded structure in C

In embedded structure we will declare a structure inside another structure. Declaring a structure inside another is called embedded structure. Consider the given example here.

// Creating embedded structure in C

struct employee_info{
    int id;
    char name[30];
    struct date_of_joining{    // nested structure
      int dd;
      int mm;
      int yyyy;

Here, the structure dt is a nested structure of type date_of_joining which is inside the main structure employee_info.

How to access nested structure in C?

We can access a nested structure in C by the following syntax. consider above program to access its different member.

// Accessing nested structure



In this above syntax, by e1.dt.dd we can access the dd member of inner structure. And by e1.dt.yyyy we can access the yyyy member of inner structure. Now, lets see an example program using nested structure in C.

C program using nested structure

Here, we will store the address of some entity employee in a structure.

// C program using nested structure

struct identity{
  int id;
  char city[30];
  char phone[14];

struct employee{
  char name[50];
  struct identity add;    // nested structure

int main(){
  struct employee em1;
  printf("Enter information of employee here : \n");
  printf("Enter name : ");

  printf("Enter city : ");

  printf("Enter phone no : ");

  printf("Enter id : ");
  scanf("%d", &;

  printf("\nPrinting employee information......\n");
  printf("ID: %d\nName: %s\nCity: %s\nPhone: %s\n",,,,;

  return 0;

Output of nested structure program:

Enter information of employee here :
Enter name : Alan
Enter city : Wasington
Enter phone no : 012345678910
Enter id : 302

Printing employee information......
ID: 302
Name: Alan
City: Wasington
Phone: 012345678910

Another C program using nested structure

// Example C program using nested structure

#include <stdio.h>
#include <string.h>
struct employee_struct{
  char name[30];
  int id;
  struct joining_date{
    int dd;
    int mm;
    int yyyy;

int main(){ = 23;
  strcpy(, "John Doe");
  e1.doj.dd = 29; = 12;
  e1.doj.yyyy = 2016;

  printf("Employee details ............. \n\n");
  printf("ID: %d\n",;
  printf("Name: %s\n",;
  printf("Joining date(dd/mm/yyyy): %d/%d/%d\n", e1.doj.dd,,e1.doj.yyyy);

  return 0;

Compile and run this program to get an output like this

nested structure in c, c program of nested structure

Previous pageStructure and function in C

Next pageC union