有没有实际的限制多少巢式块你可以有。然而,它通常是好试图巢式块数最多为3(也许4块深如果你有需要更多的功能,很可能打破你的时间函数为多小的功能)。
局部变量
有变量的范围确定谁可以看到变长,它和如何生活。变量称为局部宣布在块变量和局部变量的范围,有块(也被称为局部作用域)。变量以及块范围只能在块他们宣称,并尽快把这简
单功能块结束摧毁::
1
2
3
4
5
6
7
8
9
10
int main()
{ // start main block
int nValue = 5; // nValue created here
double dValue = 4.0; // dValue created here
return 0;
} // nValue and dValue destroyed here
因为值,值是在定义的主要功能块的声明,他们都被main()执行完毕。
一块中声明的变量只能在该块内见。因为每个函数都有它自己的块,在一个函数中的变量不能从另一个功能:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void someFunction()
{
int nValue;
}
int main()
{
// nValue can not be seen inside this function.
someFunction();
// nValue still can not be seen inside this function.
return 0;
}
嵌套块中声明的变量就被内块结束:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
int nValue = 5;
{ // begin nested block
double dValue = 4.0;
} // dValue destroyed here
// dValue can not be used here because it was already destroyed!
return 0;
} // nValue destroyed here