tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] parameter declarations and long doubles


From: Dave Dodge
Subject: Re: [Tinycc-devel] parameter declarations and long doubles
Date: Thu, 8 Jun 2006 01:38:51 -0400
User-agent: Mutt/1.4.2i

On Wed, Jun 07, 2006 at 05:22:40PM -0700, Jim Cook wrote:
> I'm using -Wall -Werror, in case that helps, and using the Windows binary
> port.

tcc's error checking is not as rigorous as it could be.  For example
gcc 4.1.1 gives several warnings:

  $ gcc -g -O3 -std=c99 -pedantic -Wall -Wextra foo.c
  foo.c: In function 'main':
  foo.c:19: warning: format '%lf' expects type 'double', but argument 2 has 
type 'long double'
  foo.c:22: warning: large integer implicitly truncated to unsigned type
  foo.c:14: warning: unused parameter 'argc'
  foo.c:14: warning: unused parameter 'argv'
  foo.c:21: warning: large integer implicitly truncated to unsigned type

> static void test1(n)
> unsigned char n;
> {
>    printf("n is %u. n is < 2? %u\n", n, n < 2);
> }
[...]
>    test1(257);

The function is passed the value 257, with type int.  That much we can
say for sure.

The question is what happens to that value when the parameter type
cannot represent it.  The rules for functions with prototypes (such as
test2) are clear, and the value would be converted to an unsigned char
"as if by assignment", producing (on x86) the value 1.  Looking at the
Standard, I can't seem to get a clear reading of what happens for a
function without a prototype, though.

For example 6.5.2.2 suggests that the call is okay, because the
promoted argument type (int) is compatible with the promoted parameter
type (which would also be int).  But then it seems to say that no
implicit conversion is performed to the parameter type unless a
prototype is present.

Meanwhile 6.9.1 says that arguments are converted to parameter types
as if by assignment, and it _doesn't_ qualify that to only apply to
prototyped functions.

As far as what tcc implements, if you cast n to an unsigned int before
passing it to printf, it prints 257. If the function call is
considered undefined, then this bizarre behavior is allowed; otherwise
it's a tcc bug.  gcc does produce different results, but that doesn't
really prove anything.

My guess is that it _is_ a tcc bug.  The best and easist workaround is
to never use old-style function definitions in the first place.  It's
been 17 years since prototypes were standardized, after all.

>    long double ld = 3.14;
[...]
>    printf("%lf\n", ld);

For long double you should use %Lf, not %lf.  Also, keep in mind that
the actual implementation of "printf" is provided by the system's C
library, not tcc.

                                                  -Dave Dodge




reply via email to

[Prev in Thread] Current Thread [Next in Thread]