This is because nearest
does not return an int
. When you assign it to an int
, C does the conversion, so printing it with the %d
format is successful. When you call it, C leaves the value as is. That's why printing with %d
fails the second time.
Adding an explicit cast will fix this problem:
/*After*/
printf("nearest 1.5 ==> %d\n",(int)nearest(1.5));
printf("nearest 2.5 ==> %d\n",(int)nearest(2.5));
printf("nearest 2.6 ==> %d\n",(int)nearest(2.6));