ISO C 中数组的左值到右值转换

发布于 2024-09-27 11:27:25 字数 690 浏览 8 评论 0原文

C++ ANSI ISO IEC 14882 2003 附录 C.1(第 668 页):

更改:条件表达式、赋值表达式或逗号表达式的结果可能是 Bean 左值
理由:C++是面向对象的语言,相对更注重左值。例如,函数可能返回左值。
对原始特征的影响:更改明确定义的特征的语义。某些隐式依赖左值到右值转换的 C 表达式将产生不同的结果。例如,

char arr[100];
sizeof(0, arr)

在 C++ 中生成 100,在 C 中生成 sizeof(char*)
...

我今天刚读到这篇文章,我记得几个月前我的一个朋友提出了一个问题,即编写一个函数,如果用 C++ 编译,则返回 0,如果用 C 编译,则返回 1我利用 C 中结构位于外部作用域的事实解决了这个问题。因此,考虑到这个新信息,我决定这将是上述问题的另一种解决方案,我在 Microsoft Visual Studio 2008 上尝试过,但无论它是编译为 C 还是 C++ 代码 sizeof(0, arr) 总是产生 4。所以有 2 个问题:

1.什么是 ISO C?这是当前的C标准吗?是唯一的吗(听说C正在快速发展) 2. 这是微软C++的bug吗?

TIA

编辑:抱歉与输出混淆并对其进行了编辑:

C++ ANSI ISO IEC 14882 2003 Annex C.1 (page 668):

Change: The result of a conditional expression, an assignment expression, or a comma expression may bean lvalue
Rationale: C + + is an object-oriented language, placing relatively more emphasis on lvalues. For example, functions may return lvalues.
Effect on original feature: Change to semantics of well-defined feature. Some C expressions that implicitly rely on lvalue-to-rvalue conversions will yield different results. For example,

char arr[100];
sizeof(0, arr)

yields 100 in C + + and sizeof(char*) in C.
...

I was reading this just today and I remembered that a couple of months a go a friend of mine proposed a problem wchich was to write a function that would return 0 if it were compiled with C++ and 1 if it were compiled with C. I solved it taking advantage of the fact that in C a struct was in the outer scope. So, considering this new information, I decided that this would be another solution to the above problem, which I tried on Microsoft Visual Studio 2008, but regardless of whether it is compiled as C or C++ code sizeof(0, arr) always yields 4. So 2 questions:

1.What is ISO C? Is it the current C standard? Is it the only one (I hear C is rapidly evolving)
2. Is this a microsoft C++ bug?

TIA

Edit: Sorry got mixed up with the output and edited it:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流心雨 2024-10-04 11:27:25

ISO C 是 C 标准。目前的版本是 C99,但 C1x 即将推出。如果所说的快速是指每十年左右就有一个新标准,那么是的,它正在迅速发展:-)

ISO C99 第 6.5.3.4/3 节指出:

当应用于 char、unsigned char 或signed char(或其限定版本)类型的操作数时,结果为 1。

当应用于具有数组类型的操作数时,结果是数组中的字节总数。

ISO C is the C standard. The current one is C99 but C1x is right around the corner. If by rapid, you mean a new standard every decade or so, then yes, it is rapidly evolving :-)

Section 6.5.3.4/3 of ISO C99 states:

When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.

When applied to an operand that has array type, the result is the total number of bytes in the array.

海的爱人是光 2024-10-04 11:27:25

或者只是微软的 C 不是 ISO C,而是其他一些标准 C(如果存在的话)。

Microsoft Visual C 仍然[仅]支持 C89,而 gcc/clang 等其他编译器也支持 C99是现行标准。

C99 [第 6.5.17/2 节] 说

逗号运算符的左操作数被计算为 void 表达式;评估后有一个序列点。然后计算正确的操作数;结果有其类型和值。95

因此 sizeof (0,arr) 的结果将是 sizeof(char*)[由于隐式左值右值转换/自动衰减为指针类型]不是100*sizeof(char)

sizeof(arr) 会给出来自 6.5.3.4/3100*sizeof(char)

95) 逗号运算符不会产生左值。


决定这将是上述问题的另一种解决方案,我在 Microsoft Visual Studio 2008 上尝试过,但无论它是编译为 C 还是 C++ 代码,sizeof(0, arr) 总是产生 4。

C++03 [5.18/1] 逗号运算符

类型和
结果的值是右操作数的类型和值;如果其右操作数是,则结果是左值。

所以 sizeof(0, arr) = sizeof (arr) 并且等于 100* sizeof(char) 而不是 = sizeof(char*)< /代码>。

因此 MSVC++ 给出了错误的结果(对于 C++ 代码)。

Or just microsoft's C is not ISO C but some other standard C (if there exists any).

Microsoft Visual C still supports C89 [only] whereas other compilers like gcc/clang etc support C99 too which is the current Standard.

C99 [Section 6.5.17/2] says

The left operand of a comma operator is evaluated as a void expression; there is a sequence point after its evaluation. Then the right operand is evaluated; the result has its type and value.95

Thus the result of sizeof (0,arr) would be sizeof(char*)[due to the implicit lvalue to rvalue conversion /automatic decay to pointer type] not 100*sizeof(char)

sizeof(arr) would have given 100*sizeof(char) from 6.5.3.4/3

95) A comma operator does not yield an lvalue.


decided that this would be another solution to the above problem, which I tried on Microsoft Visual Studio 2008, but regardless of whether it is compiled as C or C++ code sizeof(0, arr) always yields 4.

C++03 [5.18/1] Comma Operator

The type and
value of the result are the type and value of the right operand; the result is an lvalue if its right operand is.

So sizeof(0, arr) = sizeof (arr) and which would be equal to 100* sizeof(char) and not = sizeof(char*).

So MSVC++ is giving incorrect result (in case of C++ code).

嗼ふ静 2024-10-04 11:27:25

对于数组,sizeof 返回总大小。请小心作为指针传递的数组。

C99标准:

当应用于具有数组的操作数时
类型,结果为数组的总字节数

For arrays, the sizeof returns the total size. Be careful about arrays passed as pointers.

C99 standard:

When applied to an operand that has array
type, the result is the total number of bytes in the array

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文