ADVERTISEMENT
ADVERTISEMENT

C program to demonstrate example of Nested Structure

In this program, we will learn how to declare, initialize Nested Structure (Structure within Structure)? How to assign values/read values and access the Nested Structure members?

Here, in this example - we will create a structure dateOfBirth which will be declared inside the structure student.

/*C program to demonstrate example of nested structure*/
#include <stdio.h>

struct student{
	char name[30];
	int rollNo;
	
	struct dateOfBirth{
		int dd;
		int mm;
		int yy;
	}DOB;	/*created structure varoable DOB*/
};

int main()
{
	struct student std;

	printf("Enter name: "); 		gets(std.name);
	printf("Enter roll number: ");	scanf("%d",&std.rollNo);
	printf("Enter Date of Birth [DD MM YY] format: ");
	scanf("%d%d%d",&std.DOB.dd,&std.DOB.mm,&std.DOB.yy);
	printf("\nName : %s \nRollNo : %d \nDate of birth : %02d/%02d/%02d\n",std.name,std.rollNo,std.DOB.dd,std.DOB.mm,std.DOB.yy);
	
	return 0;
}

Output

Enter name: Mike 
Enter roll number: 101 
Enter Date of Birth [DD MM YY] format: 14 03 92 
 
Name : Mike  
RollNo : 101  
Date of birth : 14/03/92 

C Structure & Union Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.