术语定义 - 范围在C应用程序 [英] Terminology definition - Scope in C application

查看:158
本文介绍了术语定义 - 范围在C应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有以下类型C code的特定术语?在我的例子,我想增加范围的深度函数内code的一小块,而不必使用不必要的if / else / for语句,这样我可以用相同的名字多次重新声明一个变量。例如:

Is there a specific term for the following type of C code? In my example, I want to increase the depth of scope for a small chunk of code within a function without having to use needless if/else/for statements so that I can re-declare a variable with the same name multiple times. eg:

void myfunc(void) {
  int a = 0, b = 1;
  {
    int c;
    c = 3;
    printf("c is: %d", c);
  }

  a = 2;
  {
    int c = 5;
    printf("c is %d", c);
  }

}

什么是用于描述如何我刚刚结束在大括号一些code和增加范围深度,使该功能的其余部分没有看到'C'廉政术语?

What is the term used to describe how I just wrapped some code in curly braces and increased the scope depth so the rest of the function doesn't see the 'c' int?

谢谢!

推荐答案

范围定义为该对象是活动的区域。有五个范围在C.他们是如下:

Scope is defined as the area in which the object is active. There are five scopes in C. They are as follows.

程序作用域

这是在最上面的图层的声明。它们可高达一个节目的寿命。所有的功能都有此范围内。这也被称为全局范围。

These are the declarations at the top most layers. They are available up to the life of a program. All the functions have this scope. This is otherwise known as global scope.

文件范围

它具有范围,使得它可以从该点到该文件的末尾进行访问。

It has scope such that it may be accessed from that point to the end of the file.

void dummy(void) { }
// absence of static automatically gives program scope to `dummy()`

static void dummy(void) { } 
// static keyword here gives function `dummy()` a file scope 

功能范围

标签,只有有这个范围。在此范围内,它们是活性高达函数的末尾。

Only labels have this scope. In this scope, they are active up to end of the function.

void printFun()
{
print:
    printf("i is less than j");
}

int main()
{
    int i=1,j=2;
    if(i < j)
        goto print;
}

这code将由编译说,标签打印不清楚,因为标签只有函数范围被标记错误。如果您有职能部门之间无条件跳,你必须使用的setjmp / 的longjmp 功能。

This code will be flagged error by the compiler saying that the label print is not known because labels have only function scope. If you have to jump unconditionally between the functions, you have to use setjmp/longjmp functions.

屏蔽范围

声明是积极的到块的结尾(其中一个块中的定义报表{} )。所有函数内部的声明只有块范围。

Declarations that are active up to the end of the block (where a block is defined as statements within { }). All the declarations inside the function have only block scope.

int fun(int a, int b)
{
    int c; 
    {   
        int d;
    }
    // a, b, c, d all have block scope
}

正如我刚才所说,功能范围仅适用于标签。所以,不应该与块范围相混淆。就好像他们在与其他变量块的开头声明的函数参数处理(记住,函数体也被认为是在块{} )。因此,函数的参数有块范围(而不是函数范围内)。

As I have said, function scope applies only to labels. So should not be confused with block scope. The function arguments are treated as if they were declared at the beginning of the block with other variables (remember that the function body is also considered as a block within { }). So the function arguments have block scope (not function scope).

本地范围一般用法是指要么是功能范围或块作用域。

Local scope is general usage to refer the scope that is either function or block scope.

原型范围

他们只具有原型声明中的范围。因为变量名称仅在原型的声明是有效的,并且不与其他变量名称冲突这个范围是有趣。它的存在少用很少的时间和等被忽视。

They are having the scope only inside the prototype declaration. This scope is interesting because the variable names are valid only in the declaration of prototype and does not conflict with other variable names. It exists for very little time and of less use and so goes unnoticed.

int add(int a, float b);

下面的变量 A B 据说有原型范围。

Here the variables a and b are said to have prototype scope.

选择尽可能小的范围

当一个名称已被解决,该名称搜索在尽可能小的范围,并且如果不可用,则搜索在更高水平的范围。所以,如果一个变量都将被声明,你必须选择尽可能小的范围成为可能。如果你可以限制你的范围中,增加程序的效率,可读性和可维护性。如果你想要一个变量,它不是块外面有用,声明它的块中,而不是在外部的。同样,如果你想有一个变量的值会在函数中唯一的访问,但有保留函数调用之间的值,选择静态变量来全球性的。

When a name has to be resolved, that name is searched in the minimal scope, and if that is not available, it is searched at higher levels of scope. So, if a variable has to be declared, you have to select a minimal scope possible. If you can limit your scope, that increases the efficiency, readability and maintainability of your program. If you want a variable which is not useful outside a block, declare it inside the block and not in the outer ones. Similarly, if you want a variable whose value will be accessed only within the function but has to retain the value between the function calls, opt for static variable to a global one.

这篇关于术语定义 - 范围在C应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆