简体   繁体   中英

Why can a compiler convert char to int in C++?

Through programming in java and now in C++, I have found that you can convert a char to an int and then a int to a double .

I want to know why is that a char can be converted to an int ?

In C++ they don't have the same amount of memory allocated to them, char is 8 bits and a int is 32 bits. So how does this work?

Is that just how the compiler is setup? I just want an explanation.

Thanks for any and all help!!

On some architectures, every value that a char can hold, an int can also hold. So if you have a char , you can use it to initialize an int by giving the int the same value that the char holds. This shouldn't be surprising.

On other architectures this isn't true. Still C++ allows any integer type to be converted to any other integer type. This must be true because it was allowed in C also, but you can prevent such "narrowing" conversions using brace-initialization.

What I think you're concerned about---which should be a concern on any architecture---is take a char and "pass it off" as an int . But that's not what converting char to int does. It is what converting to int& using reinterpret_cast would do; such a conversion is dangerous and potentially triggers undefined behaviour; not only because int and char don't have the same size, but also because they may not have the same representation even if they do have the same size.

Promotion between numerical types is legitimate by the C++ standard. Meaning, this feature is part of the C++ language.

Specifically for the promotions you are referring, a char can be promoted to an int and an int can be promoted to a double without loss of precision because the standard guarantees that:

sizeof(char) <= sizeof(int) <= sizeof(double)

That is the range of values represented by char is surely included in the range of values represented by int and the range of values represented by int surely is included in the range of values supported by double .

Frozen history. C++ was based on C, and still almost has C as a proper subset. And in the 1970s, when C was developed, there was no big difference between a byte and a character : almost all, if not absolutely all, the main text encodings used a single byte per character.

In modern C++ char and its two variants unsigned char and signed char is just the basic byte type, where a byte is defined as the smallest adressable unit of memory, and when used for characters char is just the basic encoding unit (eg with UTF-8 a character consists of one to five, I think it was, bytes).

Since and including with the standardization in 1998 C++ has acquired three more purposed character types: wchar_t , char16_t and char32_t , but unfortunately no strongly typed such type.


The compiler options or setup don't affect whether char converts implicitly to integer, but they affect whether plain char is a signed or unsigned type. Usually it's signed, also for historical reasons, which is impractical. As a signed type it's still distinct from signed char , eg with respect to overload resolution of a function call, and as an unsigned type it's distinct from unsigned char .


You can define a strongly typed byte size character encoding value type by using an enum :

using Byte = unsigned char;
enum class Byte_char : Byte  {};

“Strongly typed” means that it doesn't convert implicitly to number.

However, I prefer the more relaxed type checking of

enum Byte_char : Byte  {};

which converts to integer, but is a type distinct from uses of Byte for other purposes (this doesn't mean that I use a Byte_char type, it's just about what I find practical when defining such a type).

As far as size is concerned such a type can in practice also be defined as a struct , because as far as I know no extant C++ compiler inserts padding in a single byte struct . However, such a definition can be incompatible with the “short buffer optimization” of std::basic_string . The enum works fine with that optimization.

char is not converted to int . The ASCII code of char is assigned for int value. If you convert the char '5' to int , you have to get integer value 5. But you will get 53 which is the code of '5'. The byte holding ASCII code is directly extended to 32 bit value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM