You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

忽略约束时,C99语法是否允许数组声明符搭配复合语句的函数定义?

关于C99语法是否允许数组声明符搭配函数体的疑问

先明确C99标准中的相关语法规则:

第6.9.1节(函数定义)的语法规则:

function-definition:
  declaration-specifiers declarator declaration-list_opt compound-statement

declaration-list:
  declaration
  declaration-list declaration

另外,第6.7.5节规定声明符(declarator)可以是数组类型。

问题:忽略所有约束条件的情况下,C99语法是否允许如下写法?

int x[5] { return 0; }

解答

仅从纯文法规则角度看,这段代码确实符合函数定义的语法框架:

  • int 对应 declaration-specifiers(声明说明符)
  • x[5] 是合法的数组类型声明符,匹配 declarator 部分
  • { return 0; } 属于 compound-statement(复合语句)

但必须强调,这只是语法文法层面的合规,C99的约束规则明确禁止这种写法——函数定义的声明符必须是函数类型,数组声明符不能用于函数定义。不过如果只看给定的文法规则,这段代码是符合要求的。

内容的提问来源于stack exchange,提问作者Bogdan Tanasa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 20:20:59