什么是C语言局部程序块(local block)
来源:互联网 发布:苹果8手机怎么打开网络 编辑:程序博客网 时间:2024/05/22 14:53
局部程序块是指一对大括号({})之间的一段C语言程序。一个C函数包含一对大括号,这对大括号之间的所有内容都包含在一个局部程序块中。if语句和swich语句也可以包含一对大括号,每对大括号之间的代码也属于一个局部程序块。
此外,你完全可以创建你自己的局部程序块,而不使用C函数或基本的C语句。
下面是一个使用局部程序块的例子:
#include <stdio.h>
void main(void);
void main()
{
/ * Begin local block for function main() * /
int test_ var = 10;
printf("Test variable before the if statement: %d\n", test_var);
if (test_var>5)
{
/ * Begin local block for "if" statement * /
int test_ var = 5;
printf("Test variable within the if statement: %d\n",
test_var);
{
/ * Begin independent local block (not tied to
any function or keyword) * /
int test_var = 0;
printf (
"Test variable within the independent local block: %d\n",
test_var)
}
/ * End independent local block * /
printf ("Test variable after the if statement: %d\n", test_var);
}
/*End local block for function main () * /
上例产生如下输出结果:
Test variable before the if statement: 10
Test variable within the if statement: 5
Test variable within the independent local block:0
Test variable after the if statement: 10
此外,你完全可以创建你自己的局部程序块,而不使用C函数或基本的C语句。
- 你可以在局部程序块中说明一些变量,这种变量被称为局部变量,它们只能在局部程序块的开始部分说明,并且只在说明它的局部程序块中有效。
- 如果局部变量与局部程序块以外的变量重名,则前者优先于后者。
下面是一个使用局部程序块的例子:
#include <stdio.h>
void main(void);
void main()
{
/ * Begin local block for function main() * /
int test_ var = 10;
printf("Test variable before the if statement: %d\n", test_var);
if (test_var>5)
{
/ * Begin local block for "if" statement * /
int test_ var = 5;
printf("Test variable within the if statement: %d\n",
test_var);
{
/ * Begin independent local block (not tied to
any function or keyword) * /
int test_var = 0;
printf (
"Test variable within the independent local block: %d\n",
test_var)
}
/ * End independent local block * /
printf ("Test variable after the if statement: %d\n", test_var);
}
/*End local block for function main () * /
上例产生如下输出结果:
Test variable before the if statement: 10
Test variable within the if statement: 5
Test variable within the independent local block:0
Test variable after the if statement: 10
注意:在这个例子中,每次test_var被定义时,它都要优先于前面所定义的test_var变量。此外还要注意,当if语句的局部程序块结束时,程序重新进入最初定义的test_var变量的作用范围,此时test_var的值为10。
转自 http://c.biancheng.net/cpp/html/1145.html
0 0
- 什么是C语言局部程序块(local block)
- C语言程序块
- 快速块匹配的非局部均值去噪算法_Fast Block Matching Non local means
- Objective-C语言——Block块
- 053.Local Variable 代码块局部变量
- 053.Local Variable 代码块局部变量
- block(程序块)
- 什么是线程局部变量(Thread Local Variable)
- Objective-C block (块)
- 局部变量与代码块(block)
- 什么是CSS包含块(Containing Block)
- 什么是c语言程序的模块设计
- Objective-C block(代码块)
- Object-c: Block (方法块)
- object-c块(block)
- Objective-C - 代码块block
- 【Objective-C】代码块(Block)
- Objective-c的block(块)
- JAVA获取json中的全部键值对
- React Native 配置遇到的错误:TransformError
- 无法自动生成R.java中的内部类代码
- 横向ListView及新闻头条
- 可编程DMA控制器——Intel 8257介绍
- 什么是C语言局部程序块(local block)
- Android中使用GSON包解析JSON数据(简单JSON)
- 在Ubuntu上为Android系统内置C可执行程序测试Linux内核驱动程序
- 一位毕业生的肺腑之言: 大学生活应该怎样度过——写给学弟学妹
- automake示例
- Nginx源码分析 - 基础数据结构篇 - 数组结构 ngx_array.c
- android适配器SimpleCursorAdapter的使用以及最后一个参数的作用
- C# 异步
- MYSQL存储过程怎么写