logo
Nested structure In C Language
Nested structure in C is nothing but structure within structure. One structure can be declared inside other structure. We can take any data type for declaring structure members like int, float, char etc. In the same way we can also take object of one structure as member in another structure.

Example:
struct student
{
 char[30] name;
 int age;
  struct address
    {
      char[50] locality;
      char[50] city;
      int pincode;
    };
 };
Program : The following program is an example of nested structure in c
#include <stdio.h>
#include <string.h>

struct student_college_detail
{
    int college_id;
    char college_name[50];
};


struct student_detail 
{
    int id;
    char name[20];
    float percentage;
    // structure within structure
    struct student_college_detail clg_data;
}stu_data;

int main() 
{
    struct student_detail stu_data = {1, \”sai”, 90.5, 1110,”sv University”};
    printf(“ Id is: %d \n”, stu_data.id);
    printf(“ Name is: %s \n”, stu_data.name);
    printf(“ Percentage is: %f \n\n”, stu_data.percentage);
    printf(“ College Id is: %d \n”,  stu_data.clg_data.college_id);
    printf(“ College Name is: %s \n”, stu_data.clg_data.college_name);
    return 0;
}
Output :

Id is: 1
Name is: sai
Percentage is: 90.50000
College id is: 1110
College name is: Sv University