Structure

By using arrays we can store items of single data type. Student id ( roll number ) , name , mark ,class are different data types but they belong to same entity student. Here we can declare a structure to store all details of the student which consist of different data types.

Declaring structure variables.
int main()
{
struct Students{
	int id;
	char name[50];
	float mark[5];
	}; 
struct Students s1,s2;
struct Students s3={5,"Ronn",12.5};	

return 0;
}
Here s1,s2,s3 are variables of type struct Students.
Students is the tagname. We used this tagname to create struct Variables.

Structure is declared to create an template only, we can't initialize the structure at the time of declaring it.
This is not allowed.
struct Students{
	int id=1;  // Not valid 
	char name[50]="Ravi"; // Not valid
	float mark[5]=4.5; // Not valid 
	}; 

Assigning data to struct variables

We can read user input and assing the data to struct variables.
int main()
{
struct Students{
	int id;
	char name[50];
	float mark;
	}; 
struct Students s1,s2;
printf("\nEnter ID :");
scanf("%d",&s2.id);
printf("\nEnter name :");
scanf("%s",&s2.name);
printf("\nEnter mark :");
scanf("%f",&s2.mark);
// Input over now display data ///
printf("\n\n ID:  %d",s2.id);
printf("\n name:  %s",s2.name);
printf("\n mark:  %f",s2.mark);

return 0;
}

Array of structure

Using array we store user entered data and display the same. Similarly we can create array of structure for struct variables.
int main()
{
struct Students{
	int id;
	char name[50];
	float mark;
	}std[2]; 
int i;
for(i=0;i<2;i++){
printf("\nEnter ID :");
scanf("%d",&std[i].id);
printf("\nEnter name :");
scanf("%s",&std[i].name);
printf("\nEnter mark :");
scanf("%f",&std[i].mark);
}
// Input is over , now display ///
for(i=0;i<2;i++){
printf("\n\n ID:  %d",std[i].id);
printf("\n name:  %s",std[i].name);
printf("\n mark:  %f",std[i].mark);
}
return 0;
}
We can directly assign data to struct variable ( without any user input )
int main()
{
struct Students{
	int id;
	char name[50];
	float mark;
	}; 
int i;
struct Students std[2]={
	{1,"Ronn",5.6},
	{2,"Geek",7.3}
};

// Input is over , now display ///

for(i=0;i<2;i++){
printf("\n\n ID:  %d",std[i].id);
printf("\n name:  %s",std[i].name);
printf("\n mark:  %f",std[i].mark);
}
return 0;
}
Output
 ID:  1
 name:  Ronn
 mark:  5.600000

 ID:  2
 name:  Geek
 mark:  7.300000

Accessing the value of structure variable.

We can use dot (.) operators to access the value of structure variables.
printf("Name:  %s",s3.name); // Ronn

Copying data of elements

We used strcpy() to copy string data.
int main()
{
struct Students{
	int id;
	char name[50];
	float mark;
	}; 
int i;
struct Students s1;
struct Students s2={5,"Ronn",12.5};
/// copying data from s2 to s1 ///
s1.id=s2.id+1;
strcpy(s1.name,s2.name);
s1.mark=s2.mark-0.1;

printf("ID : %d",s1.id);
printf("\nName : %s",s1.name);
printf("\nMark : %f",s1.mark);
return 0;
}
Output is here
ID : 6
Name : Ronn
Mark : 12.400000
Structure elements are stored in contiguous memory locations. So directly we can copy one element to other. Allow user to enter numbers & exit if user enters 0 or negative number
if else Different tariff plan for different range of consumption (Example of if else )
  • Video on Answer to the below Questions on grade Calculation



plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer