2

I am currently learning C and typecasts and it is suggesting that if I want to convert an int to a float I should precede the variable with (float) to do this.

However, when I don't use the typecast, the answer still comes out correct, even though I am mixing the ints and floats.

Can someone explain why/when I would need to use the typecast (float) in particular?

#include <stdio.h>

int main(void) {
    int A, B, E;
    float C, D;

    A = 2;
    B = 1;
    C = 12.5;

    D = C * A + B;
    E = C * A + B;

    printf("Total is %.1f\n", D);
    printf("total is %d", E);
    return 0;
}
Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
Steve
  • 463
  • 1
  • 8
  • 16
  • Your code is fine. Your learning materials are not very good. Casts should be avoided unless there is no other option. – M.M Oct 05 '15 at 00:15
  • M.M, I am using the 'Absolute beginners guide to C Programming', is there a better book than that? Thanks – Steve Oct 05 '15 at 00:17
  • [See this thread](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) for some recommendations – M.M Oct 05 '15 at 00:17
  • Thank you, I see you are an expert in C , any one in particular you would recommend instead ? Appreciate it – Steve Oct 05 '15 at 00:19
  • 1
    I don't have a specific recommendation , anything from that thread which has been upvoted should be good – M.M Oct 05 '15 at 00:20
  • 1
    C doesn't use the term "typecast". `(float)` is an example of a cast operator. It specifies an explicit type conversion. Type conversions may also be done implicitly in many contexts. In fact, *most* conversions should be implicit, and any use of a cast operator should be viewed with suspicion. – Keith Thompson Oct 05 '15 at 01:31

2 Answers2

2

You never need to explicitly typecast to float unless you want to force a precise operation between to ints, for example

int a = 10;
int b = 3;

a / b yields 3 of type int (because it's an integer division), while a / (float)b yields 3.3333.. of type float.

C language specification in addition ensures you that an integer type can be implicitly converted to a float whenever it's necessary to do so (eg. an operation with another float). In addition narrowing can occur if you assign a float value to an int variable, in that case value is truncated. (§6.3.1.4 of C11)

Jack
  • 131,802
  • 30
  • 241
  • 343
1

By convention, C will upcast an arithmetic expression into a float if the expression has even one float variable. So the following two expressions would end up being float:

int A = 3;
float F = 1.0;
float result1 = A * F;  // result is 3.0
float result2 = A / F;  // result is also 3.0
float result3 = A - F;  // result is 2.0

In your actual code, you have the following expression:

D = C * A + B;

In terms of order of operations, we can rewrite the right hand side like this:

((C * A) + B);

C will evaluate this as follows:

((float + int) + int)
((float) + int)
float
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 4
    This is not very well worded, for example `float = int / int + float` still does integer division – M.M Oct 05 '15 at 00:16