0
#include <stdio.h>

int Add(int a, int b);
int Add(int a, int b, int c);
double Add(double a, double b);

void main()
{
    printf("1+2=%d\n",Add(1,2));
    printf("3+4+5=%d\n",Add(3,4,5));
    printf("1.414+2.54=%f\n",Add(1.414,2.54));
}

int Add(int a, int b)
{
    return a+b;
}

int Add(int a, int b, int c)
{
    return a+b+c;
}

double Add(double a, double b)
{
    return a+b;
}

I wrote with C language and using Xcode. While studying "overload", Xcode keeps show error message that overload cannot be worked. Instead it shows "Conflicting types for 'Add'" message. With Xcode, would overload cannot be worked?

teggme
  • 37
  • 1
  • 5

4 Answers4

5

In Simple words, C doesn't allow function overloading! So, you can't write multiple functions with the same name!

Try to give different function name and try-

#include <stdio.h>
int Add2int(int a, int b);
int Add3int(int a, int b, int c);
double Add(double a, double b);

void main()
{
    printf("1+2=%d\n",Add2int(1,2));
    printf("3+4+5=%d\n",Add3int(3,4,5));
    printf("1.414+2.54=%f\n",Add(1.414,2.54));
}

int Add2int(int a, int b)
{
    return a+b;
}

int Add3int(int a, int b, int c)
{
    return a+b+c;
}

double Add(double a, double b)
{  
    return a+b;
}
Austin Mullins
  • 7,307
  • 2
  • 33
  • 48
Sathish
  • 3,740
  • 1
  • 17
  • 28
2

As mentioned C doesn't support function overloading (like in C++). Neverthless C99 introduced function-like macros with empty arguments, however commas must be preserved with exact number. Here is an example:

#include <stdio.h>
#define Add(a,b,c) (a+0)+(b+0)+(c+0)
int main(void)
{
    int a = 1, b = 2, c = 3;
    double x = 1.5, y = 2.25, z = 3.15;

    printf("%d\n", Add(a, b, c)); /* 6 */
    printf("%d\n", Add(a, b, ));  /* 3 */
    printf("%d\n", Add(, , c));   /* 3 */
    printf("%g\n", Add(, y, z));  /* 5.4 */
    printf("%g\n", Add(x, , ));   /* 1.5 */

    return 0;
}

Note that due to due usual arithmetic conversions for arguments with floating-point type 0 would be properly promoted to such type.

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
1

This may be only possible if you are using C++.
But in C you can't think of function overloading.
So try to change the function name and then it will work.

Anil Saraswati
  • 11
  • 1
  • 10
0

Actually C language does not allow function or method overloading To do method overloading you have to go to C++ or Java

Failed Scientist
  • 1,977
  • 3
  • 29
  • 48
Src7
  • 7
  • 5