Structure in C:
Requirement: Visual studio code, MinGW and C/C++ extension installed in visual studio code
a structure is a collection of variables which may be different data types.
And Each variable in the structure is known as a member of the structure.
Syntax:
struct Student
{
char name[25];
int age;
int class;
char gender[10];
};
Example:
#include stdio.h
#include string.h
struct Student
{
char name[25];
int age;
int class;
char gender[10];
};
int main()
{
struct Student s1;
// s1 is a variable of Student type(structure variable) and name, age,class, gender is a member of Student
strcpy(s1.name, "RAJ"); // using string function to add name
s1.age = 20;
s1.class = 12;
strcpy(s1.gender, "Male"); // using string function to add name
printf("Name of stdent is : %s\n", s1.name);
printf("Age of Student is: %d\n", s1.age);
printf("class of Student is: %d\n", s1.class);
printf("Gender of Student is: %s\n", s1.gender);
return 0;
}
strcpy():
The C library function char *strcpy(char *dest, const char *src)
copies the string pointed to, by src to dest.
In C programming, structure is a collection of different data items which are referenced by single name. It is also known as user-defined data-type in C.
• It helps to construct complex data.
• Heterogeneous collection of data items.
• Reduced complexity
Example: struct student s[3];
• Increased productivity:
• Maintainability of code
• Enhanced code readability: code readability is crucial for larger projects. Using structure code looks user friendly which in turn helps to maintain the project.