Search code examples
ccomparison-operators

Understanding a function in Linux source code


I have an assignment related to scheduling in which I need to modify Linux source code. I came across a piece of code which I don't understand.

static inline int entity_before(struct sched_entity *a, struct sched_entity *b) {
     return (s64)(a->vruntime - b->vruntime) < 0;
}

what does this line return (s64)(a->vruntime - b->vruntime) < 0; return? To me it seems it is returning a Boolean value like in Java, if the statement is true then it returns 1 and if it's false then it returns 0? As C language doesn't have a boolean data type.


Solution

  • The line returns 0 or 1. The result of operator < is an int value of 1 if the condition is (logically) true, or an int type of value 0 if the condition holds false.

    To me it seems it is returning a Boolean value like in java, if the statement is true then it returns 1 and if its false then it returns 0?

    Yes.

    as C language doesn't have a boolean data type.

    is false - C language has _Bool data type. Still, operator < returns an int, not a _Bool.

    See cppreference comparision operators