Nested-Structure-in-c .

In this session i have discussed nested structure in C .


Here we will learn structure within structure or nested structure .


Example : Write a C program demonstrate the nested structure in c .

    #includ<stdio.h> 
    #include<string.h>
    struct Employee
    {
        char name[20];
        int age;
        struct Sal   // nested struct
        {
            int salary;
        }S;
    }E;
    int main()
    {
        printf("\nEnter the Name :");
        scanf("%s",&E.name);
        printf("\nEnter the Age :");
        scanf("%d",&E.age);
        printf("\nEnter the Salary :");
        scanf("%d",&E.S.salary);
        printf("---------------------------------\n");
        printf("Name : %s\n",E.name);
        printf("Age : %d\n",E.age);
        printf("Salary : %d\n",E.S.salary);
        return (0);
    }
    OUTPUT : Enter the Name : Rakesh
             Enter the Age  : 20
             Enter the Salary : 20000
             -------------------------------------
             Name : Rakesh
             Age : 20
             Salary : 20000
    


Example : Write a C program that demonstrate the structure with in structure .

    #includ<stdio.h> 
    #include<string.h>
    struct Sal
    {
        int salary;
    };

    struct Employee
    {
        struct Sal S;
        char name[20];
        int age;
    }E;

    int main()
    {
        printf("\nEnter the Name :");
        scanf("%s",&E.name);
        printf("\nEnter the Age :");
        scanf("%d",&E.age);
        printf("\nEnter the Salary :");
        scanf("%d",&E.S.salary);
        printf("---------------------------------\n");
        printf("Name : %s\n",E.name);
        printf("Age : %d\n",E.age);
        printf("Salary : %d\n",E.S.salary);
        return (0);
    }

    
    OUTPUT : Enter the Name : Rakesh
             Enter the Age  : 20
             Enter the Salary : 20000
             -------------------------------------
             Name : Rakesh
             Age : 20
             Salary : 20000
    

Welcome to AndroidATPoint.

back image                     next image