C : I ask CHAT GPT to write the program for nested structure

Programmer Portfolio
2 min readOct 31, 2023

--

Here is the mind blowing result from CHAT GPT :

Prompt : write a C program to create intern structure with name, intern id, and job role and two nested structure college with college id, college name, CGPA and school with school name, and percentage as a data member accept and display.

Result :

#include <stdio.h>

// Define the 'school' structure
struct School {
char schoolName[50];
float percentage;
};

// Define the 'college' structure with nested 'school' structure
struct College {
int collegeId;
char collegeName[50];
float CGPA;
struct School schoolInfo;
};

// Define the 'intern' structure with nested 'college' structure
struct Intern {
char name[50];
int internId;
char jobRole[50];
struct College collegeInfo;
};

int main() {
struct Intern internData;

// Accepting data from the user
printf("Enter intern name: ");
scanf(" %[^\n]s", internData.name);
printf("Enter intern ID: ");
scanf("%d", &internData.internId);
printf("Enter job role: ");
scanf(" %[^\n]s", internData.jobRole);
printf("Enter college ID: ");
scanf("%d", &internData.collegeInfo.collegeId);
printf("Enter college name: ");
scanf(" %[^\n]s", internData.collegeInfo.collegeName);
printf("Enter CGPA: ");
scanf("%f", &internData.collegeInfo.CGPA);
printf("Enter school name: ");
scanf(" %[^\n]s", internData.collegeInfo.schoolInfo.schoolName);
printf("Enter percentage: ");
scanf("%f", &internData.collegeInfo.schoolInfo.percentage);

// Displaying the entered information
printf("\nIntern Information:\n");
printf("Name: %s\n", internData.name);
printf("Intern ID: %d\n", internData.internId);
printf("Job Role: %s\n", internData.jobRole);
printf("\nCollege Information:\n");
printf("College ID: %d\n", internData.collegeInfo.collegeId);
printf("College Name: %s\n", internData.collegeInfo.collegeName);
printf("CGPA: %.2f\n", internData.collegeInfo.CGPA);
printf("\nSchool Information:\n");
printf("School Name: %s\n", internData.collegeInfo.schoolInfo.schoolName);
printf("Percentage: %.2f\n", internData.collegeInfo.schoolInfo.percentage);

return 0;
}

Output :

--

--

Programmer Portfolio

Code enthusiast. Learning, practicing, and sharing my journey through various programming languages, one code at a time. 🚀💻