C/C++ 中字符 ('a') 的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2172943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Size of character ('a') in C/C++
提问by whacko__Cracko
What is the size of character in C and C++ ? As far as I know the size of char is 1 byte in both C and C++.
C 和 C++ 中字符的大小是多少?据我所知,C 和 C++ 中 char 的大小都是 1 个字节。
#include <stdio.h>
int main()
{
printf("Size of char : %d\n", sizeof(char));
return 0;
}
#include <iostream>
int main()
{
std::cout << "Size of char : " << sizeof(char) << "\n";
return 0;
}
No surprises, both of them gives the output : Size of char : 1
毫不奇怪,他们都给出了输出: Size of char : 1
Now we know that characters are represented as 'a'
,'b'
,'c'
,'|'
,... So I just modified the above codes to these:
现在我们知道字符表示为'a'
, 'b'
, 'c'
, '|'
,... 所以我只是将上面的代码修改为这些:
In C:
在 C 中:
#include <stdio.h>
int main()
{
char a = 'a';
printf("Size of char : %d\n", sizeof(a));
printf("Size of char : %d\n", sizeof('a'));
return 0;
}
Size of char : 1
Size of char : 4
In C++:
在 C++ 中:
#include <iostream>
int main()
{
char a = 'a';
std::cout << "Size of char : " << sizeof(a) << "\n";
std::cout << "Size of char : " << sizeof('a') << "\n";
return 0;
}
Size of char : 1
Size of char : 1
Why the sizeof('a')
returns different values in C and C++?
为什么sizeof('a')
在 C 和 C++ 中返回不同的值?
回答by
In C, the type of a character constantlike 'a'
is actually an int
, with size of 4 (or some other implementation-dependent value). In C++, the type is char
, with size of 1. This is one of many small differences between the two languages.
在C中,一个字符的类型恒定像'a'
实际上是一个是int
,具有4的大小(或一些其它的实现相关的值)。在 C++ 中,类型为char
,大小为 1。这是两种语言之间的许多小差异之一。
回答by David R Tribble
As Paul stated, it's because 'a'
is an int
in C but a char
in C++.
正如保罗所说,这是因为在 C 中是 a 而在 C++ 中'a'
是int
a char
。
I cover that specific difference between C and C++ in something I wrote a few years ago, at: http://david.tribble.com/text/cdiffs.htm
我在几年前写的文章中介绍了 C 和 C++ 之间的具体区别,网址为:http: //david.tribble.com/text/cdiffs.htm
回答by Smith
In C the type of character literals are intand charin C++. This is in C++ required to support function overloading. See this example:
在 C 中,字符文字的类型在 C++ 中是int和char。这是在 C++ 中需要支持函数重载。看这个例子:
void foo(char c)
{
puts("char");
}
void foo(int i)
{
puts("int");
}
int main()
{
foo('i');
return 0;
}
Output:
输出:
char
回答by msc
In C language, character literal is not a char
type. C considers character literal as integer. So, there is no difference between sizeof('a')
and sizeof(1)
.
在 C 语言中,字符字面量不是char
类型。C 将字符文字视为整数。因此,sizeof('a')
和之间没有区别sizeof(1)
。
So, the sizeof character literal is equal to sizeof integer in C.
因此,sizeof 字符字面量等于 C 中的 sizeof 整数。
In C++ language, character literal is type of char
. The cppreferencesay's:
在 C++ 语言中,字符字面量是char
. 该cppreference说的:
1) narrow character literal or ordinary character literal, e.g.
'a'
or'\n'
or'\13'
. Such literal has typechar
and the value equal to the representation of c-char in the execution character set. If c-char is not representable as a single byte in the execution character set, the literal has type int and implementation-defined value.
1)窄字符字面量或普通字符字面量,例如
'a'
or'\n'
或'\13'
。这种文字的类型char
和值等于执行字符集中 c-char 的表示。如果 c-char 在执行字符集中不能表示为单个字节,则文字具有类型 int 和实现定义的值。
So, in C++ character literal is a type of char
. so, size of character literal in C++ is one byte.
因此,在 C++ 中,字符文字是char
. 因此,C++ 中字符文字的大小是一个字节。
Alos, In your programs, you have used wrong format specifier for sizeof
operator.
Alos,在您的程序中,您对sizeof
运算符使用了错误的格式说明符。
C11 §7.21.6.1 (P9) :
C11 §7.21.6.1 (P9) :
If a conversion specification is invalid, the behavior is undefined.275) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
如果转换规范无效,则行为未定义。275)如果任何参数不是相应转换规范的正确类型,则行为未定义。
So, you should use %zu
format specifier instead of %d
, otherwise it is undefined behaviour in C.
因此,您应该使用%zu
格式说明符而不是%d
,否则它是 C 中未定义的行为。