Code:void printFloat() { printf("%f\n",2/3); } int main() { printFloat(); float a = 2/3; printf("%f\n",a); return 0; }
Code:void printFloat() { printf("%f\n",2/3); } int main() { printFloat(); float a = 2/3; printf("%f\n",a); return 0; }
I know:
because 2 and 3 is int type.
Yup. You can force it to do the calculation in floating point:
Now because one of the numbers is floating point, the calculation is done that way and the result will be float as well.Code:2.0/3
2/3 (assuming that's two divided by three) is an integer. Try this.
Output:Code:void printFloat() { printf("%d\n",2/3); } int main() { printFloat(); int a = 2/3; printf("%d\n",a); return 0; }
It gives you two zeros because the program sees anything less than one is zero. I think there's a function that gives you the actual decimal, but if you were to get the actual decimal of two divided by three, the screen would flood with sixes.Code:0 0
Last edited by Babkockdood; 04-29-2010 at 07:43 PM.
printFloat doesn't actually print a float. You're still using %d, and, you're still using integers.Code:void printFloat() { printf("%d\n",2/3); }
Quzah.
Hope is the first step on the road to disappointment.