Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use global variables in C language

Shulou Source: shulou.com Published: 2022-05-31 10:55:43 09月23日 Update

Most people do not understand the knowledge points of this article "how to use global variables in c language", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "how to use global variables in c language" article.

1. All variables defined outside the function are called global variables, and their default functional domains are valid from the location of the defined variable to the end of the source file.

2. If you need to use global variables, it is best to define them at the top of the file, so that all functions in the file can be used directly.

Example

# include void func1 () {x + = 10; y + = 20; printf ("function:% s x =% d y =% d\ n", _ _ FUNCTION__,x,y);} int x = 10 × int y = 20; void func2 () {x + = 10; y + = 20; printf ("function:% s x =% d y =% d\ n", _ _ FUNCTION__,x,y) } int main () {func1 (); func2 (); printf ("function:% s x =% d y =% d\ n", _ _ FUNCTION__,x,y); return 0;} / * output: main.cpp: In function 'void func1 ()': main.cpp:6:5: error:'x 'was not declared in this scope 6 | x + = 10 | | ^ main.cpp:7:2: error:'y' was not declared in this scope 7 | y + = 20; | ^ * / above is about the article "how to use global variables in c language". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.

Tags: Variables globals contents functions languages files articles knowledge articles valid value location functions most examples that is more best steps source files Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Redmi Shulou Tech Info vpn Shulou Information