3

Possible Duplicates:
function overloading in C
Does C support overloading ?

Can anyone explain if function overloading work in C?

I tried this and it didn't work:

#include <stdio.h>

int f(int val) {
    printf("f int\n");
    return 5;
}

int f(char *val) {
    printf("f char *\n");
    return 6;
}

int main() {
    f(5);
    f("moo");
}

The gcc C compiler says:

overload.c:8: error: conflicting types for 'f'
overload.c:3: error: previous definition of 'f' was here

However, if I compile the same code in C++, it works.

Can anyone explain it?

Thanks, Boda Cydo.

Community
  • 1
  • 1
bodacydo
  • 75,521
  • 93
  • 229
  • 319

2 Answers2

9

No, C has no function overloading.

Philipp
  • 48,066
  • 12
  • 84
  • 109
3

Function overloading is one of the additional features of C++ that are often described as "C++ as a better C".

It has nothing to do with the object-oriented features of C++.

Andy Dent
  • 17,578
  • 6
  • 88
  • 115