Nested Structure in C

Last Updated :

In C programming language, a structure is a composite data type that groups together variables of different data types under a single name. A nested structure is a structure that has another structure as one of its members.

Example:
Here’s an example of a nested structure in C:

#include <stdio.h>
#include <string.h>

struct date {
    int day;
    int month;
    int year;
};

struct student {
    char name[50];
    int roll_number;
    struct date date_of_birth;
};

int main() {
    struct student s1;

    strcpy(s1.name, "John Smith");
    s1.roll_number = 101;
    s1.date_of_birth.day = 15;
    s1.date_of_birth.month = 8;
    s1.date_of_birth.year = 1995;

    printf("Name: %s\n", s1.name);
    printf("Roll number: %d\n", s1.roll_number);
    printf("Date of birth: %d/%d/%d\n", s1.date_of_birth.day, s1.date_of_birth.month, s1.date_of_birth.year);

    return 0;
}

In this example, we define two structures: date and student. The date structure has three members: day, month, and year, all of type int. The student structure has three members: name, of type char array, roll_number, of type int, and date_of_birth, of type date.

We then declare a variable s1 of type student, and initialize its members using dot notation. Finally, we print the values of the structure members using printf statements.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Comment
Next Article
Mithlesh Upadhyay Published 20 Feb, 2023 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Article Tags :

Similar Reads

  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read