3

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.

2
  • Many C compilers support inline functions, but C doesn't have operator overloading. In fact, according to Wikipedia, C99 supports inline functions as part of the standard. Commented Mar 8, 2012 at 0:13
  • 1
    possible duplicate of Operator overloading in C Commented Mar 8, 2012 at 0:14

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Do what? Put a function pointer in a structure? struct x { void (*functionPointer)(void) };
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.
6

No it is not possible.

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

Comments

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.

Comments

0

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

Comments

0

C does not support operator overloading.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.