Kieran
Kieran

Reputation: 25

A newbie question about operations with integers and floating-points in C

I'm a C language newbie. Just trying to figure out why one code example from the textbook "Absolute Beginner's Guide to C, 3rd Edition" works like that:

// Two sets of equivalent variables, with one set
// floating-point and the other integer

float a = 19.0;
float b = 5.0;
float floatAnswer;

int x = 19;
int y = 5;
int intAnswer;

// Using two float variables creates an answer of 3.8
floatAnswer = a / b;
printf("%.1f divided by %.1f equals %.1f\n", a, b, floatAnswer);

floatAnswer = x /y; //Take 2 creates an answer of 3.0
printf("%d divided by %d equals %.1f\n", x, y, floatAnswer);

// This will also be 3, as it truncates and doesn't round up
intAnswer = a / b;
printf("%.1f divided by %.1f equals %d\n", a, b, intAnswer);

The second output is not understandable to me. We take integers so why is there a floating-point "3.0"?

The third output is not clear too. Why is there 3 when we take floating-point numbers like 19.0 and 5.0?

Pls help

Upvotes: 0

Views: 100

Answers (6)

Hitokiri
Hitokiri

Reputation: 3699

You can image as below:

floatAnswer = x /y;

From right to left, the program will calculate:

  1. temp = x/y: because x and y; they are integer, so temp = 3.
  2. floatAnswer = temp, now temp = 3, so floatAnswer = 3
intAnswer = a / b;

From right to left:

  1. temp = a/b will be 3.8
  2. intAnswer = temp, but intAnswer is integer, so temp is cast to 3

Upvotes: 0

Adrian Mole
Adrian Mole

Reputation: 51915

In the second example, the right-hand side (RHS) of the assignment is evaluated first: this is a division of two integers, so it is performed as such (an integer operation), and the result is 3; this is then converted to a float, in order to fulfil the assignment, and the conversion from an integral 3 cannot have any fractional part. However, the left-hand side is a float and, in the printf format, you are explicitly asking for 1 decimal place in the output - even though that is (and must be) zero.

In the third example, the RHS is evaluated as a floating-point division, which will (as you suspect) give an interim value of 3.8; however, when this is then converted to an int, in order to fulfil the assignmment, the fractional part (.8) is lost, as an integer cannot have any fractional component. Conversion from a floating-point type to an integer will always truncate (discard) any fractional part, so even converting 1.999999999 will give a value of 1.

Upvotes: 2

myradio
myradio

Reputation: 1805

Second case.

In the second case you assign the result of the int operation to a float.

int a = 19;
int b = 5;
float floatAnswer = a/b;

In that last line you assign an int to a float, which is implicitely casted. But the division operation was done using integer arithmetic. The cast is the last step (when it is assigned).

So basically, that's equivalent to

float floatAnswer = 3;

which is doing the same as,

float floatAnswer = (float)3;

Note the (float). That means that the value 3 is being casted (converted) to float.

Third case.

In the third case you assign the result of 19.0/5.0 to an int,

float a = 19.0;
float b = 5.0;
int intAnswer = a/b;

This implicitly casts the value to an int, and casting float to int is being done by truncating.

In this case this would be equivalent to

int intAnswer = 3.8;

Which is doing the same as,

int intAnswer = (int)3.8;

the (int) means that the value is casted (converted) to an int type.

Upvotes: 0

Stewart
Stewart

Reputation: 5062

printf("%.1f divided by %.1f equals %d\n", a, b, intAnswer);

a/b becomes 19.0/5.0 which is 3.8 and is reported that way.

Looking at the second case, we have:

floatanswer = x/y
floatanswer = 19/5
floatanswer = 3
printf( 3.0 )

You can see that the integers experience integer division before they are assigned to float answer. That means the printf of float-answer isn't your problem, it's the integer division which happens before floatanswer is ever assigned the value.

Upvotes: 0

Barmar
Barmar

Reputation: 782785

When you divide x/y you get the integer 3 because it performs integer division. When you assign that to floatAnswer, the integer is automatically converted to the equivalent float value, which is 3.0.

Upvotes: 1

ashishmishra
ashishmishra

Reputation: 419

2nd case: As I see, you have defined floatanswer as a float variable, that why It is returning decimal value.

3rd case: Because You have defined intAnswer as integer, that's why it is returning interfere value

Upvotes: -1

Related Questions