This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/32448] abs / printf bug
- From: "rob1weld at aol dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 24 Jun 2007 03:21:46 -0000
- Subject: [Bug c/32448] abs / printf bug
- References: <bug-32448-13830@http.gcc.gnu.org/bugzilla/>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- Comment #18 from rob1weld at aol dot com 2007-06-24 03:21 -------
(In reply to comment #16)
> (In reply to comment #14)
> > m.c: In function 'main':
> > m.c:9: warning: implicit declaration of function 'abs'
> Also, add a prototype for integer abs(), like "int abs(int);". Then everything
> will work as "expected".
> Rob
> ...why are the _third_ and _fourth_ answers different!
>Should the cast work without the extra parentheses.
#include <stdio.h>
int abs(int);
double fabs(double);
int main()
{
printf("%f %f %f %f\n", abs(1234.5678), fabs(1234.5678),
abs((int)1234.5678), abs((int)(1234.5678)));
return 0;
}
639356810344963382486834444601597699265113107384544848778287582042881171854556760809962494834238640539694738043963701847575050181760552125660054155932857305858816958002965674142423095065121202781270370782793943786455040.000000
0.000000 -0.422029 -0.000000
If I re-write and swap the 3rd and fourth abs'es I get:
#include <stdio.h>
int abs(int);
double fabs(double);
int main()
{
printf("%f %f %f %f\n", abs(1234.5678), fabs(1234.5678),
abs((int)(1234.5678)), abs((int)1234.5678));
return 0;
}
639356810344963382486834444601597699265113107384544848778287582042881171854556760809962494834238640539694738043963701847575050181760552125660054155932857305858816958002965674142423095065121202781270370782793943786455040.000000
0.000000 -1.333046 -0.000000
The same sort of output, so the parentheses are not at fault.
Printf does not cast it's "%i" / "%f" 's even with constants know at compile
time, let alone run-time. It is like you said in comment 2 "Especially with
%f."
So if you wanted to call functions by way of a pointer and they might return
int or float (or double) you would need code to test the type prior to
attempting to send it to multiple types of printf statement.
Using one printf for each possible combination with only 2 vars we would need 4
printf's. At least all the extra trouble buys us that it doesn't matter if they
are const or var :( here goes (there is no need for "_someone_" to post that
I'm missing a main() statement!):
...
switch (TYPE_TEST_FOR_2_VARS(x,y)) {
case TTF2V_I_I:
printf("X= %i Y=%i\n", x, y);
goto done;
case TTF2V_I_F:
printf("X= %i Y=%f\n", x, y);
goto done;
case TTF2V_F_I:
printf("X= %f Y=%i\n", x, y);
goto done;
case TTF2V_F_F:
printf("X= %f Y=%f\n", x, y);
goto done;
}
done:;
...
Same idea for 4 variables, just a lot more code ...
Gotcha, thanks Uros.
--
rob1weld at aol dot com changed:
What |Removed |Added
----------------------------------------------------------------------------
Summary|[3.3 / 3.4 / 4.1 / 4.2 / 4.3|abs / printf bug
|Regression] abs / printf bug|
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32448