Is it possible to overload operators in C?
Asked Answered
F

4

14

Is it possible to overload operators (such as operators of comparison) in C?

If so, how do you do it? I did a quick search, but all I found was for C++, and what I want is for C.

Anyone have any ideas?

Edit1: The idea is: I have a struct, and I need to do a comparison (based on a member of the struct). And for this I would like to associate operators compared to my new "data type".

Edit2: I am completely aware that I can do without the use of operator overloading, but was wondering if you can do this WITH OVERLOAD.

Answer: The concept of overload is associated with object-oriented programming. Since C is not object oriented and therefore can not contain a concept of overload. (:

Finding answered 16/5, 2012 at 18:30 Comment(6)
I'd rather do this in C: struct A; _Bool A_compare(const struct* A, const struct* A);Gemmell
I know. I'd like to use operators, if it was possible.Finding
If you want operator overloading then you'll need to shift to c++Heterolecithal
RE your edits: No, you can't. There is no such thing as operator overloading in C. You cannot define custom operators to work with your structs, in any way, at all, in C. Operator overloading is something you do in C++, it has nothing what so ever to do with C.Characharabanc
You say you have to compare two values of the same struct type. By compare, do you mean < kind or the == kind? If the former, is it for sorting? If so, qsort allows (requires) you to provide your own comparator.Elianore
What do you mean by "WITH OVERLOAD"? I don't think shouting at the compiler will help.Foeman
C
26

No, it is not possible. C does not support operator overloading by the developer.

Characharabanc answered 16/5, 2012 at 18:32 Comment(4)
Considering in this way an operator is not a specific kind of function?Finding
what do you mean by overload functions in C? C does not support function overloading.Gemmell
@xis19 No, it doesn't, my mistake. Richard: In C++, operators are very much like functions, and defining overloaded operators is done exactly the same way that you define functions.Characharabanc
@meagar I agree with your point. e.g. a*b is interpreted as a.operator*(b); and then operator*(a.this, b);Gemmell
M
7

If by overload, you mean user defined operator overloads, then the answer is no. However, some of the predefined operators such as *, + etc. are overloaded (if you think about it) for arithmetic types. The * is special since it also has an overload for de-referencing pointers.

Mottle answered 16/5, 2012 at 18:33 Comment(4)
The multiple* and dereference* are different. I think they are not overload.Gemmell
* isn't really all that special. There are two separate operators, one binary, the other unary, with the same name. The same happens with, for example, &. In both cases, the unary and binary operators are unrelated.Gaal
Yes they are different. Which is why I said the * is special and not overloaded.Mottle
@JerryCoffin: Differing in the number of arguments can be considered overloading.Mottle
B
3

C does not support overloading of operators or functions. There's no way you can redefine <, <=, >, >=, ==, or != to compare struct types directly.

Binder answered 16/5, 2012 at 19:26 Comment(0)
F
1

Since C11 you're able to overload functions, by using macros with _Generic. It provides a way to choose one of several expressions at compile time, based on a type of a controlling expression.

For example (It also works with more than one parameter):

#define f(X) _Generic((X),         \
        uint8_t:  f_uint8_t(X),    \
        uint16_t: f_uint16_t(X),   \
        uint32_t: f_uint32_t(X)    \
    )

So instead of using standard operators, you can define macros such as ADD(X, Y); SUB(X, Y), etc.

https://en.cppreference.com/w/c/language/generic

Freeswimming answered 2/4, 2022 at 14:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.