Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use global variables in C language

Shulou Source: shulou.com Published: 2022-06-03 08:59:29 11月15日 Update

Editor to share with you how to use global variables in the c language. I hope you will get something after reading this article. Let's discuss it together.

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; | ^ * / after reading this article, I believe you have some understanding of "how to use global variables in c language". If you want to know more about it, welcome to follow the industry information channel. Thank you for reading!

Tags: Variables functions global language file article valid location function done instance more best source file knowledge industry information information channel top channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Linux Shulou Information Shulou Tech Info OPPO Reno