Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is a type cast in C/C++?

Tags:

c++

c

types

casting

What exactly is a type cast in C/C++? How does the compiler check if an explicit typecast is needed (and valid)? Does it compare the space required for an value? If I have for example:

int a;
double b = 15.0;
a = (int) b;

If I remember correctly a double value requires more space (was it 8 bytes?!) than an integer (4 bytes). And the internal representation of both are completely different (complement on two/mantissa). So what happens internally? The example here is quite straightforward, but in C/C++ there are plentiful typecasts.

How does the compiler know (or the programmer) if I can cast e.g. FOO to BAR?

like image 465
pluckyDuck Avatar asked Sep 26 '11 17:09

pluckyDuck


People also ask

What is type casting explain?

Type casting is a way of converting data from one data type to another data type. This process of data conversion is also known as type conversion or type coercion. In Java, we can cast both reference and primitive data types. By using casting, data can not be changed but only the data type is changed.

What is the use of type cast?

Typecast is a way of changing an object from one data type to the next. It is used in computer programming to ensure a function handles the variables correctly. A typecast example is the transformation of an integer into a string.

What is type cast operator?

The type cast operator converts the data type of expr to the type specified by type-name : [ type-name ] expr. Explicit type conversions require the type cast operator. Implicit type conversions are performed automatically by 4Test and do not require explicit type casting.


4 Answers

A type cast is basically a conversion from one type to another. It can be implicit (i.e., done automatically by the compiler, perhaps losing info in the process) or explicit (i.e., specified by the developer in the code). The space occupied by the types is of secondary importance. More important is the applicability (and sometimes convenice) of conversion.

It is possible for implicit conversions to lose information, signs can be lost / gained, and overflow / underflow can occur. The compiler will not protect you from these events, except maybe through a warning that is generated at compile time. Slicing can also occur when a derived type is implicitly converted to a base type (by value).

For conversions that can be downright dangerous (e.g., from a base to a derived type), the C++ standard requires an explicit cast. Not only that, but it offers more restrictive explicit casts, such as static_cast, dynamic_cast, reinterpret_cast, and const_cast, each of which further restricts the explicit cast to only a subset of possible conversions, reducing the potential for casting errors.

Valid conversions, both implicit and explict are ultimately defined by the C/C++ standards, although in C++, the developer has the ability to extend conversions for user defined types, both implicit and explicit, via the use of constructors and overloaded (cast) operators.

The complete rules for which casts are allowed by the standards and which are not can get quite intricate. I have tried to faithfully present a somewhat concise summary of some of those rules in this answer. If you are truly interested in what is and is not allowed, I strongly urge you to visit the standards and read the respective sections on type conversion.

like image 85
Michael Goldshteyn Avatar answered Oct 06 '22 02:10

Michael Goldshteyn


Just want to mention something frequently overlooked:

  • A cast always creates a temporary of the target type (although if the target type is a reference, you won't notice).

This can be important. For example:

#include <iostream>

void change_one_print_other( int& a, const int& b )
{
    a = 0;
    std::cout << b << "\n";
}

int main(void)
{
    int x = 5, y = 5;

    change_one_print_other(x, x);
    change_one_print_other(y, static_cast<int>(y));
}

That cast LOOKS useless. But looks can be deceiving.

like image 43
Ben Voigt Avatar answered Oct 06 '22 04:10

Ben Voigt


There are certain type casts that the compiler knows how to do implicitly - double to int is one of them. It simply drops the decimal part. The internal representation is converted as part of the process so the assignment works correctly.

Note that there are values too large to be converted properly. I don't remember what the rules are for that case; it may be left at the discretion of the compiler.

like image 26
Mark Ransom Avatar answered Oct 06 '22 02:10

Mark Ransom


Make a little C program of your code and follow the instructions in How to get GCC to generate assembly code to see how the compiler does a type cast.

like image 25
ott-- Avatar answered Oct 06 '22 04:10

ott--