[LCP]puzzling

Andrew Weaver Andrew.Weaver at tecnomen.fi
Fri Feb 28 17:18:01 UTC 2003


On Friday, February 28, 2003 10:08 AM, Greg Black [SMTP:gjb at gbch.net] wrote:
> Kurian Abraham Kurian Abraham-A18108 wrote:
> 
> | int main()
> | {
> |   long       a = 13;
> |   long       b = 197;
> |   long       c ;
> | 
> |   c = (b-a)*16.66;
> |   printf("\nAmazing = %ld",(b - a)*(16.66));
> |   printf("\nC is %ld\n",c);
> |   return 0;
> | }
> | 
> | pls run this program....and tell me why the answers r different?
> 
> They are expected to be different -- the program is wrong.
> 
> In the "amazing" line, you tell printf() to expect a long, but
> you pass it a double.  You get what you asked for.
> 
> Greg
> 

Indeed. For further "amusement"look at this...

#include <stdio.h>
int main()
{
long a = 13, b = 197, c, d, e;

  d = (b-a);
printf("\nPre D = %ld", d);
  c = (b-a)*16.66;
printf("\nPre C = %ld", c);
  e = d * 16.66;
printf("\nPre E = %ld", e);


  printf("\nA       = %ld"
         "\nB       = %ld"
         "\nAmazing = %ld"
         "\nC       = %ld"
         "\nD       = %ld"
         "\nE       = %ld\n",
          a, b, (b - a)*(16.66),
          c, d, e);

  printf("\nA       = %ld"
         "\nB       = %ld"
         "\nC       = %ld"
         "\nD       = %ld"
         "\nE       = %ld\n",
          a, b, c, d, e);
  return 0;
}

My theory is that the calc of the varargs going on inside printf...
_______________________________________________
> This is the Linux C Programming List
> :  http://lists.linux.org.au/listinfo/linuxcprogramming List



More information about the linuxCprogramming mailing list