Float value not the same which the function is used in another translation unit

62 views Asked by At

Here's my problem: I have two files, one containing my main() :

int main()
{
  float a;

  a = 90;
  test(a);
}

and the other one containing a test() function :

float test(float a)
{
  printf("a : %f\n", a);
}

So, I compile these files (gcc test.c main.c) and I execute the binary.

But why do the output is

a : 0.0000

and not

a : 90.0000

as expected?

1

There are 1 answers

0
Sourav Ghosh On

As mentioned in the comments below the actual post, missing to provide the proper signature (forward declaration / prototype) of a function to all the different translation unit using the function will result in a fallback to the function with a signature of

  • Returning an int (obsolete in latest standard)
  • Accepting any number of arguments with no type-checking.

In this case, then, the function call

test(a);

will be interpreted as a call to a function, returning int and passing one int argument which is actually a mismatch (expected int and actual float type), causing the undefined behavior, producing unwated result.