0

Is it possible to do operator overloading or something similar (inline function?) in C? I know that c does not support class, but could I make an operator for a struct?

I cannot find anything about this online, because Google will ignore '+' so if I try to google this I only get C++ results.

Stas Jaro
  • 4,597
  • 5
  • 29
  • 53
  • Many C compilers support inline functions, but C doesn't have operator overloading. In fact, according to [Wikipedia](http://en.wikipedia.org/wiki/C99), C99 supports inline functions as part of the standard. – Adam Mihalcin Mar 08 '12 at 00:13
  • 1
    possible duplicate of [Operator overloading in C](http://stackoverflow.com/questions/3417413/operator-overloading-in-c) – Tim Cooper Mar 08 '12 at 00:14

5 Answers5

9

No, you can't do that in C. Use C++ if you want to overload operators.

You can put function pointers inside a structure if you want a sort of C++ object-like behaviour.

Carl Norum
  • 210,715
  • 34
  • 410
  • 462
  • 1
    Do what? Put a function pointer in a structure? `struct x { void (*functionPointer)(void) };` – Carl Norum Mar 08 '12 at 00:17
  • Operator overloading is in fact the reason I switched to C++ for a project I worked on -- it is a lot more convenient to use an overloaded operator than a normal function when doing vector math. – Demi Sep 08 '13 at 14:18
4

No it is not possible.

By the way, you can remove C++ from google search results if you add -"C++" to your search query.

Caner
  • 53,818
  • 35
  • 164
  • 173
1

C++ introduced an important and interesting feature which is operator overloading.

So you will have to use it if you want to use this feature.

ChapMic
  • 25,816
  • 1
  • 20
  • 20
0

C does not support operator overloading or having functions inside structs. You will need to use C++ for those features.

Mark Robinson
  • 3,077
  • 1
  • 23
  • 36
0

C does not support operator overloading.

Brian Mains
  • 50,194
  • 35
  • 142
  • 253
Aditya Naidu
  • 1,372
  • 9
  • 12