0

I was educated from here that

void f();
void f(int a) {
    printf("%d", a);
}

the parameter type list "wins". I did the following...

int fun(void); //parameter type list
int fun(int a, double b)//parameter type list
{
    printf("%d, %f", a, b);
    return 0;
}

I was expecting that compiler will throw an error (since overloading is not possible in c), but it didn't

Can anyone help me in this regards?

Thanks.

Community
  • 1
  • 1
Rachana Pal
  • 87
  • 1
  • 1
  • 5
  • what compiler are you using. It should definitely throw an error.. `error: conflicting types for ‘fun’` – Haris Aug 03 '15 at 17:58
  • Which comes with VS2013 – Rachana Pal Aug 03 '15 at 17:59
  • Yes you are correct, it is throwing an error. I checked in gcc. But why VS is not doing so? – Rachana Pal Aug 03 '15 at 18:01
  • 2
    @RachanaPal Are you sure that you're compiling with a VS C compiler, rather than C++? – Jashaszun Aug 03 '15 at 18:12
  • Are you maybe compiling it in C++ mode? – RamblingMad Aug 03 '15 at 18:12
  • @Jashaszun AFAIR it's the same compiler, distinguishing only by file name (extension). And visual studio doesn't provide an easy way (aka wizard) to create a `.c` file. –  Aug 03 '15 at 18:34
  • @FelixPalmen Yeah it does... you can create a new file -> select "New C++ File", and then in the file name box enter something that ends with ".c". Now you have a C file that it compiles with the C compiler. – Jashaszun Aug 03 '15 at 18:35
  • @RachanaPal ... "wins" is wrong: the declaration `void f();` just doesn't say anything about the parameters, that's perfectly valid but *bad practice* –  Aug 03 '15 at 18:36
  • @Jashaszun sure, that's what I do when I need it. But notice the "New **C++** File" ... it's like MS is trying to keep VS users from coding in [tag:c]. –  Aug 03 '15 at 18:37
  • @FelixPalmen Ah sorry, I thought that you actually didn't know how to do it. Yeah, I agree, it is weird that they don't have a "New C File" option. – Jashaszun Aug 03 '15 at 18:38

1 Answers1

0

Since you're using Visual Studio, the most likely reason for your situation is that your code is in a C++ file. Note that VS distinguishes between C and C++ files based on the file extension. Changing your file to a .c file rather than a .cpp file should do the trick.

(Note that when you Add New Item to create a source file, you can create a C file simply by adding a new C++ file and then changing the name of the created file to be .c, as in the image below.)

Add New Item Dialog

Jashaszun
  • 8,903
  • 3
  • 21
  • 51