版本1和2间的区别
于2006-06-20 19:50:03修订的的版本1
大小: 1509
编辑: 104
备注:
于2006-06-20 20:10:05修订的的版本2
大小: 1587
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 1: 行号 1:
## page was renamed from Functions and Program Structure/4.8 Block Structure

Navigation(slides)

4.8 Block Structure

C is not a block-structured language in the sense of Pascal or similar languages, because functions may not be defined within other functions. On the other hand, variables can be defined in a block-structured fashion within a function. Declarations of variables (including initializations) may follow the left brace that introduces any compound statement, not just the one that begins a function. Variables declared in this way hide any identically named variables in outer blocks, and remain in existence until the matching right brace. For example, in

   if (n > 0) {
       int i;  /* declare a new i */

       for (i = 0; i < n; i++)
           ...
   }

the scope of the variable i is the "true" branch of the if; this i is unrelated to any i outside the block. An automatic variable declared and initialized in a block is initialized each time the block is entered.

Automatic variables, including formal parameters, also hide external variables and functions of the same name. Given the declarations

   int x;
   int y;

   f(double x)
   {
       double y;
   }

then within the function f, occurrences of x refer to the parameter, which is a double; outside f, they refer to the external int. The same is true of the variable y.

As a matter of style, it's best to avoid variable names that conceal names in an outer scope; the potential for confusion and error is too great.

Navigation(siblings)

TCPL/4.08_Block_Structure (2008-02-23 15:34:55由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.