[FoRK] 2+2=5986

Andy Armstrong < andy at hexten.net > on > Fri Aug 4 09:09:44 PDT 2006

On 4 Aug 2006, at 16:56, Andy Armstrong wrote:
> Isn't it answered in Ch16? The downloadable PDF of Ch16 suggests it  
> is - although the listing in question is missing or intentionally  
> blank in the PDF. I presume it's something to do with screwing up a  
> printf() format string.

OK, I see what it is - on the page headed Why does 2+2=5986? there  
are two example programs

#include <stdio.h>
main() {
     int answer;
     answer = 2 + 2;
     printf("The answer is %d\n");
     return (0);
}

Why does 21/7 = 0

#include <stdio.h>
main(){
     float result;
     result = 21.0 / 7.0;
     printf("The result is %d\n", result);
     return (0);
}

On my system the first shows that 2 + 2 = -1078787380 and the second  
does indeed output "The result is 0". Can you see why? :)

-- 
Andy Armstrong, hexten.net


More information about the FoRK mailing list