为什么这个程序的输出在C和c++之间不一样?(复制)

时间:2021-11-20 17:28:28

Possible Duplicate:
Size of character ('a') in C/C++

可能重复:字符的大小('a')在C/ c++

The following program

下面的程序

#include <stdio.h>

int main()
{
    printf("%d\n", sizeof('\0'));
    printf("%d\n", sizeof(0));
}

compiled with gcc outputs

使用gcc编译输出

4
4

and with g++

和g + +

1
4

Why is this happening? I know this it's not a compiler thing but a difference between C and C++ but what's the reason?

为什么会这样?我知道这不是编译器的问题而是C和c++之间的区别但是原因是什么呢?

4 个解决方案

#1


35  

In C, character constants have type int per 6.4.4.4(10) of the standard,

在C中,字符常量的类型int为标准的6.4.4.4(10),

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.

整数字符常量具有类型int.包含单个字符的整数字符常量的值映射到单字节执行字符,该值是被解释为整数的映射字符的表示的数值。

Thus you're printing out the size of an int twice.

因此,您要打印两次int的大小。

In C++, character constants have type char.

在c++中,字符常量具有char类型。

#2


12  

The \0 character literal is treated as an int in C, so you actually end up printing sizeof(int) instead of sizeof(char).

\0字符文本在C中被视为int,因此实际上您最终打印的是sizeof(int)而不是sizeof(char)。

ideone gives the same results (C, C++).

ideone提供了相同的结果(C, c++)。

#3


4  

In C character literals are ints. In C++ they are chars.

在C字符中,字面值是ints。在c++中,它们是chars。

#4


0  

The output is already compiler dependent since you use the wrong format specifier for size_t this would be %zu. On 64 bit systems with size_t wider than int this could give you any sort of garbage.

由于对size_t使用了错误的格式说明符,因此输出已经依赖于编译器。在比int宽的64位系统上,这可能会给您带来任何类型的垃圾。

Otherwise this shure is compiler dependent. Both values that you try to print the size of are int in C and char and int in C++. So in C this should usually give you 4 4 or 8 8 and in in C++ 1 4 or 1 8.

否则,这个shure是依赖于编译器的。您尝试打印的两个值都是在C和char中都是int型的,在c++中是int型的。在C中,这通常是4或8,在C+ 1 + 4或1 8。

#1


35  

In C, character constants have type int per 6.4.4.4(10) of the standard,

在C中,字符常量的类型int为标准的6.4.4.4(10),

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.

整数字符常量具有类型int.包含单个字符的整数字符常量的值映射到单字节执行字符,该值是被解释为整数的映射字符的表示的数值。

Thus you're printing out the size of an int twice.

因此,您要打印两次int的大小。

In C++, character constants have type char.

在c++中,字符常量具有char类型。

#2


12  

The \0 character literal is treated as an int in C, so you actually end up printing sizeof(int) instead of sizeof(char).

\0字符文本在C中被视为int,因此实际上您最终打印的是sizeof(int)而不是sizeof(char)。

ideone gives the same results (C, C++).

ideone提供了相同的结果(C, c++)。

#3


4  

In C character literals are ints. In C++ they are chars.

在C字符中,字面值是ints。在c++中,它们是chars。

#4


0  

The output is already compiler dependent since you use the wrong format specifier for size_t this would be %zu. On 64 bit systems with size_t wider than int this could give you any sort of garbage.

由于对size_t使用了错误的格式说明符,因此输出已经依赖于编译器。在比int宽的64位系统上,这可能会给您带来任何类型的垃圾。

Otherwise this shure is compiler dependent. Both values that you try to print the size of are int in C and char and int in C++. So in C this should usually give you 4 4 or 8 8 and in in C++ 1 4 or 1 8.

否则,这个shure是依赖于编译器的。您尝试打印的两个值都是在C和char中都是int型的,在c++中是int型的。在C中,这通常是4或8,在C+ 1 + 4或1 8。