Write a program to get the value of structure variable and print it

Updated:13/08/2023 by Computer Hope

Go Back


A struct is a composite data type (or record) declaration in the C programming language (and many of its derivatives) that defines a physically grouped list of variables under one name in a memory block. This allows the various variables to be accessed by a single pointer or by the struct declared name, which returns it. Want to learn how we can implement Struct in c


        //Write a program  to get the  value of structure variable and print it .
struct student
{
int   sno,m;
char sname[20];
float per;
};
void main()
{
struct student s1,s2;
printf("\n enter sno,m,sname of s1");
scanf("%d%d%s",&s1.sno,&s1.m,s1.sname);
s1.per=s1.m/3.0;
printf("\n enter sno,m,sname of s2");
scanf("%d%d%s",&s2.sno,&s2.m,s2.sname);
s2.per=s2.m/3.0;
printf("\n  %d  %d   %s   %f ", s2.sno , s2.m , s2.sname , s2.per );
printf("\n  %d  %d   %s   %f" , s1.sno , s1.m , s1.sname , s1.per );
}

      

Conclusion

In this article , we created a struct student ,also assign attribute like sno,sname, per . We printed a student s1 details as scanf("%d%d%s",&s2.sno,&s2.m,s2.sname); so we good to see out put.