aeverin c
aeverin c

Reputation: 7

What are some reasons you cast a char to an int in C++?

What are some reasons you would cast a char to an int in C++?

Upvotes: 0

Views: 688

Answers (2)

Keith Thompson
Keith Thompson

Reputation: 263237

It rarely makes sense to cast a char value to int.

First, a bit of terminology. A cast is an explicit conversion, specified via a cast operator, either the C-style (type)expr or one of the C++ casts such as static_cast<type>(expr). An implicit conversion is not a cast. You'll sometimes see the phrase "implicit cast", but there is no such thing in C++ (or C).

Most arithmetic operators promote their operands if they're of an integer type narrower than int or unsigned int. For example, in the expression '0' + 1, the char value of 0 is promoted to int before the addition, and the result is of type int.

If you want to assign a char value to an int object, just assign it. The value is implicitly converted, just as it would be if you used a cast.

In most cases, implicit conversions are preferred to casts, partly because they're less error-prone. An implicit conversion specified by the language rules usually does the right thing.

There are cases where you really do need to cast a char to int. Here's one:

char c = 'A';
std::cout << "c = '" << c << "' = " << static_cast<int>(c) << "\n";

The overloaded << operator accepts either char or int (among many other types), so there's no implicit conversion. Without the cast, the same char value would be used twice. The output (assuming an ASCII-based character set) is:

c = 'A' = 65

This is an unusual case because the << operator treats types char and int very differently. In most contexts, since char is already an integer type, it doesn't really matter whether you use a char or int value.

I can think of one other very obscure possibility. char values are almost always promoted to int. But for an implementation in which plain char is unsigned and char and int are the same width, plain char is promoted to unsigned int. This can only happen if CHAR_BIT >= 16 and sizeof (int) == 1. You're very unlikely to encounter such an implementation. Even on such a system, it usually won't matter whether char is promoted to int or to unsigned int, since either promotion will yield the correct numeric value.

Upvotes: 1

WeeniehuahuaXD
WeeniehuahuaXD

Reputation: 852

In general, this should rarely ever happen as it's fairly explicit when to use a char and when to use an int.

However if you were interested in performing arithmetic on a group of chars, you would require more memory to store the overall value, for this reason you could would usually use an int(or any other data type) to store the overall value.

By doing so you would more then likely implicitly cast the chars to the chosen data type.

However you can also explicitly cast these chars before or during calculation(Later versions of C++ take care of this for you though).

This is one such and more common use for the casting of chars.

However in practice, this can usually be avoided as it makers for stronger cleaner code in the long run

Upvotes: 0

Related Questions