C program to compare the structure variables

In C programming language, a structure is a collection of different datatype variables, which are grouped together under a single name. While C doesn't provide a built-in operator to compare entire structures directly, we can compare structure variables by comparing their individual members.

Syntax

struct tagname {
    datatype member1;
    datatype member2;
    datatype membern;
};

Structure Declaration and Initialization

The general form of a structure declaration is as follows −

struct tagname {
    datatype member1;
    datatype member2;
    datatype membern;
};

Here,

  • struct is a keyword.
  • tagname specifies the name of structure.
  • member1, member2 specify the data items that make up structure.

For example −

struct book {
    int pages;
    char author[30];
    float price;
};

Structure Variable Declaration Methods

There are three methods of declaring structure variables −

Method 1: Declaration with Structure Definition

struct book {
    int pages;
    char author[30];
    float price;
} b;

Method 2: Anonymous Structure

struct {
    int pages;
    char author[30];
    float price;
} b;

Method 3: Separate Declaration

struct book {
    int pages;
    char author[30];
    float price;
};
struct book b;

Example 1: Basic Structure Comparison

This example demonstrates how to compare structure variables by comparing their individual members −

#include <stdio.h>
#include <string.h>

struct class {
    int number;
    char name[20];
    float marks;
};

int main() {
    struct class student1 = {001, "Hari", 172.50};
    struct class student2 = {002, "Bobby", 167.00};
    struct class student3;
    
    /* Copy student2 to student3 */
    student3 = student2;
    
    /* Compare student3 with student2 */
    int x = ((student3.number == student2.number) &&
             (strcmp(student3.name, student2.name) == 0) &&
             (student3.marks == student2.marks)) ? 1 : 0;
    
    if (x == 1) {
        printf("student2 and student3 are same<br>");
        printf("%d %s %.2f<br>", student3.number, student3.name, student3.marks);
    } else {
        printf("student2 and student3 are different<br>");
    }
    
    return 0;
}
student2 and student3 are same
2 Bobby 167.00

Example 2: Function to Compare Structures

This example shows a more structured approach using a comparison function −

#include <stdio.h>
#include <string.h>

struct employee {
    int id;
    char name[30];
    float salary;
};

int compareEmployees(struct employee emp1, struct employee emp2) {
    return (emp1.id == emp2.id &&
            strcmp(emp1.name, emp2.name) == 0 &&
            emp1.salary == emp2.salary);
}

int main() {
    struct employee emp1 = {101, "John", 50000.0};
    struct employee emp2 = {102, "Alice", 55000.0};
    struct employee emp3 = {101, "John", 50000.0};
    
    if (compareEmployees(emp1, emp2)) {
        printf("emp1 and emp2 are identical<br>");
    } else {
        printf("emp1 and emp2 are different<br>");
    }
    
    if (compareEmployees(emp1, emp3)) {
        printf("emp1 and emp3 are identical<br>");
    } else {
        printf("emp1 and emp3 are different<br>");
    }
    
    return 0;
}
emp1 and emp2 are different
emp1 and emp3 are identical

Key Points

  • C does not support direct comparison of structures using == operator.
  • Each member must be compared individually for complete structure comparison.
  • For string members, use strcmp() function for comparison.
  • Structure assignment (=) is supported and copies all members.

Conclusion

Comparing structure variables in C requires member-by-member comparison since there's no built-in comparison operator. Using functions for comparison improves code readability and reusability.

Updated on: 2026-03-15T14:02:09+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements