Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use static global static variables in C language

Shulou Source: shulou.com Published: 2022-06-03 09:15:47 09月29日 Update

This article mainly introduces how to use static global static variables in c language, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.

1. The static global variable is initialized only once.

2. In a single .h or .c file, ordinary global variables are the same as static-decorated global static variables.

3. The variables modified by static are stored in the static data area, and all bytes in memory default to 0x00.

Example

# include int globalNum = 100 value_static; / global variable int gAddr_for_static = 0 value_static; ram / used to save the static variable address in the function void test_static_temp () {value_static; = 10 value_static; / local static variable printf ("value_static;\ n\ n", & global); modify any global variable through the pointer! Static variable! void SetGlobalValue (int address,int v) {int * pt; pt= address; * pt= v;} int main (void) {test_static_temp (); / / address of static variable obtained from output int addr = & globalNum; printf ("initialize global variable globelNum=%d, memory address =% x\ n\ n", globalNum,&globalNum); SetGlobalValue (addr,1234) Printf ("globelNum=%d after function call, memory address =% x\ n\ n", globalNum,&globalNum); addr = gAddr_for_static;// each compiler gets a different SetGlobalValue (addr,4567); test_static_temp (); return 0 } Thank you for reading this article carefully. I hope the article "how to use static global static variables in c language" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Variables global static address article memory language function different ordinary value interest single simultaneous byte instance local pointer data file Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Apple Microsoft macOS Xiaomi