C programming language offers a powerful feature called struct
(short for structure) that allows you to group different types of variables under a single name. This is particularly useful when you want to represent a more complex entity that has multiple attributes. In this blog post, we’ll explore the basics of structs and demonstrate their use with three example programs.
What is a Struct?
A struct
in C is a user-defined data type that groups together variables of different data types. These variables, known as members, are stored together in a contiguous block of memory.
Basic Syntax of a Struct
Here’s a basic syntax to define a struct in C:
struct StructName {
dataType member1;
dataType member2;
// ... more members
};
Example 1: Defining and Using a Struct
Let’s start with a simple example. We will define a struct to represent a student and use it to store and display student information.
#include <stdio.h>
// Define a structure for a student
struct Student {
char name[50];
int age;
float gpa;
};
int main() {
// Create a student variable
struct Student student1;
// Input student details
printf("Enter student's name: ");
fgets(student1.name, 50, stdin);
printf("Enter student's age: ");
scanf("%d", &student1.age);
printf("Enter student's GPA: ");
scanf("%f", &student1.gpa);
// Output student details
printf("\nStudent Information:\n");
printf("Name: %s", student1.name);
printf("Age: %d\n", student1.age);
printf("GPA: %.2f\n", student1.gpa);
return 0;
}
Explanation
- Structure Definition: The
struct Student
defines a structure with three members:name
,age
, andgpa
. - Main Function:
- A variable
student1
of typestruct Student
is declared. - The program prompts the user to input the student’s name, age, and GPA.
- The details of
student1
are then printed to the console.
Example 2: Array of Structs
Now, let’s extend our example to handle multiple students by using an array of structs.
#include <stdio.h>
// Define a structure for a student
struct Student {
char name[50];
int age;
float gpa;
};
int main() {
int i;
struct Student students[3];
for(i = 0; i < 3; i++) {
printf("Enter details for student %d:\n", i + 1);
printf("Name: ");
getchar(); // To consume the newline character left by previous input
fgets(students[i].name, 50, stdin);
printf("Age: ");
scanf("%d", &students[i].age);
printf("GPA: ");
scanf("%f", &students[i].gpa);
}
// Output students details
printf("\nStudent Information:\n");
for(i = 0; i < 3; i++) {
printf("Student %d:\n", i + 1);
printf("Name: %s", students[i].name);
printf("Age: %d\n", students[i].age);
printf("GPA: %.2f\n\n", students[i].gpa);
}
return 0;
}
Explanation
- Array of Structs: An array
students
of typestruct Student
is declared to hold information for three students. - Loop for Input: A loop is used to input details for each student.
- Loop for Output: Another loop is used to display the details of each student.
Example 3: Passing Structs to Functions
Finally, let’s see how to pass a struct to a function and return a struct from a function.
#include <stdio.h>
// Define a structure for a student
struct Student {
char name[50];
int age;
float gpa;
};
// Function to input student details
struct Student inputStudent() {
struct Student s;
printf("Enter student's name: ");
getchar(); // To consume the newline character left by previous input
fgets(s.name, 50, stdin);
printf("Enter student's age: ");
scanf("%d", &s.age);
printf("Enter student's GPA: ");
scanf("%f", &s.gpa);
return s;
}
// Function to display student details
void displayStudent(struct Student s) {
printf("\nStudent Information:\n");
printf("Name: %s", s.name);
printf("Age: %d\n", s.age);
printf("GPA: %.2f\n", s.gpa);
}
int main() {
struct Student student1;
student1 = inputStudent();
displayStudent(student1);
return 0;
}
Explanation
- Function to Input Struct: The
inputStudent
function prompts the user to input student details and returns astruct Student
. - Function to Display Struct: The
displayStudent
function takes astruct Student
as an argument and prints the student details. - Main Function: The
main
function callsinputStudent
to get the student details and then callsdisplayStudent
to display them.
Conclusion
Structs are a fundamental part of C programming, allowing you to group different types of variables together. This makes your code more organized and easier to manage. By practicing with these examples, you’ll gain a better understanding of how to define and use structs in your programs.
Happy coding! Feel free to leave any questions or comments below.