gpt4 book ai didi

c - "const char "的大小

转载 作者:行者123 更新时间:2023-11-30 20:01:34 26 4
gpt4 key购买 nike

我知道“const”关键字使变量分配为只读变量,但是我不知道常量 char 的大小为何为 4 个字节(例如 GNU C 编译器)的原因,使其有所不同比“char”的大小为一个字节。

cquestions所述博客第一个问题“A”是 const char

printf("%d",sizeof('A'));

Output: 4

最佳答案

'A' 的类型为 int 而不是 const char,以及所有字符常量。

这是从 c11 草案中提取的

6.4.4.4 Character constants

  1. An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'. A wide character constant is the same, except prefixed by the letter L, u, or U. With a few exceptions detailed later, the elements of the sequence are any members of the source character set; they are mapped in an implementation-defined manner to members of the execution character set.

  1. An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. 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. If an integer character constant contains a single character or escape sequence, its value is the one that results when an object with type char whose value is that of the single character or escape sequence is converted to type int.

我将重要部分加粗,以便立即可见。

关于c - "const char "的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30563580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com