How to Copy and Compare Structure Variables?


Two variables of the same structure type can be copied the same way as ordinary variables. If persona1 and person2 belong to the same structure, then the following statements Are valid.

person1 = person2;

person2 = person1;

C does not permit any logical operators on structure variables In case, we need to compare them, we may do so by comparing members individually.

person1 = = person2

person1 ! = person2

Statements are not permitted.

Ex: Program for comparison of structure variables

struct class
{
int no;
char name [20];
float marks;
}
main ( )
{
int x;
struct class stu1 = { 111, “Rao”, 72.50};
struct class stu2 = {222, “Reddy”, 67.80};
struct class stu3;
stu3 = stu2;
x = ( ( stu3.no= = stu2.no) && ( stu3.marks = = stu2.marks))?1:0;
if ( x==1)
{
printf (“ \n student 2 & student 3 are same \n”);
printf (“%d\t%s\t%f  “ stu3.no, stu3.name, stu3.marks);
}
else
printf ( “\n student 2 and student 3 are different “);
}

Out Put:

Student 2 and student 3 are same
222 reddy 67.0000




Related

C Language 4124136046952277522

Post a Comment

emo-but-icon

item