首页 > 科技 > Specifiers and Function

Specifiers and Function

2007年8月27日 21点23分 发表评论 阅读评论

If they limit the scope of their variable to their pieces of code, they do not have to worry about conflicting with variales of the same name used by ohters in other parts of the program. In C, you can declare a variable and indicate its visiblity level by designing its scope. Thus, variables with local scope can only be accessed with the block in which they are declared.

1.Block scope
In this section, a block refers to any sets of statements enclosed in braces. A variable declared within block has block scope. Thus, the variable is active and accessiable from the declaration point to the end of the block. Sometimes, block scope is also called local scope. Usually, a variable with block scope is called a local variable.

2.Nested block size
If a variable declared in the outer block shares the same name with one of the variables in the inner block, the variable within the outer block is hidden by the one within the inner block for the scope of the inner block.

3.Program scope
A variable is said to have program scope when it is declared outside a function. Here variable with program scope and also called global variables, which are visiable among different files.

Since a global variable is visible among different source file of a program, using global variable increases your program’s complexity, which in turn makes your program hard to maintain or debug. It’s not recommended that you declare and are global variables unless it’s very necessary. For instance, you can declare a global variable whose value is used but never changed by several subroutines in your program(use #define directive to define constants that are used in many places in a program).

4.File scope
The program file that contains the source code.

Auto specifier:
A variable’s reserved space in the memory can be erased or relocated when the variable is out of its scope.

Static specifier:
Permanent duration, in other words, the memory storage allocated for the variabl is exited, the value of the variable is maintained outside the scope, and if execution even returns to the scope of the variable, the last stored in the variable is still there.

The register specifer:
In CPU, it’s much quicker to access a register than a memory location. Therebefore, storing variables in register may help to speed up your program. The C language provides you with the register specifer. You can apply this specifer to variables when you think it’s necessary to put the variable into the computer registers. However, the register specifier only gives the compiler a suggestion. In other words, a variable specifierd by the register keyword is not guaranteed to be stored in a register. The compiler can ignore the suggestion if there is no register available, if some other restrictions have to apply.

It is illegal to take the address of a variable that is declared with register specifier because the variable is intended to be stored in a register, not in memory.

Extern specifer:
How can a global variable declare in file A, for instance, be seen in file B? In other words, how does the compiler know the variable used in file B is actually the same variable in file A?
The answer is : Use the extern specifier provided by the C language to allude to a global variable defined elsewhere. In this case, we declare a global variable in file A, and then declare the variable again using the exterm specifier in file B.

To make your program portable across different computer platforms, you can apply the following rules in your program when you declare or allude to a global variables:
1. Ignore the extern specififer, but include an initialize when you declare a global variable.
2. Use extern specifier without an initializer.

Storage class modifiers, used to indicate to C compiler how variable many be accessed.
1.The const modifier:
The content of the variable can not be changed after it is initialized. eg:
char const *ptr_str = “hello”;
2.Volatile modifer, content can be changed without any explicite assignment statement. Ask compiler to turn off certain optimizations on a variables, can declare with valatile specifier.

A function can be declared to return any data type, except an array or function. The return statement used in a function definition returns a single value whose type should match the one declared in the function declaration. By default, the return type of a function is int, if no explicit data is specifier for the function.

syntax:
data-type-specifier function-name()
Here data-type-specifer indicates the data type that the function should return.

Function prototype(with ANSI standard, the number and types fo arguments passed to a function are allowed to be added into the function declaration). The number and types of an argument are called the function prototype.

The purpose of using a function prototype is to help the compiler check whether the data types of arguments passwd to a function match what function expects. The compiler issues an error message if the data types do not match.

“void” is used in the declaration to indicate to the compiler that no argument is needed by this function.

time() return calendar time(current data and time).
locatetime(), local time converted from calendar time
asctime: convert the date and time represented by the structure tm.

vg_start, vg_arg, vg_end.
vg_arg: use to get the next argument passed to the function.

分类: 科技 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
您必须在 登录 后才能发布评论.