1

May be this is a simple question but I am not sure about how float variables are stored in memory and why it is behaving in this way, can someone please explain about the following behavior.

#include<stdio.h>

int main ()
{
    int a = 9/5;
    printf("%f\n", a);
    return 0;
}

Output:

0.000000

I have looked at some information on how float variables are stored in memory, it has stuff about mantissa, exponent and sign. But I am not getting how to relate that here.

alk
  • 69,737
  • 10
  • 105
  • 255
Ram
  • 1,153
  • 4
  • 16
  • 34

5 Answers5

6
int a = 9/5;

performs integer division and ignores the remainder, so a is set to 1. Attempting to print that using %f gives undefined behavior, but by chance you got 0.000000 out of it.

Do

double a = 9./5.;

instead, or print with %d if integer division was the desired behavior. (float would also work, but a will be promoted to double when passed to printf, so there's no reason not to use double.)

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
1

It is an undefined behaviour in C. Use %d format specifier instead of %f.

Does printf() depend on order of format specifiers? gives you detailed answer.

Community
  • 1
  • 1
Jeyaram
  • 9,158
  • 7
  • 41
  • 63
1

It is an Undefined Behaviour.

You are using float format specifier (%f) to print an int (a). You should use %d to see correct output.

Azodious
  • 13,752
  • 1
  • 36
  • 71
0

Here's a brief analysis of your code:

int a = 9/5; // 9/5 = 1.8, but since you are doing integer division and storing the value in an integer it will store 1.
printf("%f\n", a);//Using incorrect format specifiers with respect to datatypes, will cause undefined behavior

printf("%d\n",a);//This should print 1. And correct.

Or if you want the float:

instead of int use float :

   float a=9.0f/5;//This will store 1.800000f
   //float a=9/5 will store 1.000000 not, 1.8 because of integer divison 
   printf("%f\n",a); //This will print 1.800000

Also do read this: http://en.wikipedia.org/wiki/IEEE_754-2008 on how floating points work.

Clarification about integer division:

C99: 6.5.5 Multiplicative operators

6 When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded.88) If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a

88) This is often called ‘‘truncation toward zero’’.

askmish
  • 6,464
  • 23
  • 42
  • 9/5 makes an integer division. You probably meant 9.0/5.0f – Aniket Inge Oct 25 '12 at 10:06
  • 1
    9/5 is not 1.8, it is 1, since 9 is int and 5 is int. It doesn't matter whether you attempt to store the 1 into an int or a float, it doesn't affect the calculation. – Lundin Oct 25 '12 at 11:12
  • 1
    P.S: About Multiplicative operators 6 When integers are divided, `the result of the / operator is the algebraic quotient with any fractional part discarded`.88) If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a. And I must say, the integer is result, due to the truncation towards zero. – askmish Oct 25 '12 at 12:43
-1

Just assuming that your division should result in a fraction in normal math won't magically make it a float.

int a = 9/5;

You need to use

float a = 9.0/5;

First you need the right data type i.e. float (or better yet double for higher precision) to store a float.

Secondly, if you are dividing two integers i.e. 9 and 5, it will simply follow integer division i.e. only store the integer part of division and discard the rest. To avoid that i have added .0 after 9. This would force compiler to implicitly covert into float and do the division.

Regarding your mentioning of why it is printing 0, it is already mentioned that trying %f on integer is undefined behavior. Technically, a float is 4 bytes containing 3 byte number and 1 byte exponent and these are combined to generate the resultant value.

fkl
  • 5,412
  • 4
  • 28
  • 68
  • 9/5 is actually 1. So that's not what he is looking for. Why is floating point to integer conversion having a value of 0.00 for this particular problem – Aniket Inge Oct 25 '12 at 10:03
  • 9/5 is actually 1 it is obvious. I pasted it from question and added the correct version under it to clarify the difference. Where did i say he was looking for 1?? – fkl Oct 25 '12 at 10:14
  • Strictly speaking you should either write `float a = 9.0f / 5.0f` or `double a = 9.0 / 5.0`. As you wrote this example, you enforce the calculation to be done on a double, which you then truncate into a float. You make the code more ineffective for no gain. If you are lucky the compiler might be smart enough to optimize, but don't count on it. – Lundin Oct 25 '12 at 11:09
  • 1
    That's correct but very 'strictly speaking'. Considering the person asking the question. Preference of double or float should stay out of the way. As a teacher, i have always learned to illustrate but not complicate the topic. Even if it is demoted to float, it achieves the goal of generating the floating point value albeit with lesser precision. I certainly not see this as a reason for down vote or consider above a better explanation. But every one has his own opinions :) – fkl Oct 25 '12 at 11:14