Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANSI C and function overloading [duplicate]

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.

like image 974
Amir Saniyan Avatar asked Oct 20 '11 09:10

Amir Saniyan


People also ask

Why does C doesn't support function overloading?

Function overloading is a feature of Object Oriented programming languages like Java and C++. As we know, C is not an Object Oriented programming language. Therefore, C does not support function overloading.

Is it possible to overload a function in C?

This feature is present in most of the Object Oriented Languages such as C++ and Java. But C doesn't support this feature not because of OOP, but rather because the compiler doesn't support it (except you can use _Generic).

Does C support function overloading and overriding?

No, C does not support overloading, but it does support Variadic functions. printf is an example of Variadic functions.

Can two functions have the same name in C?

In C++, two functions can have the same name if the number and/or type of arguments passed is different. Here, all 4 functions are overloaded functions. Here, both functions have the same name, the same type, and the same number of arguments. Hence, the compiler will throw an error.


1 Answers

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);
like image 148
vicentazo Avatar answered Nov 14 '22 23:11

vicentazo