免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: hopetoseeyou
打印 上一主题 下一主题

[C] C语言中变量的定义要放在所有执行语句的最前面吗? [复制链接]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
11 [报告]
发表于 2011-03-26 08:15 |只看该作者
本帖最后由 flw 于 2011-03-26 08:24 编辑

你这个写的不好,两个 getc 显然是冗余的。换我的话会这么写:
  1. while(1) {
  2.         char c;
  3.         c = fgetc(fp);

  4.         if ( c == EOF ){
  5.                 break;
  6.         }

  7.         putchar(c);
  8. }
复制代码

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
12 [报告]
发表于 2011-03-26 08:54 |只看该作者
1. C89 确实有这样的规定(虽然准确的描述与lz是不同的……)
2. C99 与 C++ 取消了这样的限制。
3. gcc无论 -std=什么都没有这样的限制, 除非 gcc -std=c89 -pedantic


to 10楼: 对, 是指这样。
你这个写得有点小错,fgetc的返回类型是int。换我的话会这么写:

  1. {
  2.       int c;
  3.       while (c=fgetc(fp), c!=EOF)
  4.             putchar(c);
  5. }
复制代码

论坛徽章:
0
13 [报告]
发表于 2011-03-26 09:17 |只看该作者
我觉得是这样,昨天下午写了一个小程序,也验证了这个,详见博文:


但是,有人回复说,是我的编译器太 ...
hopetoseeyou 发表于 2011-03-25 08:46



我也觉得变量应该就近定义, 但是就近定义也是有麻烦的, 看我刚发的那个帖子你就明白了,
要定义很多变量, 还不容易修改...

论坛徽章:
0
14 [报告]
发表于 2011-03-26 09:20 |只看该作者
可以折衷一下,
在花括号的开始处声明变量,我就是这样做的。
flw 发表于 2011-03-25 20:50



我开始也想这样做,无故添加花括号来提高代码的结构, 感觉也不是很妥当,

论坛徽章:
3
巳蛇
日期:2013-10-03 10:41:48申猴
日期:2014-07-29 16:12:04天蝎座
日期:2014-08-21 09:24:52
15 [报告]
发表于 2011-03-26 10:32 |只看该作者
一对花括号是一个结构。
这段结构里面要用到的变量最好放在这个结构的刚开始处。

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:56:11
16 [报告]
发表于 2011-03-26 10:40 |只看该作者
可以在每段前面定義。

  1. func
  2. {
  3.   int s;

  4.   ///section 1
  5.   {
  6.    int a, b;

  7.   }

  8.   ///section 2
  9.   {
  10.   int x, y;

  11.   }

  12. }
复制代码

论坛徽章:
0
17 [报告]
发表于 2011-03-26 11:04 |只看该作者
1. C89 确实有这样的规定(虽然准确的描述与lz是不同的……)
OwnWaterloo 发表于 2011-03-26 08:54

请教一下具体的条目
davelv认为没有这个规定

论坛徽章:
0
18 [报告]
发表于 2011-03-26 11:38 |只看该作者
就放在紧前头定义。工整好看。如果一个函数的变量太多。函数太长,以至于你看本函数时,都不知道变量是用来做什么用的了。这个函数就要拆开写。

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
19 [报告]
发表于 2011-03-26 13:20 |只看该作者
回复 17# KBTiller

- function-definition

函数定义的组成在 C89 与 C99 大体相同:

-- C89 6.7.1 Function definitions
Syntax
function-definition:
  declaration-specifiersopt declarator declaration-listopt compound-statement

-- C99 6.9.1 Function definitions
Syntax
function-definition:
  declaration-specifiers declarator declaration-listopt compound-statement

都是这几个部分, C89 首部是可选的, 所以可以万恶地省略返回类型……
重点是最后的 compound-statement。
也就是平时所说的"函数体" body

-- C++ 8.4 Function definitions
“函数体” 的概念在 C++ 中更明显:

function-definition:
  decl-specifier-seqopt declarator ctor-initializeropt function-body
  decl-specifier-seqopt declarator function-try-block

function-body:
  compound-statement

也许 "函数体" 的说法是从C++传入的?

总之, return_type function_name( parameters ) 之后的 , 是一个 compound-statement 。

- compound-statement

-- C89 - 6.6.2 Compound statement, or block
Syntax
Compound statement
  { declaration-listopt statement-listopt }

declaration-list:
  declaration
  declaration-list declaration

statement-list:
  statement
  statement-list statement

C89 区分 declaration 与 statement , 且 compound-statement 由 0 或多个 declaration 后跟 0 或多个 statement 组成。
所以声明必须在 compound-statement 中的前部, 在任一语句之前

-- C99 - 6.8.2 Compound statement
Syntax
compound-statement:
  { block-item-listopt }
block-item-list:
  block-item
  block-item-list block-item
block-item:
  declaration
  statement

C99 将 declaration 与 statement 统称为 block-item , compound-statement 由 0 或多个 block-item 组词。
所以 declaration 与 statement 可以交替出现

-- C++ 6.3 Compound statement or block
1 So that several statements can be used where one is expected, the compound statement (also, and equivalently, called “block”) is provided.

compound-statement:
  { statement-seqopt }
statement-seq:
  statement
  statement-seq statement

C++ 里又没有这样统称 (只是将 compound-statement 称为 block ) 。

- statement

-- C89 6.6, C99 6.8
statement:
  labeled-statement
  compound-statement
  expression-statement
  selection-statement
  iteration-statement
  jump-statement

-- C++ - 6 Statements
statement:
  labeled-statement
  expression-statement
  compound-statement
  selection-statement
  iteration-statement
  jump-statement
  declaration-statement
  try-block

C89/C99 将 6 种语句统称为 statement , 然后再将 statement 与 declaration 统称为 block-item 。

C++ 在 statement 中增加 declaration-statement, C++:statement 与 C99:block-item 是相同的(不考虑 try-block) 。
所以C++中声明也是可以随处丢的

C89 若想将声明延后, 只能在组成函数体的 compound-statement 中使用子 compound-statement , 并在其首部写声明。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
20 [报告]
发表于 2011-03-26 18:49 |只看该作者
我开始也想这样做,无故添加花括号来提高代码的结构, 感觉也不是很妥当,
三月廿七 发表于 2011-03-26 09:20

别套近乎。
你这样做当然不妥当了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP