Structure in C Language

Hello friends how are you, today in this blog i will teach you what is structure, syntax of structure, how structure works and many programs of structure in a very simple way.

Let's start

1.It is a collection of data of different data type.
2.It is a user defined data type.
3.Data can of int, char, float, double etc. data type.
4.We can access the member of structure by making the variable of structure.
5.struct keyword is used to create a structure.

👉Syntax

👉Example 1 

struct student
{
 char name[200];
 int rollno;
 float marks;
};

1.Here student is the name of structure .
2.struct is a keyword.

👉Declaration of structure variable method 1 

struct student
{
 char name[200];
 int rollno;
 float marks;
};
int main()
{
struct student student1;
return 0;
}

Here student1 is the variable of structure. 

👉Declaration of structure variable method 2 

struct student
{
 char name[200];
 int rollno;
 float marks;
}student1;
int main()
{
return 0;
}

Here student1 is the variable of structure. 

👉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 student information

#include<stdio.h>
#include<string.h>
struct student
{
 char name[200];
 int rollno;
 float marks;
};
int main()
{
struct student student1;//Declaring structure variable
strcpy(student1.name,"Lucky");
student1.rollno=201;
student1.marks=85.9;
printf("Student Name =%s\n",student1.name);
printf("Student Rollno=%d\n",student1.rollno);
printf("Student Marks=%f\n",student1.marks);
}
/*
### Output ###
Student Name=Lucky
Student Rollno=201
Student Marks=85.9
*/

👉Example: Method 2  

#include<stdio.h>
#include<string.h>
struct student
{
 char name[200];
 int rollno;
 float marks;
};
int main()
{
struct student student1={"Lucky",201,85.9};
printf("Student Name =%s\n",student1.name);
printf("Student Rollno=%d\n",student1.rollno);
printf("Student Marks=%f\n",student1.marks);
}
/*
### Output ###
Student Name =Lucky
Student Rollno=201
Student Marks=85.900002
*/

👉User input with structure  

#include<stdio.h>
#include<string.h>
//structure declaration
struct student
{
 char name[200];
 int rollno;
 float marks;
};
int main()
{
//creating variable of student
struct student student1;
printf("Enter name of student:");
scanf("%s",&student1.name);
printf("Enter rollno of student:");
scanf("%d",&student1.rollno);
printf("Enter marks of student:");
scanf("%f",&student1.marks);
printf("Student Name =%s\n",student1.name);
printf("Student Rollno=%d\n",student1.rollno);
printf("Student Marks=%f\n",student1.marks);
}
/*
### Output ###
Enter name of student:Rocky
Enter rollno of student:204
Enter marks of student:85.6
Student Name =Rocky
Student Rollno=204
Student Marks=85.599998
*/

👉Pointer with structure  

#include<stdio.h>
#include<string.h>
//structure declaration
struct student
{
 int rollno;
};
int main()
{
//creating normal variable of student
struct student student1;
student1.rollno=204;
//creating pointer variable of student
struct student *ptr;
//passing normal variable address into pointer variable
ptr=&student1;
printf("Student Rollno=%d\n",*ptr);
}
/*
### Output ###
Student Rollno=204
*/

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 

If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thanks.😊

Post a Comment

0 Comments