What is Structure in C ?
Arrays allow you to define the type of variables that can hold many data items of the same type. Similarily In C, a structure is a user-defined data type that groups together different types of data (such as integers, characters, and strings) under a single name.
Syntax for defining a structure in C
struct struct_name
{
data_type member_name1;
data_type member_name2;
...
};
For example, you can define a structure called "student" that contains information about a student, such as their name, ID number, and grade point average:
struct student
{
char name[50];
int id;
float gpa;
};
Accessing Structure Members
Once you have defined a structure, you can create variables of that structure type and access their members using the dot operator (.). For example, you can create a variable called "s" of type "student" and set its members like so:
struct student s;
s.name = "John Smith";
s.id = 12345;
s.gpa = 3.5;
Structures are useful for organizing related data in a logical and easy-to-understand way.
For Example:
#include <stdio.h>
struct student
{
char name[20];
int age;
float percentage;
};
int main()
{
struct student s1;
printf("Enter Student name: ");
scanf("%s", s1.name);
printf("Enter Student age: ");
scanf("%d", &s1.age);
printf("Enter Student Percentage: ");
scanf("%f", &s1.percentage);
printf("Student Information:");
printf("Name: %s", s1.name);
printf("Age: %d", s1.age);
printf("Salary: %.2f",s1.percentage);
return 0;
}
This program creates a structure called "student" with three elements: a character array for the name, an integer for the age, and a float for the percentage. The program then declares a variable "s1" of the struct type "student" and uses the scanf function to read in values for the student's name, age, and percentage. Finally, the program prints out the student's information using the printf function.
Advantages of Structure in C
Structures in C provide several benefits, including:
-
Data organization: Structures allow you to group together related data items, such as the name, ID, and grade point average of a student, under a single name. This makes it easier to understand and manipulate the data in your program.
-
Code reusability: By defining a structure once, you can create multiple variables of that structure type and use them throughout your program. This can help you avoid writing repetitive code and make your program more modular.
-
Improved memory management: Structures allow you to store multiple types of data in a single variable, which can save memory compared to creating separate variables for each piece of data.
-
Simplified parameter passing: When you pass a structure variable to a function, the entire structure is passed, rather than each member variable separately. This can simplify the function's implementation and make the code more readable.
-
Better data representation: Structures provide a way to represent real-world objects or concepts in your program, which can make your code more expressive and easy to understand.
Overall, structures in C provide a powerful and flexible way to organize and manipulate data, and are often used in many types of C programs.