OneBite.Dev - Coding blog in a bite size

convert variable from float to int in C

Code snippet on how to convert variable from float to int in C

  int floatToInt(float num) { 
    int ival = (int)num; 
    return ival; 
  } 

This code snippet defines a function called floatToInt() which takes a single floating-point number as its argument and returns the nearest integer value. The code begins by declaring the function with a data type of int, meaning it will return an integer. Inside the function, the argument is cast as an integer and stored in the ival variable. Finally, the integer stored in ival is returned. In other words, this code snippet defines a function to convert a floating-point number to an integer by rounding the fractional part of the number.

c