I just want to know if C supports over loading? As we use system functions like printf with different no of arguments. Help me out
-
2Duplicate: http://stackoverflow.com/questions/479207/function-overloading-in-c – Mar 20 '10 at 10:25
-
1See Leushenko's 2014 answer using C11 _Generic type selector in the dupe http://stackoverflow.com/questions/479207/function-overloading-in-c. – Dwayne Robinson Apr 16 '15 at 07:52
9 Answers
No, C doesn't support any form of overloading (unless you count the fact that the built-in operators are overloaded already, to be a form of overloading).
printf
works using a feature called varargs. You make a call that looks like it might be overloaded:
printf("%d", 12); // int overload?
printf("%s", "hi"); // char* overload?
Actually it isn't. There is only one printf function, but the compiler uses a special calling convention to call it, where whatever arguments you provide are put in sequence on the stack[*]. printf (or vprintf) examines the format string and uses that to work out how to read those arguments back. This is why printf isn't type-safe:
char *format = "%d";
printf(format, "hi"); // undefined behaviour, no diagnostic required.
[*] the standard doesn't actually say they're passed on the stack, or mention a stack at all, but that's the natural implementation.

- 273,490
- 39
- 460
- 699
-
I can't speak for all compilers, but the one I work with has the caller adjust the stack for the args it pushed, once the function returns. This seems to be a good idea to me for variadic functions -- safer than having the function pop them from the stack. – tomlogic Feb 28 '10 at 17:42
-
1You're right, actually the cdecl calling convention works exactly like that (the caller is responsible for the stack cleanup); I deleted the comment to avoid confusing other readers. The real issue is with scanf, that, if you provide an incorrect format string, may write the data at random locations (taken from the stack) in memory. – Matteo Italia Feb 28 '10 at 17:47
-
No, C does not support overloading, but it does support Variadic functions. printf is an example of Variadic functions.

- 445,704
- 82
- 492
- 529
It all depends on how you define "support".
Obviously, C language provides overloaded operators within the core language, since most operators in C have overloaded functionality: you can use binary +
with int
, long
and with pointer types.
Yet at the same time C does not allow you to create your own overloaded functions, and C standard library also has to resort to differently-named functions to be used with different types (like abs
, fabs
, labs
and so on).
In other words, C has some degree of overloading hardcoded into the core language, but neither the standard library nor the users are allowed to do their own overloading.

- 312,472
- 42
- 525
- 765
No, C doesn't support overloading. If you want to implement overloading similar to C++, you will have to mangle your function names manually, using some sort of consistent convention. For example:
int myModule_myFunction_add();
int myModule_myFunction_add_int(int);
int myModule_myFunction_add_char_int(char, int);
int myModule_myFunction_add_pMyStruct_int(MyStruct*, int);

- 151
- 1
- 4
There is no provision in the C standard for operator overloading; proposals to add it have been rejected on the basis that many build systems have no facility to accommodate multiple functions with the same name. While C++ can work around this by e.g. having
void foo(int);
int foo(char*);
long foo(char *, char **);
compile to functions named something like v__foo_i, i__foo_pc, and l__foo_pc_ppc [compilers use different naming conventions, though the C++ standard forbids the use of internal double-underscores in identifiers so as to allow compilers to give things names like the above without conflict]. The authors of the C standard did not want to require any compilers to change naming conventions to allow for overloading, so they don't provide for it.
It would be possible and useful for a compiler to allow overloading of static and inline functions without creating naming problems; this would in practice be just as useful as allowing overloading of externally-linkable functions since one could have a header file:
void foo_zz1(int);
int foo_zz2(char*);
long foo_zz3(char *, char **);
inline void foo(int x) { foo_zz1(x); }
inline int foo(char* st) { foo_zz2(st); }
long foo(char *p1, char **p2) { foo_zz3(p1,p2); }
I recall looking at an embedded compiler for a hybrid between C and C++ which supported the above as a non-standard extension, but I'm not positive about the details. In any case, even if some C compilers do support overloading of functions which do not have external linkage, it is not supported by C14 nor am I aware (unfortunately) of any active efforts to add such a feature to future C standards.
Nonetheless, GCC can be made, using macros, to support a form of overloading which is not supported directly in languages with operator overloading. GCC includes an intrinsic which will identify whether an expression can be evaluated as a compile-time constant. Using this intrinsic, one can write a macro which can evaluate an expression different ways (including by calling functions) depending upon the argument. This can be useful in some cases where a formula would evaluate as a compile-time constant if given a compile-time constant argument, but would yield a horrible mess if given a variable argument. As a simple example, suppose one wishes to bit-reverse a 32-bit value. If the value is constant, one could do that via:
#define nyb_swap(x) \
((((x) & 1)<<3) | (((x) & 2)<<1) | (((x) & 4)>>1) | ((((x) & 8)>>3) )
#define byte_swap(x) \
( (nyb_swap(x)<<4) | nyb_swap((x) >> 4) )
#define word_swap(x) \
( (byte_swap(x)<<24) | (byte_swap((x) >> 8)<<16) | \
(byte_swap((x) >> 16)<<8) | (byte_swap((x) >> 24)) )
And an expression like uint32_t x=word_swap(0x12345678);
would simply load x
with 0x87654321. On the other hand, if the value is not a constant, the result would be horrible: an expression like uint32_t y=word_swap(x);
might generate many dozens of instructions; a call to a function with a partially-unrolled loop would be almost as fast but a lot more compact. On the other hand, using a loop would prevent the result from being regarded as a compile-time constant.
Using GCC, one can define a macro which will either use the constant-yielding macro if given a constant, or call a function when given a variable:
#define wswap(x) \
(__builtin_constant_p((x)) ? word_swap((x)) : word_swap_func((x))
This approach can't do everything type-based overloading can do, but it can do many things overloading can't.

- 77,689
- 9
- 166
- 211
Not directly, and this is not how printf
works, but it is possible to create the equivalent of overloaded functions using macros if the types are of different sizes. The type-generic math functions in tgmath.h of the C99 standard may be implemented in that manner.

- 48,893
- 5
- 92
- 171
C Does not support overloading. But we can implement that functionality by programming our own library that in turn could provide overloading support.

- 605
- 4
- 10
- 15
-
2Not sure what you mean. What would a library providing overloading support look like? – sepp2k Feb 28 '10 at 17:12
-
@Carl: It sounds like you're talking about a vtable. That's for inheritance, I'm not sure how that would help with overloading. – sepp2k Feb 28 '10 at 18:13
-
2you can emulate overloading with a function that takes void pointers as arguments - the trick is knowing at runtime what to do with those void pointers, you will probably need type tagging of some sort (you could consider the format string in printf a rudimentary type tagging). – Justin Smith Feb 28 '10 at 22:46
No c does not support function overloading. But you can get it to compile/work if you are using g++ (a c++ compiler).

- 609
- 3
- 5
-
4Seconded. If you're using a C++ compiler to compile your code, what you are writing is C++ code, not C code. Especially if you are using features from C++ that are not in C, such as function overloading! – Brooks Moses Feb 28 '10 at 21:40