2

I understand what this code produce but I don't know what kind of syntax (or function) it is.

code

void main()
{
    float af;
    int a = 75;
    int cov = 100;

    af = a/cov;
    printf("af: %f\n", af);
    af = (float) a/(float) cov;
    printf("af: %f\n", af);
}

output

af: 0.000000
af: 0.750000

Without the (float) in front of the two int variables, it would have done the division between integers (thus returning 0.0) and then converted it to float, while with the (float) it seems to temporarly convert a and cov to float and than perform the division.

What is this (type) form?

Dario R
  • 77
  • 7
  • 1
    It is exactly the way you described. How else does this work? ;-) – L. F. Mar 19 '19 at 12:48
  • first convert to float and than divide -> 0.75 – Mike Mar 19 '19 at 12:49
  • 1
    You seem to have answered your own question... a cast is there to enforce an explicit type conversion. So what's the question here? – Lundin Mar 19 '19 at 12:50
  • When at least one of the operands of the division operator is a `float`, the result will be a `float` – Spikatrix Mar 19 '19 at 12:50
  • That is a typecast. More info here: https://www.tutorialspoint.com/cprogramming/c_type_casting.htm – Rudy Velthuis Mar 19 '19 at 12:51
  • The term you need is 'type casting', in this case, explicit type casting. There's lots of info on SO and other places about how it works. – visibleman Mar 19 '19 at 12:52
  • It's a cast. Personally I prefer `1.0f * a / cov`. – Bathsheba Mar 19 '19 at 12:56
  • It's worth noting that casting is a unary prefix operator, and therefore has very high precedence (i.e., it is applied before almost all other operator). P.S. IMO it also makes it weird to leave a gap between the cast and the variable, to me it would be like seeing `++ a`. – James Snook Mar 19 '19 at 13:04
  • 1
    @JamesSnook "before any other operator" -> "before most other operators". E.g. member access `.` and `->` are higher. – Bathsheba Mar 19 '19 at 13:07
  • Possible duplicate of [What exactly is a type cast in C/C++?](https://stackoverflow.com/questions/7558837/what-exactly-is-a-type-cast-in-c-c) – Ctx Mar 19 '19 at 13:09

4 Answers4

4

It's called a typecast, or cast for short. The syntax is:

(type) expression

It does exactly what you already inferred: it evaluates to the value of expression, converted to the given type.

Thomas
  • 174,939
  • 50
  • 355
  • 478
0

Type casting is a way to convert a variable from one data type to another data type.

(type_name) expression

Another way to do this is

float af;
int a = 75;
float cov = 100.0;

af = a/cov;
printf("af: %f\n", af);

The result would be a float.

liakoyras
  • 1,101
  • 12
  • 27
-1

This is typecasting. Putting a type in parentheses before an expressions converts the expression to that type. For example,

(int) 1.0; // gives 1
davidlowryduda
  • 2,404
  • 1
  • 25
  • 29
-1
af = a/cov;

Integer division 75 / 100 = 0
this means in float -> 0.0

af = (float) a/(float) cov;

typcast 75 to float -> 75.0
typcast 100 to flat -> 100.0
float division 75.0 / 100.0 -> 0.75

Mike
  • 4,041
  • 6
  • 20
  • 37