Type Casting

Casting in C is a method used to convert a variable from one data type to another. There are two types of casting: implicit casting (automatic conversion) and explicit casting (manual conversion).

Implicit Casting

When it is necessary and safe to do so, the C compiler automatically performs implicit casting. This usually happens when a variable of a smaller data type (like int) is assigned to a variable of a larger data type (like long). In such cases, the compiler automatically extends the smaller type to the larger type.

Example:

int i = 100;
long l = i;  // Implicit casting from int to long

Bit-Level Explanation:

  • The int variable i is typically 4 bytes (32 bits) on most systems.

  • When i is assigned to long l, which is typically 8 bytes (64 bits), the compiler automatically extends i to 64 bits by adding zeros to its binary representation.

Explicit Casting

Explicit casting is when the programmer manually converts data types, and it is necessary when converting a larger data type to a smaller one or when converting between types where automatic conversion is not defined.

Example:

double d = 9.5;
int i = (int)d;  // Explicit casting from double to int

Bit-Level Explanation:

  • A double is typically 8 bytes (64 bits), holding both the integer and fractional parts.

  • Casting to int (4 bytes, 32 bits) involves truncating the fractional part and potentially some of the integer part if the number is too large for an int.

Sample Code with Implicit and Explicit Casting:

#include <stdio.h>

int main() {
    // Implicit casting
    int i = 150;
    long l = i;
    printf("Implicit cast (int to long): %ld\n", l);

    // Explicit casting
    double d = 123.456;
    int j = (int)d;
    printf("Explicit cast (double to int): %d\n", j);

    return 0;
}

Expected Output:

Implicit cast (int to long): 150
Explicit cast (double to int): 123

What Happens at the Bit Level

  1. Implicit Casting (int to long):

    • i (32 bits) is extended to 64 bits. If i is positive, zeros are added to the left. If i is negative, the sign bit (leftmost bit) is extended.

    • This process preserves the numerical value while changing the data type.

  2. Explicit Casting (double to int):

    • The double value is truncated to fit into an int. This means the fractional part is discarded, and if the integer part is too large for an int, it gets truncated.

    • This process can lead to loss of information and potential overflow issues.

Understanding casting is essential for efficient and error-free programming in C, especially when dealing with different data types and hardware-level operations.

0
Subscribe to my newsletter

Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jyotiprakash Mishra
Jyotiprakash Mishra

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.