Sai A Sai A
Updated date Oct 16, 2023
In this blog, we will learn how to convert double to int in C. Explore multiple methods, including the type casting operator, conversion functions, macros, and considerations for handling overflow/underflow.
  • 2.2k
  • 0
  • 0

Introduction:

Data types play a crucial role in defining the nature of variables and the operations that can be performed on them. One common scenario is the need to convert a double (floating-point) value to an int (integer) value in the C programming language. This process, known as type casting, involves changing the data type of a variable to another. In this blog, we will explore various methods to cast a double to an int in C.

Method 1: Using Type Casting Operator

The easiest way to convert a double to an int is by using the type casting operator. The syntax is simple:

#include <stdio.h>

int main() {
    double doubleValue = 5.67;
    int intValue = (int)doubleValue;

    printf("Method 1 Output: %d\n", intValue);

    return 0;
}

Output:

Method 1 Output: 5
  • The (int) inside the parentheses is the type casting operator. It explicitly tells the compiler to treat doubleValue as an integer.
  • The fractional part of the doubleValue (0.67 in this case) is truncated, and the integer part (5 in this case) is assigned to intValue.

Method 2: Using Type Conversion Functions

C provides functions like floor(), ceil(), and round() from the math.h library, which can be used to perform specific types of rounding before casting to an integer. Let's explore the floor() function:

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

int main() {
    double doubleValue = 5.67;
    int intValue = (int)floor(doubleValue);

    printf("Method 2 Output: %d\n", intValue);

    return 0;
}

Output:

Method 2 Output: 5
  • The floor() function rounds down the doubleValue to the nearest integer that is less than or equal to the original value.
  • The type casting operator is then used to convert the result to an integer.

Method 3: Using Type Conversion Macro

Another way to perform the conversion is by using a macro. This allows you to encapsulate the casting logic in a reusable form:

#include <stdio.h>

#define DOUBLE_TO_INT(x) ((int)(x))

int main() {
    double doubleValue = 5.67;
    int intValue = DOUBLE_TO_INT(doubleValue);

    printf("Method 3 Output: %d\n", intValue);

    return 0;
}

Output:

Method 3 Output: 5
  • The DOUBLE_TO_INT macro is defined to cast a given value to an integer using the type casting operator.
  • It provides a concise and reusable way to convert double to int.

Method 4: Handling Overflow and Underflow

When dealing with large double values that cannot be represented accurately as integers, it's essential to handle overflow and underflow. The limits.h header file provides constants for minimum and maximum integer values (INT_MIN and INT_MAX). Here's an example:

#include <stdio.h>
#include <limits.h>

int main() {
    double doubleValue = 1.0e20;

    // Checking for overflow before casting
    if (doubleValue > INT_MAX || doubleValue < INT_MIN) {
        printf("Method 4 Output: Overflow or Underflow\n");
    } else {
        int intValue = (int)doubleValue;
        printf("Method 4 Output: %d\n", intValue);
    }

    return 0;
}

Output:

Method 4 Output: Overflow or Underflow
  • The program checks whether the doubleValue is within the representable range of an int before casting.
  • If the value is outside this range, it prints a message indicating overflow or underflow.

Conclusion:

In this blog, we have learned multiple methods for casting a double to an int in the C programming language. Each method has its advantages and considerations based on the specific requirements of the task at hand. Whether you prefer the simplicity of the type casting operator, the precision of mathematical functions, the encapsulation of macros, or the handling of overflow and underflow, the choice depends on the context of your program.

Comments (0)

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