默认情况下,char 是有符号的还是无符号的? [英] Is char signed or unsigned by default?

查看:34
本文介绍了默认情况下,char 是有符号的还是无符号的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Complete Reference of C"一书中提到char默认是无符号的.

In the book "Complete Reference of C" it is mentioned that char is by default unsigned.

但我正在尝试使用 GCC 和 Visual Studio 验证这一点.它默认将其视为签名.

But I am trying to verify this with GCC as well as Visual Studio. It is taking it as signed by default.

哪个是正确的?

推荐答案

书错了.该标准没有指定普通 char 是有符号还是无符号.

The book is wrong. The standard does not specify if plain char is signed or unsigned.

事实上,标准定义了三种不同的类型:charsigned charunsigned char.如果你#include 然后查看CHAR_MIN,你可以发现普通的char是否是signedunsigned(如果 CHAR_MIN 小于 0 或等于 0),但即便如此,三种类型是不同的就标准而言.

In fact, the standard defines three distinct types: char, signed char, and unsigned char. If you #include <limits.h> and then look at CHAR_MIN, you can find out if plain char is signed or unsigned (if CHAR_MIN is less than 0 or equal to 0), but even then, the three types are distinct as far as the standard is concerned.

请注意 char 在这种情况下是特殊的.如果将变量声明为 int,则 100% 等效于将其声明为 signed int.对于所有编译器和架构来说,这始终是正确的.

Do note that char is special in this way. If you declare a variable as int it is 100% equivalent to declaring it as signed int. This is always true for all compilers and architectures.

这篇关于默认情况下,char 是有符号的还是无符号的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆