Is there any way to achieve function overloading in C? I am looking at simple functions to be overloaded like
foo (int a)
foo (char b)
foo (float c , int d)
I think there is no straight forward way; I'm looking for workarounds if any exist.
Is there any way to achieve function overloading in C? I am looking at simple functions to be overloaded like
foo (int a)
foo (char b)
foo (float c , int d)
I think there is no straight forward way; I'm looking for workarounds if any exist.
In the sense you mean — no, you cannot.
You can declare a
va_arg
function likevoid my_func(char* format, ...);
, but you'll need to pass some kind of information about number of variables and their types in the first argument — like
printf()
does.Here is the clearest and most concise example I've found demonstrating function overloading in C:
https://gist.github.com/barosl/e0af4a92b2b8cabd05a7
Try to declare these functions as
extern "C++"
if your compiler supports this, http://msdn.microsoft.com/en-us/library/s6y4zxec(VS.80).aspxThere are few possibilities:
Yes, sort of.
Here you go by example :
It will output 0 and hello .. from printA and printB.
Can't you just use C++ and not use all other C++ features except this one?
If still no just strict C then I would recommend variadic functions instead.