In C, structures are used to group different data types. They allow you to define your own data types by grouping variables of different types under a single name.
Here's a sample code that demonstrates how to use structures to store information about students in an array of structures, accept information, query by ID, and display information about that student:
#include <stdio.h>
struct Student {
int id;
char name[50];
int age;
};
int main() {
struct Student students[5];
for (int i = 0; i < 5; i++) {
printf("Enter ID for student %d: ", i + 1);
scanf("%d", &students[i].id);
printf("Enter name for student %d: ", i + 1);
scanf("%s", students[i].name);
printf("Enter age for student %d: ", i + 1);
scanf("%d", &students[i].age);
}
int queryId;
printf("Enter the ID of the student to query: ");
scanf("%d", &queryId);
for (int i = 0; i < 5; i++) {
if (students[i].id == queryId) {
printf("Student found!\n");
printf("ID: %d\nName: %s\nAge: %d\n", students[i].id, students[i].name, students[i].age);
return 0;
}
}
printf("Student with ID %d not found.\n", queryId);
return 0;
}
In this code:
We define a structure Student with three members: id, name, and age.
We declare an array of Student structures to store information for multiple students.
We use a loop to accept information for each student from the user.
We then prompt the user to enter the ID of the student they want to query and display the information if the student is found.
The code uses structures to organize and manage student information effectively.
We can use typedef to create an alias for the struct Student in the above code. This can make the code more readable and easier to maintain.
Here's how we can use typedef in the code:
#include <stdio.h>
typedef struct {
int id;
char name[50];
int age;
} Student;
int main() {
Student students[5];
for (int i = 0; i < 5; i++) {
printf("Enter ID for student %d: ", i + 1);
scanf("%d", &students[i].id);
printf("Enter name for student %d: ", i + 1);
scanf("%s", students[i].name);
printf("Enter age for student %d: ", i + 1);
scanf("%d", &students[i].age);
}
int queryId;
printf("Enter the ID of the student to query: ");
scanf("%d", &queryId);
for (int i = 0; i < 5; i++) {
if (students[i].id == queryId) {
printf("Student found!\n");
printf("ID: %d\nName: %s\nAge: %d\n", students[i].id, students[i].name, students[i].age);
return 0;
}
}
printf("Student with ID %d not found.\n", queryId);
return 0;
}
In this modified code, we use typedef to create an alias Student for the struct that defines the student's information. This simplifies the declaration of the array of structures and makes the code more concise.
Here's a sample program with nested structures related to students, along with detailed comments to explain the nested structure and its usage:
#include <stdio.h>
struct Address {
char street[50];
char city[50];
char state[50];
int zipCode;
};
struct Student {
int id;
char name[50];
int age;
struct Address studentAddress;
};
int main() {
struct Student student1;
printf("Enter ID for the student: ");
scanf("%d", &student1.id);
printf("Enter name for the student: ");
scanf("%s", student1.name);
printf("Enter age for the student: ");
scanf("%d", &student1.age);
printf("Enter street: ");
scanf("%s", student1.studentAddress.street);
printf("Enter city: ");
scanf("%s", student1.studentAddress.city);
printf("Enter state: ");
scanf("%s", student1.studentAddress.state);
printf("Enter zip code: ");
scanf("%d", &student1.studentAddress.zipCode);
printf("Student Information:\n");
printf("ID: %d\nName: %s\nAge: %d\nAddress: %s, %s, %s, %d\n", student1.id, student1.name, student1.age, student1.studentAddress.street, student1.studentAddress.city, student1.studentAddress.state, student1.studentAddress.zipCode);
return 0;
}
In this code:
We define a structure Address to represent the address of the student, including street, city, state, and zip code.
We then define a structure Student that includes the student's ID, name, age, and a nested structure studentAddress representing the student's address.
In the main function, we accept information for the student and the student's address, and then display the information.
This nested structure allows us to organize related data (in this case, student information and address) hierarchically, making the code more readable and maintainable.