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,747
  • 5
  • 31
  • 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) –  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
  • 219,201
  • 40
  • 422
  • 469
  • 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
  • 57,267
  • 35
  • 174
  • 180
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
  • 26,954
  • 1
  • 21
  • 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,135
  • 1
  • 22
  • 37
0

C does not support operator overloading.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
Aditya Naidu
  • 1,362
  • 9
  • 12