[Bug c/15207] New: Problem with float in local-scope function declarations

gandalf at winds dot org gcc-bugzilla@gcc.gnu.org
Thu Apr 29 18:27:00 GMT 2004


In GCC versions earlier than 3.4.0, I get unpredicted results running the
following code:  (tested on i686-pc-gnu-linux and hppa2.0w-hp-hpux11.00)
 
---test.c---
 
int main()
{
  int test(float);
 
  float k=470.82;
  int j;
 
  test(k);
 
  printf("Main=%f\n", k);
  return 0;
}
 
int test(f)
float f;
{
  printf("Test=%f\n", f);
  return 0;
}
 
-----------
 
Command: gcc test.c -o test
 
The resulting output shows this with both GCC 2.95.3 and 3.3:
 
  Test=0.000000
  Main=470.820007
 
In the above code, Test= is erroneously showing 0 instead of the 470.82 number.
Outside of this simple test case, Test= sometimes shows random floating-point
values other than 0.
 
Now, to try to remedy this problem, I first upgraded GCC to 3.4.0. When I went
to compile it, I got the following error:
 
  test.c:15: error: conflicting types for 'test'
  test.c:3: error: previous declaration of 'test' was here
 
Now as far as I know, I don't see any conflicting type for 'test' up there.
They both return 'int' and they both have 1 argument of type 'float'. So I
tried changing the code to see if I could fix the conflicting type.
 
I came up with 2 solutions, both which work on GCC 2.95.3, 3.3, AND 3.4.0.
 
Solution 1: in the definition of 'test(f)', move the 'float' keyword up to the
spot where the function parameter is.
 
---test1.c---
 
int main()
{
  int test(float);
 
  float k=470.82;
  int j;
 
  test(k);
 
  printf("Main=%f\n", k);
  return 0;
}
 
int test(float f)
{
  printf("Test=%f\n", f);
  return 0;
}
 
-------------
 
The above code compiles and works on all 3 compiler versions. It outputs:
 
  Test=470.820007
  Main=470.820007
 
Solution 2: Move the 'int test(float);' function declaration out of main().
 
---test2.c---
 
int test(float);
 
int main()
{
  float k=470.82;
  int j;
 
  test(k);
 
  printf("Main=%f\n", k);
  return 0;
}
 
int test(f)
float f;
{
  printf("Test=%f\n", f);
  return 0;
}
 
--------------
 
The above code ALSO compiles correctly on all 3 compiler versions, outputting:
 
  Test=470.820007
  Main=470.820007
 
So, I think I've hit a bug only where both cases are true: The function is
declared in the scope of main() (or any other function), And the function
definition itself uses the old C-style parameter types.
 
Thanks,
 -Byron Stanoszek  <gandalf@winds.org>

-- 
           Summary: Problem with float in local-scope function declarations
           Product: gcc
           Version: 3.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: gandalf at winds dot org
                CC: gcc-bugs at gcc dot gnu dot org
  GCC host triplet: i686-pc-linux-gnu & hppa2.0w-hp-hpux11.00


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15207



More information about the Gcc-bugs mailing list