英文:
Is it expected for sizeof(char) to return 1 instead of 4 when integer promotion takes place in C?
问题
Output: d 1
当C中隐式进行整数提升时,为什么char的大小打印为1(以字节为单位),而不是4(以字节为单位),因为大小应该增加到32位,对吗?
我认为输出应该是:d 4
英文:
#include <stdio.h>
int main()
{
unsigned char c = 'a';
c = c + 3;
printf("%c ", c);
printf("%zu",sizeof(c));
return 0;
}
Output:d 1
when integer promotion takes place implicitly in c. then why the size of char is printing 1(in bytes) rather 4(in bytes), since the size should increase to 32bits right?
Iam thinking that the output shall be: d 4
答案1
得分: 3
这是正确的,计算c + 3
涉及整数提升,即结果的类型是int
。然而,当您使用c = ...
将该结果存储在您的char
变量中时,它将再次转换回char
。赋值操作不会(也不能)更改变量的大小。
尝试这段代码:
char c = 'a';
printf("%zu\n", sizeof(c));
printf("%zu\n", sizeof(c + 3));
c = c + 3;
printf("%zu\n", sizeof(c));
在我的系统上,它的输出是:
1
4
1
英文:
It's correct that the calculation of c + 3
involves integer promotion, i.e. the type of the result is int
. However, when you store that result in your char
variable using c = ...
it will be converted back to a char
again. The assignment will not (can not) change the size of your variable.
Try this code:
char c = 'a';
printf("%zu\n", sizeof(c));
printf("%zu\n", sizeof(c + 3));
c = c + 3;
printf("%zu\n", sizeof(c));
On my system it gives:
1
4
1
答案2
得分: -1
请参考C99标准或C11标准,第6.5.3.4节标题为'sizeof和_Alignof运算符' - 点#4
当sizeof应用于具有char、unsigned char或signed char类型(或其修饰版本)的操作数时,结果为1。
对于C99标准,第6.5.3.4节标题为'sizeof运算符' - 点#3
当应用于具有char、unsigned char或signed char类型(或其修饰版本)的操作数时,结果为1。
这意味着,无论使用哪种编译器实现或环境,char
的大小始终为1。
在C99标准的第5.1.2.3节中有一段引用的示例代码,参考编号#10
例2
在执行片段
char c1, c2;
/* ... */
c1 = c1 + c2;
时,“整数提升”要求抽象机器将每个变量的值提升到int大小,然后将这两个int相加并截断总和。只要两个char的相加不会溢出,或者溢出会静默地包装以产生正确的结果,实际执行只需产生相同的结果,可能会省略提升。
因此,根据问题的基础,是的,将发生整数提升,然后重新评估为char
类型后,结果将始终返回1。只要值没有溢出并且不超过字节值的最大大小,即255。
英文:
Please see the C99 Standard or C11 Standard, under section 6.5.3.4 titled 'The sizeof and _Alignof operators' - Point #4
> When sizeof is applied to an operand that has type char, unsigned
> char, or signed char, (or a qualified version thereof) the result is 1
For the C99 standard, under section 6.5.3.4 titled The sizeof operator Point #3 -
> When applied to an operand that has type char, unsigned char, or
> signed char, (or a qualified version thereof) the result is 1.
Meaning, no matter what implementation of compiler used or environment, the size of char
, the result will always be 1.
There is a quoted sample of code under Section 5.1.2.3 of the C99 standard, reference #10
> EXAMPLE 2
> In executing the fragment
char c1, c2;
/* ... */
c1 = c1 + c2;
> the ‘‘integer promotions’’ require that the abstract machine
> promote the value of each variable to int size and then add the two
> ints and truncate the sum. Provided the addition of two chars can be
> done without overflow, or with overflow wrapping silently to produce
> the correct result, the actual execution need only produce the same
> result, possibly omitting the promotions.
So, on the basis of the question, yes, the integer promotion will happen, it is after re-evaluating back to type of char
will always return back 1 afterwards. As long as there's no over-flow of the value and that it falls under the maximum size of byte value, i.e. 255.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论