0

I found a workaround for the following question, but I'd still like to understand why typecasting isn't working the way I understood it to work.

Here I'm simply trying to take the user's input, convert it to cents, and then typecast the float to an integer.

int main(void)
{
    float change;
    int numCoins = 0;
    do
    {
        /*Two lines here use printf and GetFloat() to ask for a user input*/
    }
    while (change <= 0);  
    change = round(100 * change);
    (int)change;
    printf("%i\n", change);}

When I compile the code, I get a couple of errors, the first saying: "format specifies type 'int' but the argument has type 'float'"

Clearly the line

(int)change;

isn't typecasting the float to an int.

I also tried:

(int)change = round(100 * change);

and:

change = (int)round(100 * change);

with no success.

My solution involved creating a new int variable and assigning the float change to that. But that's not the same as typecasting a variable from one type to another type...so what have I done wrong?

Thanks, Eric

1

Clearly the line

(int)change;

isn't typecasting the float to an int.

it is actually. the expression (int) change; evaluates to an integer version of the value stored in change. but what do you do with this integer value next? absolutely nothing. for example, you're never storing it.

another point is that you can not change the type of a variable in C. that is, if change was declared to be of type float, it's gonna be of that type until it dies.

so even if you store the casted value back in change, in this situation, it'll be promoted back up to float because ultimately change is still of type float.

I also tried:

(int)change = round(100 * change);

to explain why this doesn't work either, you'll probably need to know something about L-values and R-values. in short, when you do something like

x = y;

where x is a variable and y is an expression (e.g., a value), you're essentially instructing your computer to

  1. evaluate x such that determining which location in memory it maps to.
  2. evaluate y to a value.
  3. and store that value in the location mapped to by x.

as stated above (int) change is just evaluated to an integer value. that is, unlike x, it's not a variable. it doesn't map to a memory location. change does though.

and:

change = (int)round(100 * change);

answered above.

2

It looks like you don't have a clear understanding of how typecasting works. It is a temporary change, not a permanent one, and is only active for the current line of code. You had the following code:

(int)change;
printf("%i\n", change);

and you expected change to be treated as an int in the printf() statement. It doesn't work that way. change was converted to an int only for the one line of code, which didn't do anything, assuming it even compiled. However, if you had done this, it would have worked:

printf("%i\n", (int) change);

In this case, change gets cast as an int for the printf() statement. Any subsequent lines will treat it as a float, which is its actual type.

(int)change = round(100 * change); throws an error, so it failed.

change = (int)round(100 * change); will do the round operation and then truncate it because of the (int) casting, and assign the result to change, but it is stored as a float since that's the var type of change.

So, the key to all this is to understand that casting has only a temporary effect. Also you should think of it as something that is applied to a source, not to a target.

If this answers your question, please click the check mark to accept this and remove the question from the unanswered pool. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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