C Language Basics Tutorial (v) functions (8)

Source: Internet
Author: User
Tags printf variable scope

3. Function scope

Each function in the Turbo C2.0 is a separate block of code that is owned by the function code, and is not accessible to any statement in any other function except for a call to a function. For example, if you use a jump statement goto, you cannot jump from one function to another. Unless you use the whole variable, the program code and data defined inside a function do not interact with the program code and data in another function.
The scope of all functions in Turbo C2.0 is at the same nesting level, that is, you cannot describe or define another function within one function.
In Turbo C2.0, a function is invoked on the other child functions, that is, the function is in a different file, without having to append any descriptive statements to another function, which means that a function is visible to the entire program.
4. Variable scope of function
In Turbo C2.0, variables can be described at various levels of subroutines, that is, in any function, the variable indicates that it is allowed to be described only at the beginning of a function body, and that the description of the variable (including initialization) is followed by the left curly brace of a compound statement until the paired right curly brace. Its scope is only within this pair of curly braces, and it will no longer exist when the program executes to the curly braces. Of course, the variables in the inner layer are not related to each other even if they are the same as the variable names in the outer layers.
Example 9.
#include <stdio.h>
int i=10;
int main ()
{
int i=1;
printf ("%d\t, I");
{
int i=2;
PRITNF ("%d\t", I);
{
extern i;
I+=1;
printf ("%d\t", I);
}
printf ("%d\t", ++i);
}
printf ("%d\n", ++i);
return 0;
}
Run result is
1 2 11 3 2
The results from the program running are not difficult to see the relationship between the variables in the program, as well as the scope of each variable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.