Tuesday, 21 August 2012

Does C support function overloading like C++?

Look at the printf() function in C, that may lead one to think that C supports function overloading. Because, in C you can have printf("%d", DecimalValue) and printf("%f", FloatValue). This looks a lot like function overloading, because we are using the same function name to handle different arguments differently.

Actually, this is not a case of function overloading – the printf function is just using a feature of C known as variable argument lists. This should not be confused with function overloading. So, to answer the question, Standard C does not support function overloading.

As an interesting side note, C++ doesn’t really have function overloading. What it does have is a means of faking it: the C++ compiler actually ‘mangles’ (or changes) function names according to the function’s parameters. So, functions that share the same name but have different numbers or types of parameters can be differentiated when invoked. Also, since the ‘mangling’ of function names is not standardized, it’s usually difficult to link object files compiled by different C++ compilers.

No comments:

Post a Comment