声明/定义作为C和C ++中的语句 [英] Declarations/definitions as statements in C and C++

查看:436
本文介绍了声明/定义作为C和C ++中的语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当它不能在C中编译时,我很困惑:

I was confused when this wouldn't compile in C:

int main()
{
    for (int i = 0; i < 4; ++i)
        int a = 5; // A dependent statement may not be declaration

    return 0;
}

我习惯使用C ++进行编译.我呆呆呆了一段时间,直到我想起关于SO和C和C ++的不同事物如何被视为陈述"的答案.这是关于switch语句的.在C和C ++中,必须在for循环括号后面加上一个语句".这可以通过添加分号或创建{}弯曲的方括号来完成.

I'm used to C++ where this will compile. I just stared dumbfounded for a while until I remembered an answer here on SO about how in C and C++ different things are considered "statements". This was in regard to a switch statement. A "statement" after the for loop brackets must be present both in C and C++. This can be done in both either adding a semicolon or creating a { } squiggly bracket block.

在C ++中,"int a = 7;"被视为声明,定义和初始化.我相信在C语言中,它也被视为所有这些,但是在C语言中,它不被视为声明".

In C++ "int a = 7;" is considered a declaration, definition, and initialisation. In C I believe it is also considered all of these, however in C it is not considered a "statement".

有人能确切说明为什么在C语言中不是语句,而在C ++语言中是语句吗?这使我对陈述是什么的概念感到困惑,因为一种语言说的是,另一种语言说的不是,所以我有点困惑.

Could someone exactly clarify why in C this isn't a statement whereas in C++ it is? This is confusing my concept of what a statement is, because one language says it is, and another says it isn't, so I'm kind of confused.

推荐答案

在C ++中,一条语句是(C ++ 17标准草案)

In C++, a statement is (C++17 standard draft)

excerpt from [gram.stmt]

statement:
    labeled-statement
    attribute-specifier-seqopt expression-statement
    attribute-specifier-seqopt compound-statement
    attribute-specifier-seqopt selection-statement
    attribute-specifier-seqopt iteration-statement
    attribute-specifier-seqopt jump-statement
    declaration-statement
    attribute-specifier-seqopt try-block

init-statement:
    expression-statement
    simple-declaration

declaration-statement:
    block-declaration

...

请注意,C ++中有一些声明语句,它们是声明,也是语句.同样,简单的声明是init语句.并非所有的声明都是语句.声明的语法包含不在语句列表中的内容:

Note that there are declaration statements in C++, which are declarations, and are statements. Similarly, simple declarations are init statements. Not all declarations are statements though. The grammar of declarations contains things that are not in the list of statements:

excerpt from [gram.dcl]

declaration:
    block-declaration
    nodeclspec-function-declaration
    function-definition
    template-declaration
    deduction-guide
    explicit-instantiation
    explicit-specialization
    linkage-specification
    namespace-definition
    empty-declaration
    attribute-declaration

block-declaration:
    simple-declaration
    asm-definition
    namespace-alias-definition
    using-declaration
    using-directive
    static_assert-declaration
    alias-declaration
    opaque-enum-declaration

simple-declaration:
    decl-specifier-seq init-declarator-listopt ;
    attribute-specifier-seq decl-specifier-seq init-declarator-list ;
    attribute-specifier-seqopt decl-specifier-seq ref-qualifieropt [ identifier-list ] initializer ;

...

声明语法列表会持续几页.

The list of declaration grammars continues on for a few pages.

在C语言中,声明为(C11标准草案)

In C, a statement is (C11 standard draft)

excerpt from Statements and blocks

statement:
    labeled-statement
    compound-statement
    expression-statement
    selection-statement
    iteration-statement
    jump-statement

请注意,没有声明是C中的语句.

Note that there are no declarations that are statements in C.

因此,声明的含义在语言中明显不同. C ++中的语句似乎比C中的语句具有更广泛的含义.

So, the meaning of statement is clearly different in the languages. Statement in C++ appears to have a broader meaning than statement in C.

这篇关于声明/定义作为C和C ++中的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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