6

Possible Duplicate:
function overloading in C

ANSI C doesn't permit function overloading (I don't sure about C99).

for example:

char  max(char  x, char  y);
short max(short x, short y);
int   max(int   x, int   y);
float max(float x, float y);

is not a valid ANSI C source code.

Which technique (or idea) should be used for function overloading problem in ANSI C?

Note:

An answer is renaming the functions, but which pattern should be used for renaming, that function names remain 'good function name'?

for example:

char  max1(char  x, char  y);
short max2(short x, short y);
int   max3(int   x, int   y);
float max4(float x, float y);

is not a good naming for max function name.

Community
  • 1
  • 1
Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137

2 Answers2

11

Using the data type to be evaluated in the function name, for example

char  max_char(char  x, char  y);
short max_short(short x, short y);
int   max_int(int   x, int   y);
float max_float(float x, float y);
vicentazo
  • 1,739
  • 15
  • 19
  • 1
    This is what the standard library does - for example we have `atoi()`, `atol()`, `atof()` and `atod()`. – caf Oct 20 '11 at 11:56
0

In this example, the proper solution is using a macro. You could also simply use an inline function that takes the largest-possible integer or floating point type and let the compiler optimize it down when the argument is known to be smaller. There are some corner cases you should consider with regards to signedness etc. but those happen already anyway.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711