2

For the life of me I cannot get this float to cast to an int, and I don't know why – I've tried a number of different things, including printing it as an int, which seems to have worked (printf("%d" , (int) change);) but I don't need the number to print.

The error message is telling me quite explicitly that I can't print a %d as the input (change) is a %f. But I can't get it to cast – and I can't seem to find any resources that tell you how to do this, other than how I have it written below.

Here's the code in my main function:

//receiving user input, outputting number of cents. 
float change; 

do {
printf("How much change is owed?\n"); 
change = GetFloat(); 
}
while (change <= 0); 

//switching to cents
change = change * 100; 
change = roundf(change); 
(int) change;  

printf("%d", change); 

1 Answer 1

6

Your code isn't working for the same reason that this code:

int x = 5;
x + 5;
printf("%i", x);

does not print the number 10.

In C, (int) is a type casting operator; much like the + operator, it does not modify a variable; it creates an expression, which is evaluated and replaced by its result.

So the above code is equivalent to:

int x = 5;
10;
printf("%i", x);

In the same way, when you write (int) change; you've only written an expression, which will be evaluated in place using the value of change, without modifying change itself. You have to explicitly do something with that value, or it just gets thrown away. In this case, "do something" probably means assigning the result of the expression to a new variable, since change can only take float type values.

change = change * 100;
change = roundf(change);
int cents = (int) change;

In fact, when you assign a float value to a variable of type int, the value will be silently truncated so that it can be stored as an integer, whether you include the explicit cast operator (int) or not. When you do not explicitly cast the value, this is commonly referred to as type coercion:

change = change * 100;
change = roundf(change);
int cents = change;

If you are certain the value is (or should be) a whole number, even when stored as a float, you shouldn't have to worry about the truncation. But if you expect some decimal value, beware that truncation is equivalent to always rounding down, never up; a number like 5.9999 gets truncated to 5 if you coerce it to an integer.

You can also do the above three lines all in one step. Not only is this shorter code, it never modifies the original value of change, which might be useful if you want to use it again somewhere else:

int cents = roundf(change * 100);

In this case I think it's more than sufficiently clear what's going on without explicit type casting. However, if you had declared int cents; somewhere earlier in the code, it might make sense to add the (int) operator in front of the expression to remind whoever's reading the code that the value being stored doesn't have the same type as change.

5
  • Thank you so much! This is an awesome comment- thorough and very helpful!! Commented Nov 22, 2014 at 8:05
  • i was able to follow everything in your answer, except (int) at the end. can you explain the difference between int cents = roundf(change * 100); and (int) cents = roundf(change * 100); i tried both in my program, which essentially outputs the same? thanks in advance.
    – mazal
    Commented Jan 28, 2015 at 3:27
  • @mazal When I say to add (int) "in front of the expression" I mean after the assignment operator. And the difference is explicit versus implicit; the value is truncated either way, but using (int) is a sign that the behavior is intentional, not accidental.
    – Air
    Commented Jan 28, 2015 at 20:04
  • 1
    @AirThomas i see.. so you mean, int cents = roundf(change * 100); can (or should) be expressed as: int cents = (int) roundf(change * 100); ?
    – mazal
    Commented Jan 29, 2015 at 6:43
  • @mazal Yes. In that example, the variable is declared as int on the same line already, so I would say the intent is clear enough without the explicit cast operator, (int).
    – Air
    Commented Jan 29, 2015 at 17:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .