Alistair Mcfarlane Alistair Mcfarlane - 1 year ago 65
C Question

is there a different syntax for functions with double in c?

I'm new to coding in C so im sure this is a basic question. in my mind, this code should read an input, run the rcall() which does nothing, and output the same value. This works perfectly with int() values, but as soon as I switch to double, the output doesn't change from -6158. How do fix this logic error?

double input, output, rcall(x);

int main(void){
scanf("%lf", &input); /*read the input*/
output=rcall( input); /*call the function*/
printf("%lf", output); /*print the output*/

double rcall(x){ /*this function does nothing*/
return x;

Answer Source

The problem here is that you are using a 17 year old compiler, or have your current compiler incorrectly configured to compile the code as if it was older than 17 years.

The declaration double rcall(x); is nonsense in modern C.

But ancient C allowed sloppy declarations where not all types were specified, or even allowed you to call functions that had no declaration. The compiler would then always "helpfully" assume that those types that weren't explicitly specified are all int. If it turned out that they actually weren't, your program would then crash and burn.

This sheer stupid system was removed from the C language in the year 1999, with the "C99" standard. In modern C, your declaration should be

double rcall (double x);

and the definition should be

double rcall (double x)
  return x;

In case you are using GCC, you can configure it to correct code according to modern standard C by adding the options gcc -std=c11 -pedantic-errors. Then you would have gotten a compiler error for the original code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download