In C, a structure is a user-defined data type that groups together related data items of different data types. The structure provides a convenient way to represent complex data in a simple and organized way.
Here is an example of a structure:
#include
struct student {
char name[50];
int roll_no;
float marks;
};
int main() {
struct student s1;
printf("Enter name: ");
scanf("%s", s1.name);
printf("Enter roll number: ");
scanf("%d", &s1.roll_no);
printf("Enter marks: ");
scanf("%f", &s1.marks);
printf("\nName: %s\n", s1.name);
printf("Roll number: %d\n", s1.roll_no);
printf("Marks: %.2f\n", s1.marks);
return 0;
}
In this example, we define a structure named student that contains members: name, roll_no, and marks. Each member has a different data type. We then declare a variable s1 of type student in the main function. We use the scanf function to read in values for the members of the s1 structure, and then print out the values using the printf function.
You can also use arrays and pointers with structures in C. For example, you can declare an array of structures like this:
struct student {
char name[50];
int roll_no;
float marks;
} s[10];
In this example, we declare an array s of 10 elements, each of which is a student structure. You can then access the members of the structures using the array index, like this:
s[0].roll_no = 1;
s[0].marks = 90.5;
You can also use pointers to structures like this:
struct student *ptr;
ptr = &s[0];
ptr->roll_no = 1;
ptr->marks = 90.5;
In this example, we declare a pointer ptr to a student structure, and initialize it to point to the first element of the s array. We can then access the members of the structure using the -> operator.
In C, a union is a user-defined data type that allows you to store different data types in the same memory location. A union can hold only one value at a time, but the type of the value can change at runtime.
Here is an example of a union:
#include
union my_union
{
int x;
float y;
};
Int main()
{
union my_union u;
u.x = 10;
printf("x: %d\n", u.x);
u.y = 3.14;
printf("x: %d\n", u.x);
printf("y: %.2f\n", u.y);
return 0;
}
In this example, we define a union named my_union that contains two members: x of type int and y of type float. We then declare a variable u of type my_union in the main function. We use the . operator to access the x member of the union and assign a value of 10 to it. We then print out the value of x.
Next, we assign a value of 3.14 to the y member of the union, and print out the values of both x and y. Note that since x and y share the same memory location, assigning a value to y changes the value of x.
Unions are often used in C to save memory when different data types need to be stored in the same memory location. For example, if you have a data structure that can be represented as an integer, a float, or a string, you can define a union to store the data in a more memory-efficient way. However, unions can be tricky to use and require careful programming to avoid errors.
Learners TV is a website that is designed to educate users and provide instructional material on particular subjects and topics.