C和C ++中的sizeof('x') [英] sizeof('x') in C and in C++

查看:76
本文介绍了C和C ++中的sizeof('x')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在阅读C ++常见问题解答,我惊讶地发现sizeof(''x'')是

等于C ++中的sizeof(char),但是等于C中的sizeof(int)。


1.为什么这样?

2.你能提供一个吗?简单的代码片段可以显示其含义

sizeof('''')等于sizeof(int)(我希望它是

sizeof(char )如在C ++中)?

3.如果用C ++代码编译C应用程序,代码是否会中断?


TIA

解决方案



jimjim写道:

大家好,

我正在阅读C ++常见问题解答我很惊讶地发现sizeof('''')在C ++中等于sizeof(char),但在C中等于sizeof(int)。

1。为什么是这样?


Becasue字符文字是C中的整数类型和字符类型

C ++。

2.你能提供一个简单的代码片段,可以显示sizeof('''')等于sizeof(int)的含义(我希望它是像C ++中的sizeof(char))?


不,我现在不能。但是你可以通过打印两者的值来实现。

3.如果使用C ++代码编译C应用程序,代码是否会中断?


C和C ++是两种不同的语言。尽量不要混淆这些

两个。

TIA




-
kr ****** @ india.ti.com


jimjim写道:

大家好,

我正在阅读C ++ FAQ,我惊讶地发现sizeof(''x'')是
等于C ++中的sizeof(char),但是等于C中的sizeof(int)。

1.这是为什么?



" ;由于历史原因


劳伦斯柯比在最近的帖子中对这段历史有所了解

这个小组名为:

为什么一个字符常量大于charcter变量的大小

< vague-memory>

我还记得读一些地方(或者在clc或csc)

BCPL也可能参与其中。

< vague-memory>


< snip>


jimjim写道:

大家好,

我正在阅读C ++ FAQ,我惊讶地发现sizeof(''x'')是
等于C ++中的sizeof(char),但是等于C中的sizeof(int)。

1.为什么会这样?
2.你能提供一个可以显示的简单代码片段吗? sizeof('''')的含义等于sizeof(int)(我希望它是
sizeof(char),就像在C ++中一样)?
3。如果C应用程序是用C ++代码编译的,代码是否中断?

TIA



由于历史原因

搜索为什么字符常量大于字符大小

变量在这一组中(Lawrence Kirby对历史有所了解)


<模糊记忆>

我也依稀记得在某处读书(或者在clc或csc)

BCPL也可能参与其中。

< vague-memory>


< snip>


Hello all,

I was reading the C++ FAQ and I was astonished to learn that sizeof(''x'') is
equal to sizeof(char) in C++, but is equal to sizeof(int) in C.

1. Why is this?
2. Can you provide a simple code fragment that can exibit the implications
of sizeof(''x'') being equal to sizeof(int) (I would expect it to be
sizeof(char) as in C++)?
3. Will the code break if a C application is compiled with C++ code?

TIA

解决方案


jimjim wrote:

Hello all,

I was reading the C++ FAQ and I was astonished to learn that sizeof(''x'') is
equal to sizeof(char) in C++, but is equal to sizeof(int) in C.

1. Why is this?
Becasue character literal is of integer type in C and character type in
C++.
2. Can you provide a simple code fragment that can exibit the implications
of sizeof(''x'') being equal to sizeof(int) (I would expect it to be
sizeof(char) as in C++)?
No, I can''t right now. But you can, by printing the value of both.
3. Will the code break if a C application is compiled with C++ code?
C and C++ are two different language. Try to refrain from mixing these
two.

TIA



--
kr******@india.ti.com


jimjim wrote:

Hello all,

I was reading the C++ FAQ and I was astonished to learn that sizeof(''x'') is
equal to sizeof(char) in C++, but is equal to sizeof(int) in C.

1. Why is this?


"For historical reasons"

Lawrence Kirby shed''s some light on that history in the recent thread in
this group titled:
"Why is a character constant more than size of charcter variable"
<vague-memory >
I also vageuly remember reading some where (either in clc or in csc)
that BCPL also might have had an hand in that.
<vague-memory>

<snip>


jimjim wrote:

Hello all,

I was reading the C++ FAQ and I was astonished to learn that sizeof(''x'') is
equal to sizeof(char) in C++, but is equal to sizeof(int) in C.

1. Why is this?
2. Can you provide a simple code fragment that can exibit the implications
of sizeof(''x'') being equal to sizeof(int) (I would expect it to be
sizeof(char) as in C++)?
3. Will the code break if a C application is compiled with C++ code?

TIA


"For historical reasons"

search for "Why is a character constant more than size of character
variable" in this group (Lawrence Kirby sheds some light on the history)

<vague-memory >
I also vaguely remember reading some where (either in clc or in csc)
that BCPL also might have had an hand in that.
<vague-memory>

<snip>


这篇关于C和C ++中的sizeof('x')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆