Comparing one structure variable with another structure variable is illegal in C

C does not allow us to compare one structure variable with another variable

 if(stud1.fees>stud2.fees)

As can be seen from the below example that the structure called student contain two variable that is stud1 and stud1

But in C, it is not possible to compare one structure variable(stud1) with another structure variable (stud2)

Such as

Stud1>stud2 or  Stud1>stud2

Let’s see an example

#include <stdio.h>
struct student
{
    char name[50];
    int roll_no;
    float fees;
    char course[80];
};
int main()
{
    struct student stud1={"one",01,100.0,"history"};
    struct student stud2={"two",02,200.0,"biology"};
    printf("\n%f",stud1.fees);
    printf("\n%f",stud2.fees);
    if(stud1.fees>stud2.fees)
    {
        printf("\nThere is a difference between different student fees");
    }
   return 0;
}

Output

100.000000
200.000000

It is important to realize, that you can use an assignment operator on a structure variable

if(stud1.fees=0)

Another key point, you can use equality operators on a structure variable without any problem

if(stud1.fees!=0)

But, C does not allow to use of equality comparison on a structure variable

if(stud1.fees==0)

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.