Print decimal value of a char
打印字符十进制值的程序:
1
2 3 4 5 6 7 8 9 |
为什么打印第二个字符的十进制值,为什么不是第一个字符的十进制值?
输出:ch is 66
- ‘AB’ 不是合法的字符常量。
- 这实际上编译没有错误吗? ‘AB’ 不是字符,它是两个字符。
- @Useless 是的,它正在编译没有错误。
- stackoverflow.com/a/34571914/2067492
- 它被视为一个 int,并分配给一个 char,因此它被截断。 stackoverflow.com/a/7755280/2067492。
- @stark:多字符常量是合法的;该标准描述并说明了它们。它只是说该值是实现定义的。
- multichar.c:5:13: warning: multi-character character constant
- 在字符中分配”ABCDEF”是合法的。请看下面我的帖子。如果我们将”AB”分配给字符,编译器会抛出错误。
- @stark ‘AB’ 和 ‘A’ 都不是 char 常量。两者都是 int 类型的整数字符常量。 ‘AB’ 带有实现定义的行为。
因为 ‘AB’ 是一个多字符常量,其值是由实现定义的,所以无论它是否是 66 在原则上和实践中都是”不可预测的”,尽管在不同的实现中可预测是不一样的。
通常,您只在单引号中间使用单个字符。如果使用多个字符,
如果你使用过 gcc,那么根据这个来源会发生这种情况
The compiler evaluates a multi-character character constant a character at a time, shifting the previous value left by the number of bits per target character, and then or-ing in the bit-pattern of the new character truncated to the width of a target character. The final bit-pattern is given type int, and is therefore signed, regardless of whether single characters are signed or not. If there are more characters in the constant than would fit in the target int the compiler issues a warning, and the excess leading characters are ignored.
For example, ‘ab’ for a target with an 8-bit char would be interpreted as a€?(int) ((unsigned char) ‘a’ * 256 + (unsigned char) ‘b’)a€?, and ‘\\234a’ as a€?(int) ((unsigned char) ‘\\234’ * 256 + (unsigned char) ‘a’)a€?
- 所以这并不意味着它只需要第二个字符?
- 一点也不,虽然我们不知道实现的细节。但肯定不是因为它忽略了第一个字符而只取了第二个字符。
- 谢谢你帮助我。
- 不同意”定义的实现,无论是否为 66 都不是真正可预测的。”。这是可以预见的。简单回顾一下平台的实现。
- @chux 你能详细说明一下吗?
- @chux根据标准显然不一定是可预测的C11 $6.4.4.4(字符常量):。但是当然是”不可预测”意味着它是可以预测的,因为您知道有关实现的细节,这就是为什么我添加 gcc 实现细节以进行说明。
- @IharobAlAsimi 实现定义的行为是可预测的。使用 ID,每个实现都记录了如何做出选择。
- @chux 我想这就是我所说的。如果您认为措辞可以改进,请直接告诉我如何改进。
- 假设一个实现将值定义为随机生成的数字……这是否是一个有效的实现定义?我当然不会称之为可预测的。
- @grek40 C 允许在实现定义的行为中进行各种行为。随机是理论上的还是你有编译器实现的行为的例子?
- @chux我认为”让我们说”是理论上的。
- @chux 绝对是理论上的……但是,如果我对生活感到厌倦,我需要编写一个病态的 C 编译器,它故意混淆所有实现定义的可能性,并在任何 UB 情况下将用户困在俄罗斯方块游戏中……
- @grek40 即使您曾经编写过这样的编译器,如果他们的代码是可移植的,程序员也不应该依赖实现定义的行为。所以你假设的编译器会很有趣,并且由于它符合标准,它也可以编译有用的程序。
- @ grek40 perthis 答案,”必须为阅读编译器文档的用户提供足够的信息,以准确预测特定情况下会发生什么。”所以在可能和实际的领域里,随机行为是不符合实际标准的。
-
根据这个,我会使用”未指定”而不是”不可预测”。回想一下,char ch = ‘A’; printf(“ch is %d\
“,ch); 未指定打印 65。它是实现定义的行为 (ASCII)。只是这种行为得到了极大的同意,而 ‘AB’ 则不太一致。
1
2 3 |
Why it is printing the decimal value of second character,why not the first character’s decimal value?
‘AB’ 是一个 int 字符常量。
The value of an integer character constant containing more than one character (e.g., ‘ab’), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. C11 ?§6.4.4.4 10
示例:您的输出可能不同。
1
2 |
16706 与 0x4142 的值相同,0x4142 是 ASCII A 和 B 的连接值。 ch is 16961 (0x4241) 或 ch is 1111556096 (0x42410000) 或其他的打印输出是可能的。这是实现定义的行为。
将 16706 分配给 char 是实现定义的行为或明确定义的行为 – 取决于 char 是有符号还是无符号。一个常见的 ID 结果是分配低字节,或 0x42。
`
1
2 |
将 char 范围之外的值分配给 char 可能会引发警告。
1
2 3 4 |
// Example warning
// warning: overflow in implicit constant conversion [-Woverflow] char ch1 = ‘AB’; char ch2 = 16706; |
此外,鉴于此类辅音的实现定义性质,以下内容也可能会发出警告:
1
2 3 |
// Example warning
// warning: multi-character character constant [-Wmultichar] char ch1 = ‘AB’; |
多字符字符常量的使用仅限于少数选择情况。如此之少,它更有可能是一个很好用的编码错误。
C11 $6.4.4.4(字符常量):
A multi-char always resolves to an int, but that the exact value is
a€?implementation-dependenta€?. That is, different compilers may resolve
the same multi-char to different integers. This is a portability
problem, and it is one of the reasons multi-chars are discouraged.
表示int ch = ‘AB’;对于int类型是可以的。但是,如果它被声明为类型char,第二个字节将不会被保留。
所以用
1
|
char ch = ‘A’;
|
而不是
1
|
char ch = ‘AB’;
|
并为 char 类型使用 %c 格式说明符。
1
2 |
-
将 “%d” 与 printf(“ch is %d\
“,ch); 一起使用并没有错 - “并为 char 类型使用 %c 格式说明符。”不,OP想要一个十进制值。
伊哈罗布是对的。 ‘AB’ 是一个多字符结果是不可预测的。如果您打算保留两个字符 ‘AB’,我建议将此常量声明为字符串 char ab[] =”AB”;。字符串的打印可以是 printf(“ch is %d\
“, ab[0]);,以将 65 作为输出。
- 这将完整打印字符串,而不是执行 OP 要求的操作。
- 你说得对。已更改打印以适合第一个字符的输出。
- 它仍然不正确,现在它将打印第一个元素的地址。
首先,这个程序会抛出一个类似
的警告
1
2 3 |
4:12: warning: multi–character character constant [–Wmultichar]
In function ‘int main()’: 4:12: warning: overflow in implicit constant conversion [–Woverflow] |
如果您了解运算符优先级的概念,将帮助您了解为什么要获取第二个字符 (B) 的十进制值。在赋值运算符中,优先级从右到左开始。所以在这种情况下,最右边的字符具有更高的优先级并存储到 char ch 中,其余字符被忽略。
输出:
1
2 |
ch is 66
ch1 is ‘G’ |
另一个使用赋值运算符的例子:
1
2 3 4 5 6 7 8 9 |
#include<stdio.h>
int main(void){ int a = (2,3,4,5,6); // Here 6 is assigned to ‘a’ and remaining values are ignored. } |
输出:
1
|
a is 6
|
请通过以下链接了解运算符优先级。
http://en.cppreference.com/w/c/language/operator_precedence
- 如果我们只有一个运算符,那么运算符优先级有多高?多字符文字中没有逗号。
- “所以在这种情况下,最右边的字符具有更高的优先级并存储到 char ch 中,其余字符将被忽略。”是不正确的——但它肯定是一种思考字符解析和运算符优先级的新颖方式。
- 但是确切的值是一个€?implementation-dependenta€?没有什么可说的了…
来源:https://www.codenong.com/45461862/