float functions

Tommy Levitte pk-tle at nada.kth.se
Thu Aug 4 19:11:35 AEST 1988


>> How do I get c to recognize a function as returning a float and not
>> a double?  It seems the moment I declare something as a function, the
>> compiler cast it as double.  I am primarily using sizeof to determine
>> the type.
>> 
>> Here is a sample code.
>> 
>> float f1()
>> {  return( (float) 10.0);
>> }
>> 
>> main()
>> {  float f1(), res;
>>    res = f1();
>>    printf("sizeof f1() is %d\n",sizeof(f1()));
>>    printf("sizeof res is %d\n",sizeof(res));
>> }
>> 
>> This program invariable returns 8 and 4, not 4 and 4.   Help.
>> 
> ...
>
>Since the Second Edition of K&R follows the proposed ANSI standard and
>your compiler may not, what I suspect is happening is that your
>compiler's version of sizeof is returning the number of bytes in a
>POINTER TO A FUNCTION, which could very well BE 8 bytes on your system.

WRONG!
to get the size of the pointer to the function f1(), you should use sizeof(f1).
Using sizeof(f1()) does return the size of the return value of f1().

Now there are a few rules which applies on parameter passing and value 
returnings.

  - A char is converted to an int.
  - A float is converted to a double.

This also applies in an expression of any kind.

So any float function will in fact return a double! And any float parameter
will in fact be a double!

Of course, at assignment time, it is converted back to float, if required.

Just to convince you and myself, I made this TurboC test:

float far f1(dummy1,x,y,dummy2)
float x;
double y;
int dummy1,dummy2;
{
   printf("In f1 :\n");
   printf("  &dummy1 = %lX\n",&dummy1);
   printf("  &x = %lX\n",&x);
   printf("  &y = %lX\n",&y);
   printf("  &dummy2 = %lX\n",&dummy2);
   printf("  sizeof(x) = %d\n",sizeof(x));
   printf("  sizeof(y) = %d\n",sizeof(y));
   printf("  sizeof(x+y) = %d\n",sizeof(x+y));
   printf("Exiting f1\n");
   return x+y;
}

main()
{
  float res;

  printf("sizeof(f1) = %d\n",sizeof(f1));
  printf("sizeof(f1()) = %d\n",sizeof(f1())); /* This is the size of the
                                                      return value */
  res=f1(0,3.0,4.0,0);
  printf("sizeof(res) = %d\n",sizeof(res));
  printf("sizeof(res+1.0) = %d\n",sizeof(res+1.0));
}

And I got the result is the following (I add some C-format comments):

sizeof(f1) = 4		/* The pointer to the function is 4 bytes */
sizeof(f1()) = 8	/* But this is the size of a double ! */
In f1 :
  &dummy1 = 19280FC2
  &x = 19280FC4	/* As you can see, x occupies the size of a double (8 bytes) */
  &y = 19280FCC	/* And so does y */
  &dummy2 = 19280FD4
  sizeof(x) = 4	/* At compile time, anyway, a float param is a float */
  sizeof(y) = 8
  sizeof(x+y) = 8	/* The expression is a double */
Exiting f1
sizeof(res) = 4		/* res is a float */
sizeof(res+1.0) = 8	/* but a float expression is allways double */
-- 
-------------------------------------------------------------------------------
Tommy Levitte (pk-tle at draken.nada.kth.se or gizmo at kicki.stacken.kth.se)
-------------------------------------------------------------------------------



More information about the Comp.lang.c mailing list