c语言中字符a为多少,C / C ++中字符(';a';)的大小

本文翻译自:Size of character ('a') in C/C++

What is the size of character in C and C++ ? C和C ++中的字符大小是多少? As far as I know the size of char is 1 byte in both C and C++. 据我所知,在C和C ++中char的大小均为1个字节。

In C: 在C中:

#include int main()

{

printf("Size of char : %d\n", sizeof(char));

return 0;

}

In C++: 在C ++中:

#include 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' , '|' 现在我们知道字符被表示为'a' , 'b' , 'c' , '|' ,... So I just modified the above codes to these: ,...所以我刚刚将以上代码修改为:

In C: 在C中:

#include int main()

{

char a = 'a';

printf("Size of char : %d\n", sizeof(a));

printf("Size of char : %d\n", sizeof('a'));

return 0;

}

Output: 输出:

Size of char : 1

Size of char : 4

In C++: 在C ++中:

#include int main()

{

char a = 'a';

std::cout << "Size of char : " << sizeof(a) << "\n";

std::cout << "Size of char : " << sizeof('a') << "\n";

return 0;

}

Output: 输出:

Size of char : 1

Size of char : 1

Why the sizeof('a') returns different values in C and C++? 为什么sizeof('a')在C和C ++中返回不同的值?

#1楼

参考:https://stackoom.com/question/97ht/C-C-中字符-a-的大小

#2楼

As Paul stated, it's because 'a' is an int in C but a char in C++. 正如保罗所说,这是因为'a'在C语言中是int ,而在C ++中是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

#3楼

In C, the type of a character constant like 'a' is actually an int , with size of 4 (or some other implementation-dependent value). 在C语言中,像'a'这样的字符常量的类型实际上是一个int ,其大小为4(或其他一些与实现相关的值)。 In C++, the type is char , with size of 1. This is one of many small differences between the two languages. 在C ++中,类型为char ,大小为1。这是两种语言之间的许多小差异之一。

#4楼

In C the type of character literals are int and char in C++. 在C语言中,字符文字的类型在C ++中为int和char 。 This is in C++ required to support function overloading . 这是C ++中支持函数重载所必需的。 See this example: 请参阅以下示例:

void foo(char c)

{

puts("char");

}

void foo(int i)

{

puts("int");

}

int main()

{

foo('i');

return 0;

}

Output: 输出:

char

#5楼

In C language , character literal is not a char type. 在C语言中 ,字符文字不是char类型。 C considers character literal as integer. C将字符文字视为整数。 So, there is no difference between sizeof('a') and sizeof(1) . 因此, 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 . 在C ++语言中 ,字符文字是char类型。 The cppreference say's: cppreference说的是:

1) narrow character literal or ordinary character literal, eg 'a' or '\\n' or '\\13' . 1) 窄字符文字或普通字符文字,例如'a'或'\\n'或'\\13' 。 Such literal has type char and the value equal to the representation of c-char in the execution character set. 这种文字的类型为char ,其值等于执行字符集中c-char的表示形式。 If c-char is not representable as a single byte in the execution character set, the literal has type int and implementation-defined value. 如果c-char在执行字符集中不能表示为单个字节,则文字具有int类型和实现定义的值。

So, in C++ character literal is a type of char . 因此,在C ++中,字符文字是char 。 so, size of character literal in C++ is one byte. 因此,C ++中字符文字的大小为1个字节。

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中是未定义的行为。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值