Overloading in C…..? ..Its possible…..

hi everyone..

since we started studying C language, we have been taught that method overloading is not possible in C..like everyone, even I believed that..
But yesterday I learnt that it is possible..by including a header file named STDARG.H.

This header file lets you create variadic functions.i.e. functions with variable arguments.
To perform method overloading, you just have to include this header file and declare a function which you intent to “overload” or provide variable inputs.

syntax:-
return type function-name( datatype1 arg1, datatype2 arg2,…);

Note:- the last 3 dots in the list(…), this is the one which tells the compiler that it would be taking variable arguments. It is mandatory to include these 3 dots(…) else your function would not accept variable inputs.

You can make a function which accepts same typed arguments or different.

The procedure to be followed is as follows:
1. you need to declare a variable “va_list” for iterating through the variable list.
2. then use macros like “va_start” – to start iterating through the variable list va_list.This macro takes two arguments. First the variable with type va_list and second is the name of the last argument given in the list. In the syntax given above , it would be arg2.
3. va_arg- to retrieve an argument from the list. In this macro you need to provide two arguments. First bariable of type va_list and second is the datatype of the next argument to be fetched.
4. finally, va_end macro- to find the end to the variable list.

Its really fun to work with this feature of C and perform a function which was said not to be possible in C.

This was just a jist about the header file and its macros.

Happy C Programming..!!!