Sai A Sai A
Updated date Oct 23, 2023
In this blog, we will explore how to convert a float to an int in C. Dive into methods such as direct type casting, using the floor() function, and leveraging the ceil() function.

Introduction:

Data types play an important role in defining the nature of variables and how they interact with each other. C, being a low-level language, demands explicit handling of data types, especially during variable conversions. One common conversion scenario is casting a floating-point number to an integer. This blog explores how to convert a float to an int in C.

Method 1: Direct Type Casting

The simple approach to convert a float to an int is through direct type casting. In C, this involves placing the target type in parentheses before the variable to be converted. Here's a simple program to illustrate this method:

#include <stdio.h>

int main() {
    float floatNumber = 23.45;
    int intNumber;

    // Method 1: Direct Type Casting
    intNumber = (int)floatNumber;

    printf("Method 1: Direct Type Casting\n");
    printf("Float Number: %f\n", floatNumber);
    printf("Int Number: %d\n", intNumber);

    return 0;
}

Output:

Method 1: Direct Type Casting
Float Number: 23.450000
Int Number: 23

In this example, the float number 23.45 is cast directly to an integer. The fractional part is truncated, and the resulting integer is 23.

Method 2: Using the floor() Function

An alternative method involves using the floor() function from the math.h library. The floor() function rounds down the given floating-point number to the nearest integer. Here's how it can be applied:

#include <stdio.h>
#include <math.h>

int main() {
    float floatNumber = 23.45;
    int intNumber;

    // Method 2: Using the floor() Function
    intNumber = floor(floatNumber);

    printf("Method 2: Using the floor() Function\n");
    printf("Float Number: %f\n", floatNumber);
    printf("Int Number: %d\n", intNumber);

    return 0;
}

Output:

Method 2: Using the floor() Function
Float Number: 23.450000
Int Number: 23

In this example, the floor() function is applied to 23.45, resulting in the same output as direct type casting.

Method 3: Using the ceil() Function

Similar to the floor() function, the ceil() function from math.h rounds up the given floating-point number to the nearest integer. Here's a program demonstrating this method:

#include <stdio.h>
#include <math.h>

int main() {
    float floatNumber = 23.45;
    int intNumber;

    // Method 3: Using the ceil() Function
    intNumber = ceil(floatNumber);

    printf("Method 3: Using the ceil() Function\n");
    printf("Float Number: %f\n", floatNumber);
    printf("Int Number: %d\n", intNumber);

    return 0;
}

Output:

Method 3: Using the ceil() Function
Float Number: 23.450000
Int Number: 24

Here, the ceil() function is applied to 23.45, resulting in rounding up to the nearest integer, which is 24.

Conclusion:

In this blog, we have discussed various methods for casting a float to an int in C. Direct type casting, using the floor() function, and employing the ceil() function all offer distinct approaches to this conversion.

Comments (0)

There are no comments. Be the first to comment!!!