Skip to main content

Practical-12

Write a program using structure within structure.

Introduction

In this program, we will learn to use a structure within a structure.

Algorithm

  1. Start
  2. Declare a structure called outer_struct, and it stores information about a student.
  3. Declare a structure called inner_struct, and it stores information about a student.
  4. Intiliaze the student details.
  5. Print the student details.
  6. End

Code

Practical-12.c

#include <stdio.h>

void main()
{
/* STRUCTURE within STRUCTURE */

/* first define the outer structure */
struct outer_struct {
char name[20];
int age;

/* now define the inner structure */
struct inner_struct {
int marks;
char grade;
} inner;
} outer;

/* assign values to outer structure */
strcpy(outer.name, "Naruto");
outer.age = 20;
/* assign values to the inner structure */
outer.inner.marks = 80;
outer.inner.grade = 'A';

/* display the structure member values */
printf("name: %s\n", outer.name);
printf("age: %d\n", outer.age);
printf("marks: %d\n", outer.inner.marks);
printf("grade: %c\n", outer.inner.grade);

}

Output

name: Naruto
age: 20
marks: 80
grade: A
Explanation

In this program, we have used a structure within a structure. We have defined a structure called outer_struct, and it stores information about a student. We have defined another structure called inner_struct, and it stores information about a student. We have initialized the student details. We have printed the student details.

Related Links