函数作用域与函数原型作用域,代码块作用域有什么区别?

PGKGP 2011-07-13 04:49:01
???
...全文
664 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
RacheliOS 2014-03-19
  • 打赏
  • 举报
回复
函数原型作用域,作用在函数在声明时候,那么到底是什么作用在函数声明时候,形参?如果是形参,那么既然有作用域,就说明系统在函数声明时候给该形参分配了空间,这样理解是正确的么?
FrankHB1989 2011-07-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 frankhb1989 的回复:]

引用 4 楼 anyidan 的回复:

作用域又分为生命域(其存在的时间范围) 和 可见域 (可以看到的时间范围)

扯蛋吧。scope和lifetime一定能对应?或者又生造名词?
C语言的作用域这个概念比较糊,本身只是表示identifier的声明位置的属性。
无论是C/C++,函数作用域只是对于label而言的;函数原型作用域只对于函数原型声明的形式参数有意义;其它变量声明之……
[/Quote]
没说清楚,“其它变量”是指局部变量。。。排除file scope/namespce scope之类。。。= =
至善者善之敌 2011-07-13
  • 打赏
  • 举报
回复
帮顶吧,概念性的东西,找扁文章看看就可以了
FrankHB1989 2011-07-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 anyidan 的回复:]

作用域又分为生命域(其存在的时间范围) 和 可见域 (可以看到的时间范围)
[/Quote]
扯蛋吧。scope和lifetime一定能对应?或者又生造名词?
C语言的作用域这个概念比较糊,本身只是表示identifier的声明位置的属性。
无论是C/C++,函数作用域只是对于label而言的;函数原型作用域只对于函数原型声明的形式参数有意义;其它变量声明之类都在块作用域(C++称为局部作用域)。
AnYidan 2011-07-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 snowwhite1 的回复:]

变量在其作用域内起作用。
函数作用域就是变量的作用范围限制在一个函数内部,一般包括函数的形参以及其内声明的局部变量。
函数原型作用域,这种说法比较少,可能特指函数单独进行声明的情况。
代码块作用域,指的是用一对大括号括起来的一个区域内声明的变量;其实函数实际上就是一个特殊的代码块作用域。
变量只在区域内起作用。并且小的区域变量会覆盖外表更大区域的同名变量。
[/Quote]

函数原型作用域 -- 函数声明时在那一句中的范围

变量只在区域内起作用。并且小的区域变量会覆盖 (屏蔽外面)外表更大区域的同名变量。

作用域又分为生命域(其存在的时间范围) 和 可见域 (可以看到的时间范围)
W170532934 2011-07-13
  • 打赏
  • 举报
回复
C++ primer上有解释!
zsc_ericluo 2011-07-13
  • 打赏
  • 举报
回复
新手,长见识了。
flysnowhite 2011-07-13
  • 打赏
  • 举报
回复
变量在其作用域内起作用。
函数作用域就是变量的作用范围限制在一个函数内部,一般包括函数的形参以及其内声明的局部变量。
函数原型作用域,这种说法比较少,可能特指函数单独进行声明的情况。
代码块作用域,指的是用一对大括号括起来的一个区域内声明的变量;其实函数实际上就是一个特殊的代码块作用域。
变量只在区域内起作用。并且小的区域变量会覆盖外表更大区域的同名变量。
zhouganghao 2011-07-13
  • 打赏
  • 举报
回复
我是来学习的
FrankHB1989 2011-07-13
  • 打赏
  • 举报
回复
补上:关于什么是块(block):
ISO C99:
6.8.2 Compound statement
Syntax
1 compound-statement:
{ block-item-list opt }
block-item-list:
block-item
block-item-list block-item
block-item:
declaration
statement
Semantics
2 A compound statement is a block.
FrankHB1989 2011-07-13
  • 打赏
  • 举报
回复
无语……搞不清函数作用域和块作用域的,估计要么偷懒没学,要么遇到烂书了。
ISO C99 6.2.1:
2 For each different entity that an identifier designates, the identifier is visible (i.e., can be used) only within a region of program text called its scope. Different entities designated by the same identifier either have different scopes, or are in different name spaces. There are four kinds of scopes: function, file, block, and function prototype. (A function prototype is a declaration of a function that declares the types of its parameters.)
3 A label name is the only kind of identifier that has function scope. It can be used (in a
goto statement) anywhere in the function in which it appears, and is declared implicitly
by its syntactic appearance (followed by a : and a statement).
4 Every other identifier has scope determined by the placement of its declaration (in a
declarator or type specifier). If the declarator or type specifier that declares the identifier appears outside of any block or list of parameters, the identifier has file scope, which terminates at the end of the translation unit. If the declarator or type specifier that declares the identifier appears inside a block or within the list of parameter declarations in a function definition, the identifier has block scope, which terminates at the end of the associated block. If the declarator or type specifier that declares the identifier appears within the list of parameter declarations in a function prototype (not part of a function definition), the identifier has function prototype scope, which terminates at the end of the function declarator. If an identifier designates two different entities in the same name space, the scopes might overlap. If so, the scope of one entity (the inner scope) will be a strict subset of the scope of the other entity (the outer scope). Within the inner scope, the identifier designates the entity declared in the inner scope; the entity declared in the outer scope is hidden (and not visible) within the inner scope.
金刚葫芦娃 2011-07-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 benben2301 的回复:]
C/C++ code

void fun()
{//函数作用域开始
int i;
for (i = 0; i < 100; i++)
{//代码作用域开始
int d = 10;
printf("%d", i);
//代码作用域结束;
}
//函数作用域结束
}
[/Quote]

+++
  • 打赏
  • 举报
回复

void fun()
{//函数作用域开始
int i;
for (i = 0; i < 100; i++)
{//代码作用域开始
int d = 10;
printf("%d", i);
//代码作用域结束;
}
//函数作用域结束
}

69,337

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧