为什么会出现这种使用逗号工作,在一个前pression,但在声明中失败? [英] Why does this use of comma work in a expression but fail in a declaration?

查看:132
本文介绍了为什么会出现这种使用逗号工作,在一个前pression,但在声明中失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从高位OOP语言C#和Java Am与最​​近开始抓我的头C.我觉得℃,有点怪怪的是同样的人觉得JS是。所以,希望下面澄清:

Am from high level OOP languages C# and Java and recently started scratching my head in C. I feel C a bit weird as equally as one feels JS is. So want to clarify below:

列出了错误,这似乎直观的,因为它看起来不正确的语法,即使在OOP语言

Below gives error and that seems intuitive as it looks like incorrect syntax even in OOP languages

int i=0,1,2;  

/*
Error : expected identifier or ‘(’ before numeric constant
 int i = 0, 1, 2;
            ^
*/

但下面的工作令人惊讶的:

However below works surprisingly:

int i;
i = 0,1,2;    //works

为什么这种行为?是他们的任何意义,以保持这种行为或只是一些技术性的解析?

Why is this behavior? Is their any significance to keep such behavior or just some parsing technicalities?

推荐答案

这实际上是一个棘手的问题,因为它依赖于C的语法是复杂的细节。理解它的最好的来源是草案,我们可以使用附件A 语言语法摘要的作为一个参考。

This is actually a tricky question because it relies on the details of the C grammar which is complicated. The best source for understanding it is the draft standard, we can use Annex A Language syntax summary as a reference.

的基本思想是:

int i=0,1,2; 

是一个声明和:

i = 0,1,2; 

是一个前pression。

is an expression.

在一个前pression我们能有逗号操作它计算左手侧(一般副作用的),丢弃的结果,然后计算右边等等...

In an expression we can have the comma operator which evaluates the left hand side(usually for side effects), throws away the result and then evaluates the right hand side etc...

在声明中逗号是一个语法分隔不是逗号操作符。在分离说明符,自 1 2 不在此声明的背景是声明符不正确的语法:

In declaration the comma is a grammatical separator not the comma operator. The , separates declarators and since 1 and 2 are not declarators in the context of a declaration this is incorrect syntax:

 int i=0,1,2;
        ^^^^

从C99标准相关的语法如下:

The relevant grammar from the C99 standard is as follows:

init-declarator-list:
    init-declarator
    init-declarator-list , init-declarator   <--- here comma is just a seperator
init-declarator:
    declarator
    declarator = initializer

因此​​,在这种情况下,中隔离的的init声明符的这可以是一个的声明符的或声明符=初始的既不 1 也不 2 说明符的,所以我们有不正确的语法。

So in this case the , separates init-declarators which can either be a declarator or a declarator = initializer neither 1 nor 2 are declarators and so we have incorrect syntax.

有没有价值,一个的初始化的可以是一个的分配-EX pression 的,但这个前pression不为我们提供了一个光秃秃的路径的逗号操作符的,虽然我们可以在用逗号运营结束()通过初级-EX pression ),但是这不会像隔板。

It is worth nothing that an initializer can be an assignment-expression but this expression does not gives us a path to a bare comma operator, although we could end up with a comma operator in ()(through primary-expression) but this would not look like a separator.

有关除权pression从节中的相关语法 6.5.17 是:

For the expression the relevant grammar from section 6.5.17 is:

expression:
    assignment-expression
    expression , assignment-expression

逗号运算的描述的如下:

一个逗号操作符的左操作数计算为一个空白
  前pression;还有就是它的评估之后序列点。然后,
  右操作数进行评估;结果有它的类型和值。[...]

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.[...]

注意到的逗号操作符的拥有的最低precedence 下面前pression:

Noting that the comma operator has the lowest precedence the following expression:

i = 0,1,2; 

相当于:

(i = 0),1,2; 

I 将获得 0 的价值和进一步的评估结果被扔掉。

and so i will get the value of 0 and the results of the further evaluations are thrown away.

这篇关于为什么会出现这种使用逗号工作,在一个前pression,但在声明中失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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