Couldnt understand how numbers are handled in C. Could anyone point to a good tutorial.
#include<stdio.h>
main()
{
printf("%f",16.0/3.0);
}
This code gave: 5.333333
But
#include<stdio.h>
main()
{
printf("%d",16.0/3.0);
}
Gave some garbage value: 1431655765
Then
#include<stdio.h>
main()
{
int num;
num=16.0/3.0;
printf("%d",num);
}
Gives: 5
Then
#include<stdio.h>
main()
{
float num;
num=16/3;
printf("%f",num);
}
Gives: 5.000000
printfis declared asthe first arg (
format) is string, and the rest can be anything. How the rest of the arguments will be used depending on the format specifiers informat. If you have:xwill be treated as int,ywill be treated aschar.So,
is ok, since you ask for
floatdouble (%f), passfloatdouble(16.0/3.0)you ask for int(%d), you pass
floatdouble (double and int have different internal representation) so, the bit representation of 16.0/3.0 (double) corresponds to bit representation of 1431655765(int).compiler knows that you are assigning to int, and converts it for you. Note that this is different than the previous case.