-2

If I add "double" into statement then it will print 60.00 else the code will print 0.0000..

#include <stdio.h>

int main(void)
{
    void foo();// if i add "double" into statement then will printf 60.00
    char c = 60;
    foo(c);
    return 0;
}

void foo(double d)
{
    printf("%f\n", d);//now printf 0.0000
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
along
  • 1
  • duplicates: [Is there a difference between `foo(void)` and `foo()` in C++ or C?](https://stackoverflow.com/q/51032/995714), [What happens to the unspecified arguments in function()?](https://stackoverflow.com/q/62010502/995714), [Why can functions with no arguments defined be called with any number of arguments?](https://stackoverflow.com/q/36005527/995714) – phuclv Aug 23 '21 at 09:08

1 Answers1

1

Yor code invokes Undefined Behaviour as the caller does not know the parameters of foo. Your prototype says that the function will take an unspecified number of parameters. It does not say anything about the types of those parameters. So it does the implicit conversion to int and passes int to foo.

foo expects the double parameter.

What to do?

Add the function prototype befoe the function call.

void foo(double);

int main(void)
{
    char c = 60;
    foo(c);
    return 0;
}

void foo(double d)
{
    printf("%f\n", d);//now printf 60.0000
}
DevSolar
  • 67,862
  • 21
  • 134
  • 209
0___________
  • 60,014
  • 4
  • 34
  • 74