有人可以解释char的签名如何具体平台? [英] Can someone explain how the signedness of char is platform specific?

查看:170
本文介绍了有人可以解释char的签名如何具体平台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近阅读了

之间的区别

  char 
unsigned char

  signed char 

是平台特定的。

我不能让我的头围绕这个?它是否意味着位序列可以从一个平台到下一个不同,即platform1的符号是第一位,platform2的标志可能在结束?

 

基本上我的问题来自看到这一行:

code> typedef unsigned char byte;

我不明白标志的相关性?

解决方案

让我们假设你的平台有8位字节,假设我们有位模式 10101010 。对于 signed char ,该值为− 86。对于 unsigned char ,虽然,相同的位模式表示170.我们没有移动任何位;它是相同的位,解释两种不同的方式。



现在 char 。该标准没有说明这两种解释中哪一种应该是正确的。保存位模式 10101010 char 可以是 170.它将是这两个值之一,但是你必须知道编译器和平台,才能预测它将是什么。一些编译器提供命令行开关来控制它将是哪一个。一些编译器根据它们运行的​​操作系统有不同的默认值,因此它们可以匹配操作系统约定。



在大多数代码中,它真的不重要。它们被视为三种不同类型,用于超载的目的。指向这些类型之一的指针与与另一类型的指针不兼容。尝试使用 signed char * 或unsigned char * strlen c>;



使用 signed char 当您需要一个单字节的有符号数字类型时, unsigned char ,当你想要一个单字节无符号数字类型。当您要保存字符时,使用纯旧 char 。这就是程序员在写你所要求的typedef时所想的。名称byte不具有保存字符数据的内涵,而名称unsigned char在其名称中具有单词char,并且使得一些人认为它是用于保持字符的好类型,或者它是一个好主意与类型 char 的变量比较。



因为你不可能做一般的算术在字符上,在您使用的任何平台和编译器上 char 是否已签名或未签名并不重要。


I recently read that the differences between

char
unsigned char

and

signed char

is platform specific.
I can't quite get my head round this? does it mean the the bit sequence can vary from one platform to the next ie platform1 the sign is the first bit, platform2 the sign could be at the end? how would you code against this?

Basically my question comes from seeing this line:

typedef unsigned char byte;

I dont understand the relevance of the signage?

解决方案

Let's assume that your platform has eight-bit bytes, and suppose we have the bit pattern 10101010. To a signed char, that value is −86. For unsigned char, though, that same bit pattern represents 170. We haven't moved any bits around; it's the same bits, interpreted two different ways.

Now for char. The standard doesn't say which of those two interpretations should be correct. A char holding the bit pattern 10101010 could be either −86 or 170. It's going to be one of those two values, but you have to know the compiler and the platform before you can predict which it will be. Some compilers offer a command-line switch to control which one it will be. Some compilers have different defaults depending on what OS they're running on, so they can match the OS convention.

In most code, it really shouldn't matter. They are treated as three distinct types, for the purposes of overloading. Pointers to one of those types aren't compatible with pointers to another type. Try calling strlen with a signed char* or an unsigned char*; it won't work.

Use signed char when you want a one-byte signed numeric type, and use unsigned char when you want a one-byte unsigned numeric type. Use plain old char when you want to hold characters. That's what the programmer was thinking when writing the typedef you're asking about. The name "byte" doesn't have the connotation of holding character data, whereas the name "unsigned char" has the word "char" in its name, and that causes some people to think it's a good type for holding characters, or that it's a good idea to compare it with variables of type char.

Since you're unlikely to do general arithmetic on characters, it won't matter whether char is signed or unsigned on any of the platforms and compilers you use.

这篇关于有人可以解释char的签名如何具体平台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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