简体   繁体   English

将字符类型转换为整数类型是否安全?

[英]Is it safe to cast a character type to an integer type

int main() {
    char ch = 'a';
    int x;
    x = ch; 
    printf("x=%c", x);
}

Is this code safe to use (considering endiness of machine)? 此代码是否安全使用(考虑机器的使用寿命)?

Yes, it is safe to cast a character (like char ) type to an integer type (like int ). 是的,将char类型(如char )转换为整数类型(如int )是安全的。

In this answer and others, endian-ness is not a factor. 在这个答案和其他答案中,字节序不是一个因素。

There are 4 conversions going on here and no casting: 这里进行了4次转换,并且没有强制转换:

  1. a is character of the C encoding. a是C编码的字符。 'a' converts to an int at compile time. 'a'在编译时转换为int

     'a' 
  2. The int is converted to a char . int转换为char

     char ch = 'a'; 
  3. The char ch is converted to an int x . char ch将转换为int x In theory there could be a loss of data going from char to int **, but given the overwhelming implementations, there is none. 理论上讲 ,从charint **的数据可能会丢失,但是鉴于压倒性的实现,没有任何实现。 Typical examples: If char is signed in the range -128 to 127, this maps well into int . 典型示例:如果char在-128到127范围内签名,则可以很好地映射到int If char is unsigned in the range 0 to 255, this also maps well into int . 如果char在0到255范围内是无符号的,则这也很好地映射到int

     int x; x = ch; 
  4. printf("%c", x) uses the int x value passed to it, converts it to unsigned char and then prints that character. printf("%c", x)使用传递给它的int x值,将其转换为unsigned char ,然后打印该字符。 (C11dr §7.21.6.1 8 @haccks) Note there is no conversion of x due to the usual conversion of variadic parameters as x is all ready an int . (C11dr§7.21.6.18 @haccks)请注意,由于可变参数的通常转换,因此没有x的转换,因为x都已准备好成为int

     printf("x=%c", x); 

** char and int could be the same size and char is unsigned with a positive range more than int . ** charint的大小可以相同,并且char的无符号数比int This is the one potential problem with casting char to int although typically there is not loss of data. 尽管通常不会丢失数据,但这是将charint的一个潜在问题。 This could be further complicated should char have range like 0 to 2³²-1 and int with a range of -(2³¹-1) to +(2³¹-1). 如果char范围在0到2³²-1之间,并且int的范围是-(2³¹-1)到+(2³¹-1),则这可能会更加复杂。 I know of no such machine. 我知道没有这样的机器。

Yes, casting integer types to bigger integer types is always safe. 是的,将整数类型转换为更大的整数类型始终是安全的。

Standard library's *getc (fgetc, getchar, ...) functions do just that--they read unsigned chars internally and cast them to int because int provides additional room for encoding EOF (end of file, usually EOF==-1). 标准库的*getc (fgetc,getchar,...)函数就是这样做的-它们在内部读取unsigned chars并将其unsigned chars转换为int因为int为编码EOF(文件末尾,通常为EOF ==-1)提供了额外的空间。

是的,因为int大于char ,但出于相同的原因,使用char代替int并不安全。

it is safe here because char is converted to int anyway when calling printf . 这里很安全,因为在调用printf时, char始终会转换为int

see C++ variadic arguments 请参阅C ++可变参数

What you are doing is first = 您首先要做的是=

int x = ch => Assigning the ascii value of the char to an int int x = ch =>将char的ascii值分配给int

And finally : printf("x=%c", x); 最后: printf("x=%c", x); => Printing the ascii value as a char, which will print the actual char that correspond to that value. =>将ascii值打印为一个char,这将打印与该值相对应的实际char。 So yeah it's safe to do that, it's a totally predicatable behaviour. 是的,这样做是安全的,这完全是可预防的行为。
But safe does not mean useful as integer is bigger than char, usually we do the inverse to save some memory. 但是安全并不意味着有用,因为整数大于char,通常我们做相反的操作以节省一些内存。

暂无
暂无

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

相关问题 强制转换隐式无符号整数类型? - Cast implied unsigned integer type? 将字符数据类型打印为整数数据类型 - Printing a character data type as integer data type 如何在C语言中将字符串转换为整数并将其存储在整数数组中? - how to type cast a string into integer in C and store it in the integer array? 将ceil的结果转换为整数是否安全? - Is it safe to cast result of ceil to integer? 编译器将类型字符随机转换为 integer - compiler converts type character to integer randomly 不兼容的指针类型和返回值使整数无强制转换问题 - Incompatible pointer type and return makes integer without a cast issue 如果两个指针都被转换为整数类型,是否将两个指针与 &lt; 未定义行为进行比较? - Is comparing two pointers with < undefined behavior if they are both cast to an integer type? 当您键入将整数值转换为char指针时会发生什么? - What happens when you type cast an integer value into a char pointer? 错误:从较小的 integer 类型“int”转换为“字符串”(又名“字符 *”)[-Werror,-Wint-to-pointer-cast] - error: cast to 'string' (aka 'char *') from smaller integer type 'int' [-Werror,-Wint-to-pointer-cast] 错误:从较小的 integer 类型“int”转换为“string”(又名“char *”)[-Werror,-Wint-to-pointer-cast] - error: cast to 'string' (aka 'char *') from smaller integer type 'int' [-Werror,-Wint-to-pointer-cast]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM