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. (:

markgz
  • 6,054
  • 1
  • 19
  • 41
richardaum
  • 6,651
  • 12
  • 48
  • 64
  • 1
    I'd rather do this in C: struct A; _Bool A_compare(const struct* A, const struct* A); – xis May 16 '12 at 18:38
  • I know. I'd like to use operators, if it was possible. – richardaum May 16 '12 at 18:40
  • If you want operator overloading then you'll need to shift to c++ – Peter M May 16 '12 at 18:43
  • 2
    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. – user229044 May 16 '12 at 18:44
  • What do you mean by "WITH OVERLOAD"? I don't think shouting at the compiler will help. – Skizz May 16 '12 at 19:34
  • 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. – Happy Green Kid Naps May 16 '12 at 19:00

4 Answers4

24

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

Eric Schnipke
  • 482
  • 1
  • 6
  • 22
user229044
  • 232,980
  • 40
  • 330
  • 338
  • Considering in this way an operator is not a specific kind of function? – richardaum May 16 '12 at 18:33
  • 2
    what do you mean by overload functions in C? C does not support function overloading. – xis May 16 '12 at 18:33
  • 1
    @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. – user229044 May 16 '12 at 18:33
  • @meagar I agree with your point. e.g. a*b is interpreted as a.operator*(b); and then operator*(a.this, b); – xis May 16 '12 at 18:36
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.

dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • The multiple* and dereference* are different. I think they are not overload. – xis May 16 '12 at 18:35
  • 3
    `*` 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. – Jerry Coffin May 16 '12 at 18:35
  • Yes they are different. Which is why I said the `*` is special and not overloaded. – dirkgently May 16 '12 at 18:35
  • @JerryCoffin: Differing in the number of arguments can be considered overloading. – dirkgently May 16 '12 at 18:44
3

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

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

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