Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of Local and Global scope of interpretation in C language

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "interpreting local and global scope case analysis in c language". The editor shows you the operation process through actual cases, and the operation method is simple, fast and practical. I hope this article "interpretation local and global scope case analysis in c language" can help you solve the problem.

What is the scope of a variable?

Before we move on to the scope of local and global variables, let's understand the meaning of scope.

Simply put, the scope of a variable is its life cycle in the program.

This means that the scope of a variable is a block of code in which the variable is declared, used, and can be modified throughout the program.

In the next section, you will learn about the local scope of variables

Local scope of variables in C-nested blocks

In this section, you will learn how local variables work in C. You will first write a few examples, and then summarize the scope principle.

▶, this is the first example:

# include int main () {int my_num = 7; {/ / add 10 my_num my_num = my_num + 10; / / or my_num + = 10-more succinctly printf ("my_num is% d", my_num);} return 0;}

Let's find out what the above program does.

In C, you separate blocks of code with {}. The left and right curly braces represent the beginning and end of the block, respectively.

The main () function has an integer variable my_num, which is initialized to 7 in the outer block.

An internal block attempts to add 10 to the variable my_num.

Now, compile and run the above program. This is the output:

/ / Outputmy_num is 17

You can see the following:

The inner block can access the value declared by my_num in the outer block and modify it by adding 7 to it.

The value of my_num is now 17, as shown in the output

Local scope of variables in C-nested block example 2

▶, this is another related example:

# include int main () {int my_num = 7; {int new_num = 10;} printf ("new_num is% d", new_num); / / this is line 9 return 0;}

In this program, the main () function my_num has an integer variable in the outer block.

The other variable, new_num, is initialized in the internal block. The inner block is nested within the outer block.

We try new_num to access and print the value of the inner block in the outer block.

If you try to compile the above code, you will notice that it did not compile successfully. You will receive the following error message:

Line Message9 error: 'new_num' undeclared (first use in this function) this is because the variable new_num is declared in the inner block and its scope is limited to the inner block. In other words, it is local to the internal block and cannot be accessed from the external block.

Based on the above observation, let's write down the general principles for the local scope of the following variables:

Local scope of variables in {/ * OUTER BLOCK*/ {/ / contents of the outer block just before the start of this block / / CAN be accessed here / * INNER BLOCK*/} / / contents of the inner block are NOT accessible here} C-different blocks

In the previous example, you learned how variables in nested internal blocks cannot be accessed from outside the block.

In this section, you will learn about the local scope of variables declared in different blocks.

# include int main () {int my_num = 7; printf ("% d", my_num); my_func (); return 0;} void my_func () {printf ("% d", my_num);}

In the above example

The integer variable my_num is declared inside the main () function.

Inside the main () function, the value printed by my_num.

There is another function, my_func (), that attempts to access and print the value of my_num.

When the program starts execution from the main () function, it calls main () inside the function my_func ().

▶ now compiles and runs the above program. You will receive the following error message:

Line Message13 error: 'my_num' undeclared (first use in this function)

If you notice on line 13, the function my_func () attempts to access the main () variable declared and initialized inside the my_num function.

Therefore, the scope of the variable my_num is limited to the main () function, which is called the function local main ().

We can generally express the concept of this local scope as follows:

Global scope of variables in {/ * BLOCK 1 variables / contents of BLOCK 2 cannot be accessed here} {/ * BLOCK 2 variables / contents of BLOCK 1 cannot be accessed here} C

So far, you have understood the local scope of the C variable. In this section, you will learn how to declare global variables in C.

▶, let's start with an example.

# include int my_num = 7 printf main () {printf ("my_num can be accessed from main () and its value is% d\ n", my_num); / / call my_func my_func (); return 0;} void my_func () {printf ("my_num can be accessed from my_func () as well and its value is% d\ n", my_num);}

In the above example

The variable my_num declares my_func () outside the functions main () and.

We try to access the function my_num internal main () and print its value.

We call the main () function inside the function my_func ().

The function my_func () also tries to access the value my_num and print it out.

The program compiles without any errors, and the output is as follows:

/ / Outputmy_num can be accessed from main () and its value is 7my_num can be accessed from my_func () as well and its value is 7

In this example, there are two functions-main () and my_func ().

However, the variable my_num is not in either of these two functions. This kind of variable which is not local to any function is called global scope, which is called global variable.

The principle of this global variable scope can be summarized as follows:

/ / all global variables are declared herefunction1 () {/ / all global variables can be accessed inside function1} function2 () {/ / all global variables can be accessed inside function2} about "interpreting local and global scope instance analysis in c language", thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report