简体   繁体   English

C中有符号和无符号字符之间的区别

[英]Difference between in signed and unsigned char in c

What is the difference between signed and unsigned char in C ? C中有符号和无符号char什么区别? When I tried to run the following code without unsigned it entered an infinite loop as the range of signed char is up to 127 (Why is it signed by default ?). 当我尝试运行以下代码且未unsigned它进入了无限循环,因为已签名char的范围最大为127 (为什么默认情况下会对其进行签名?)。 But when I added unsigned (whose range is up to 255) it works fine. 但是,当我添加unsigned (其范围最大为255)时,它可以正常工作。 What's the reason ? 什么原因 ?

#include <stdio.h>

int main() {
    unsigned char x;
    x = 0;
    while (x <= 225) {
         printf("%c=%d\n", x, x);
         x = x + 1;
    }
    return 0;
}

There's no dedicated "character type" in C language. C语言中没有专用的“字符类型”。 char is an integer type, same (in that regard) as int, short and other integer types. char是一个整数类型,与int,short和其他整数类型相同(就此而言)。

  • They are both integer types. 它们都是整数类型。
  • They have same size(one btye). 它们具有相同的大小(一个btye)。

If you are using numbers as char 如果您将数字用作字符

  • Signed char, which gives you at least the -127 to 127 range. 带符号的字符,至少为-127至127。 (-128 to 127 is common) (-128至127是常见的)
  • Unsigned char, which gives you at least the 0 to 255 range. 无符号字符,至少为您提供0到255的范围。

From 3.9.1 Fundamental types 从3.9.1基本类型开始

Plain char, signed char, and unsigned char are three distinct types. 普通字符,有符号字符和无符号字符是三种不同的类型。 A char, a signed char, and an unsigned char occupy the same amount of storage and have the same alignment requirements (3.11); 一个char,一个已签名char和一个未签名char占用相同的存储量,并具有相同的对齐要求(3.11); that is, they have the same object representation. 也就是说,它们具有相同的对象表示形式。

With the statement 与声明

x=x+1;

there are a few things happening. 有一些事情正在发生。

First the expression x + 1 will be evaluated. 首先将对表达式x + 1求值。 That will lead to usual arithmetic conversion of the value in x so it becomes promoted to an int . 这将导致x值的常规算术转换 ,因此它升为int The (promoted) value will be increased by 1 , and then the result will be assigned back to x . (提升的)值将增加1 ,然后将结果分配回x And here the problems start. 问题就从这里开始。

Because the result of x + 1 is an int it needs to be converted back to a ( signed ) char . 因为x + 1的结果是一个int所以需要将其转换回一个(带signedchar This is done through integer conversion and when converting from a larger signed type (like int ) to a smaller signed type (like signed char ) when the value can't fit in the smaller type (like trying to store 128 in a signed char ) the behavior is implementation defined . 这是通过整数转换来完成的,当值不能适合较小的类型(例如尝试将128存储在一个有signed char )时,则从较大的有符号的类型(如int )转换为较小的有符号的类型(如signed char )来完成。行为是实现定义的

What happens in practice is that the value becomes negative (the value is increased from 0x7f to 0x80 which is -128 in two's complement (the most common encoding for negative numbers)). 在实践中发生的是该值变为负数 (该值从0x7f增大到0x80 ,即2的补码-128 (最常见的负数编码))。 This further leads to x being less than 225 forever and your infinite loop. 这进一步导致x 永远小于225从而导致无限循环。

Signed char range is -128 to 127 . 带符号的char范围是-128到127。 Unsigned char range is 0 to 255. Your while loop will work as expected if the x variable defined as unsigned char. 无符号字符范围是0到255。如果x变量定义为无符号字符,则while循环将按预期工作。

If you define the variable with signed char, then the variable 'x' laid between -128 to 127. It is always less than 255. 如果使用带符号的char定义变量,则变量'x'介于-128到127之间。它始终小于255。

  • char has as size one byte which mean 256 possible values to code. char具有一个字节的大小,这意味着要编码的256个可能值。
  • signed means the value can be negatif or positif,in C standard a variable declared as signed chard ranges from -127 to 127 signed表示该值可以是negatif或positif,在C标准中,声明为signed chard的变量的范围是-127127
  • unsigned means only positive value and this lead to the fact that variable declared as unsigned char range from 0 to 255 unsigned表示仅是正值,这导致声明为unsigned char变量的范围为0255

To understand this better let's examine this example unsigned char x =-1 , the question is what is the vale of x? 为了更好地理解这一点,让我们研究这个示例unsigned char x =-1 ,问题是x的值是多少? the answer is x is not -1 in the machine because th compiler will interept x as positive value of type char and so x will be 255 , if we have unsigned char x = -2 then x egale to 254 and so on. 答案是x在机器中不是-1,因为编译器会将x截断为char类型的正值,因此x将为255 ,如果我们有unsigned char x = -2则x egale为254,依此类推。 This example so the importance of understanding how the compiler interprets things in background and what result we get during code execution. 这个例子对理解编译器如何在后台解释事物以及我们在代码执行过程中得到什么结果的重要性。

The unsigned char simply means: Use the most significant bit instead of treating it as a bit flag for +/- sign when performing arithmetic operations. 无符号char的简单含义是:在执行算术运算时,使用最高有效位代替将其视为+/-符号的位标志。

It makes significance if you use char as a number for instance: 例如,如果将char用作数字,则意义重大:

typedef char BYTE1; typedef char BYTE1; typedef unsigned char BYTE2; typedef unsigned char BYTE2;

BYTE1 a; BYTE1 a; BYTE2 b; BYTE2 b; For variable a, only 7 bits are available and its range is (-127 to 127) = (+/-)2^7 -1. 对于变量a,仅7位可用,其范围为(-127至127)=(+/-)2 ^ 7 -1。 For variable b all 8 bits are available and the range is 0 to 255 (2^8 -1). 对于变量b,所有8位均可用,范围为0到255(2 ^ 8 -1)。

If you use char as character, "unsigned" is completely ignored by the compiler just as comments are removed from your program. 如果将char用作字符,则从程序中删除注释时,编译器将完全忽略“ unsigned”。

Char is a data type which is used in C programming for storing characters like letters and punctuation marks. Char是一种数据类型,在C编程中用于存储字母和标点符号之类的字符。 However, it still remains to be an integer type. 但是,它仍然是整数类型。 This is due to the reason that char type technically stores integers and not characters. 这是由于char类型在技术上存储整数而不是字符的原因。 It makes use of a numerical code which represents characters by using integers The transformation of char into int values is done automatically by C. However, it is still dependent on the machine which decides that the result would be negative or not. 它使用通过整数来表示字符的数字代码。将char转换为int值是由C自动完成的。但是,它仍然取决于确定结果是否为负的机器。 The upper case A is equivalent to integer value of 65. 大写字母A等于65的整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 什么是签名的char指针和未签名的char指针之间的工作差异? - Whats the working difference between a signed char pointer and an unsigned one? C中的unsigned int和signed int有什么区别? - What is a difference between unsigned int and signed int in C? 在 C 中将 unsigned char 和 signed char 类型转换为 int - typecasting unsigned char and signed char to int in C 在数组中对signed和unsigned char进行索引的区别是什么? - whats the difference for indexing of signed and unsigned char in array? C 字符串 - 使用字符串时,指向有符号字符和无符号字符的指针是否有所不同? - C strings - Do pointers to signed char vs unsigned char make a difference when working with strings? *(unsigned char *)s1和(unsigned char)* s1之间的C差异 - C difference between *(unsigned char *)s1 and (unsigned char)*s1 char和unsigned char有什么区别? - What is the difference between char and unsigned char? 在这种情况下 char 和 unsigned char 有什么区别 - What is the difference between char and unsigned char in this situation C/C++ 字符、无符号字符和有符号字符的底层表示 - C/C++ underlying representation of char, unsigned char and signed char 关于编码签名/未签名的字符C问题 - Char C question about encoding signed/unsigned
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM