Does function overloading work in C?

Possible duplicates:
function overload in C
Does C support overload?

Can someone explain if the overload function works in C?

I tried this and it did not 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 this?

Thanks, Boda Sido.

+3
source share
2 answers

No, C has no function overload.

+9
source

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

This has nothing to do with the object-oriented functions of C ++.

+3
source

Source: https://habr.com/ru/post/1754097/


All Articles