Home > Backend Development > C++ > body text

Why are `char`, `signed char`, and `unsigned char` Treated Differently from `int`, `signed int`, and `unsigned int` in C ?

Barbara Streisand
Release: 2024-10-26 22:37:03
Original
274 people have browsed it

Why are `char`, `signed char`, and `unsigned char` Treated Differently from `int`, `signed int`, and `unsigned int` in C  ?

Char!=(Signed Char), Char!=(Unsigned Char)

In C Programming, char, signed char, and unsigned char are considered distinct data types. This behavior is different from integers, where int and signed int are the same type, and unsigned int is another separate type.

Question:

Why is it that the code below compiles differently for char than for int types?

cout << getIsTrue< isX<int8>::ikIsX  >() << endl;
cout << getIsTrue< isX<uint8>::ikIsX  >() << endl;
cout << getIsTrue< isX<char>::ikIsX >() << endl;
Copy after login

Answer:

C distinguishes between char, signed char, and unsigned char as three separate types. This is why the template instantiations for char, int8, and uint8 are distinct.

For integers, however, int and signed int are the same type, while unsigned int is a different type. This is why the template instantiations for int, int32, and uint32 result in only two unique templates.

The standard [basic.fundamental](https://eel.is/c draft/basic.fundamental) states:

"Plain char, signed char, and unsigned char are three distinct types."

This means that they have different object representations and can have different value ranges, depending on the implementation.

The above is the detailed content of Why are `char`, `signed char`, and `unsigned char` Treated Differently from `int`, `signed int`, and `unsigned int` in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!