Where is the function/operator where C finds out that two objects are the same?

Asked 2 years ago, Updated 2 years ago, 173 views

Where is the C standard function that determines whether two instances are the same or not? I used == or is in Python, but I can't find it in C

c struct equality

2022-09-22 13:23

1 Answers

Since there is no such function in C, the programmer must create a function that compares the members.

#include <stdio.h>
#include <stdbool.h>

struct Coord{
    int x, y;
};

bool CoordEqual(struct Coord const c1, struct Coord const c2){
    return (c1.x==c2.x && c1.y==c2.y);
}

int main(int argc, const char * argv[]) {
    struct Coord c1 = {3,5};
    struct Coord c2 = {3,5};
    struct Coord c3 = {5,3};

    printf("c1 == c2: %s\n", CoordEqual(c1,c2) ? "true" : "false");
    printf("c1 == c3: %s\n", CoordEqual(c1,c3) ? "true" : "false");
}


2022-09-22 13:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.