Monday, September 30, 2024

Structures in C Language

 C Structures

Structure - structure is a collection of different data types.

program to create a structure and use them with structure variable. 

#include<stdio.h>
#include<string.h>
struct student
{
char name[20];
int age;
float per;
};
main()
{
struct student s1;
clrscr();
printf("\n Enter the name= ");
scanf("%c",&s1.name);
printf("\n Enter the age= ");
scanf("%d", &s1.age);
printf("\n Enter the per= ");
scanf("%f", &s1.per);
printf("\n The name is= %s",s1.name);
printf("\n The age is= %d",s1.age);
printf("\n The per is= %f",s1.per);
getch();
}

//structure keyword     : sturct
//sturctue name      : student
//structure variable     : s1
//structure member     : name, age, per
//structure presentation     : variable.member

program to create a structure and use them with two structure variable. 

#include<stdio.h>
#include<string.h>
struct student
{
char name[20];
int age;
float per;
};
main()
{
struct student s1,s2; //two structure variable
clrscr();
printf("\n Enter the name= ");
scanf("%c",&s1.name);
printf("\n Enter the age= ");
scanf("%d", &s1.age);
printf("\n Enter the per= ");
scanf("%f", &s1.per);
flush(stdin);
printf("\n Enter the name= ");
scanf("%c",&s2.name);
printf("\n Enter the age= ");
scanf("%d", &s2.age);
printf("\n Enter the per= ");
scanf("%f", &s2.per);
printf("\n The name is= %s",s1.name);
printf("\n The age is= %d",s1.age);
printf("\n The per is= %f",s1.per);
printf("\n The name is= %s",s2.name);
printf("\n The age is= %d",s2.age);
printf("\n The per is= %f",s2.per);
getch();
}
//structure keyword     : sturct
//sturctue naem     : student
//structure variable     : s1,s2
//structure member     : name, age, per
//structure presentation     : variable.member

program to create a Array of structure i.e. more than two structure variable using for loop.

//Array of structure i.e. more than two structure variabel using for loop.
#include<stdio.h>
#include<string.h>
struct student
{
char name[20];
int age;
};
main()
{
struct student s[3]; //structure array of 3
int i;
clrscr();
for(i=0;i<3;i++)
{
printf("\n Enter the name");
fulush(stdin);
scanf("%s",&s[i].name);
printf("\n Enter the age= ");
scanf("%d",s[i].age);
}
printf("\n Name Age ");
printf("\n ---------------------");
for (i=0;i<3;i++)
{
printf("\n %s %d",s[i].name, s[i].age);
}
printf("\n #####################");
getch();
}
//structure keyword     : sturct
//sturctue name     : student
//structure variable     : s[i]
//structure member     : name, age
//structure presentation     : variable array.member


Qus:- how to create structure with name, age and percent of a student, and how to create that structure variable to access the structure members?

Ans:-     struct student {char name[20]; int age, per}
student s;

Qus:- write down a structure of name, age, and percent and print two student details.

Ans:-     struct student {char name[20]; int age, per}
student s1,s2;

Qus:- write down a structure of name, age and percent and print 4 student details using structure array.

Ans:- struct student {char name[20]; int age, per}
student s[3];

Qus:- write down an union of name and age and then print them.
Ans:- *s (undefined length);

No comments:

Post a Comment

Autonomous Vehicles

Autonomous Vehicles Autonomous vehicles are cars or trucks that can drive themselves without needing a human to control them. They use advan...