[英]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次转换,并且没有强制转换:
a
is character of the C encoding. a
是C编码的字符。 'a'
converts to an int
at compile time. 'a'
在编译时转换为int
。
'a'
The int
is converted to a char
. 将
int
转换为char
。
char ch = 'a';
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. 从理论上讲 ,从
char
到int
**的数据可能会丢失,但是鉴于压倒性的实现,没有任何实现。 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;
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
. **
char
和int
的大小可以相同,并且char
的无符号数比int
。 This is the one potential problem with casting char
to int
although typically there is not loss of data. 尽管通常不会丢失数据,但这是将
char
为int
的一个潜在问题。 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.