Structure in c language
- It is a collection of data of different data type.
- It is a user defined data type.
- Data can of int,char,float,double etc. data type.
- We can access the member of structure by making the variable of structure.
- struct keyword is used to create a structure.
syntax:
struct structure_name
{
data_type variable 1;
data_type variable 2;
};
Example:
struct student
{
char name[100];
int rollno;
float marks;
};
- Here student is the name of structure.
- struct is a keyword
Declaration of structure variable method 1
struct student
{
char name[100];
int rollno;
float marks;
};
void main( )
{
struct student student1;
};
- Here student1 is the variable of structure.
Declaration of structure variable method 2
struct student
{
char name[100];
int rollno;
float marks;
}student1;
int main( )
{
return 0;
}
Accessing the data members of structure :
The data member of structure can be accessed as structure_variable.data member
for example if we want to access the rollno of student then we can write as student1.rollno
example – write a program to store and display the student name,rollno and marks.
#include< stdio.h >
>#include< string. h >
struct student
{
char name[100];
int rollno;
float marks;
};
void main( )
{
struct student student1;
(strcpy(student1.name,"Aditya");
student1.rollno = 07;
student1.marks = 80.5;
printf("Student name = %s \n",student1.name);
printf("Student rollno = %d \n",student1.rollno);
printf("Student marks = %f \n",student1.marks);
}
Output:
Student name = Aditya
Student rollno = 07
Student marks = 85.5