当前位置:网站首页>Global variable vs local variable
Global variable vs local variable
2022-06-26 13:41:00 【Tom looks at programming】
1. Global variables
Global variables : Global variables can be referenced by all objects or functions in this program



extern usage :
The right solution : Use extern Key words to Statement Variables are external variables . Specifically, in one of them c In file Definition A global variable key, Then in the other to use key Of this variable c Use... In the document extern keyword Statement once , This indicates that this variable is an external variable , Is in the other c Global variables defined in the file . Please pay attention to the words I use here : Definition and Statement . For example, in main.c Variables defined in file key, stay common.c Declaration in the document key Variables are external variables , In this way, this variable can be shared between the two files key 了 , As shown in the figure below .
The code is as follows ( Write only the part related to the problem we are talking about ):
(1)main.c file
#include "common.h"
unsigned char key;
(2)common.c file :
#include "common.h"
extern unsigned char key;
Many people may be confused after reading it , Here's a little , In fact, that is Variable definitions and Variable declarations The difference between , Variable definitions Use “ data type + Variable name ” In the form of , The compiler needs to allocate memory units to it ; and Variable declarations Use “extern Variable type + Variable name ” In the form of , Is to tell the compiler that this variable will be external to others c The document defines , I only use it externally here . The compiler doesn't allocate memory space to it , Wait until you really encounter the variable definition, and then allocate memory space for it .
2. local variable
local variable : local variable , Also called internal variables , A variable defined inside a function or a compound statement . local variable The scope of is the function that defines the variable or the compound statement that defines the variable . local variable The lifetime of the function is calculated from the time when the function is called to the time when the function returns to the call

The local variables in the two functions do not affect each other , It's not relevant at all .
Citations :C Language extern effect ( Global variables ) - Creamy onion - Blog Garden (cnblogs.com)
边栏推荐
- MySQL数据库讲解(五)
- 【Proteus仿真】Arduino UNO按键启停 + PWM 调速控制直流电机转速
- Detailed sorting of HW blue team traceability process
- [how to connect the network] Chapter 2 (middle): sending a network packet
- Zero basics of C language lesson 7: break & continue
- imagecopymerge
- 输入文本自动生成图像,太好玩了!
- Beifu realizes the control of time slice size and quantity through CTU and ton
- Basic type of typescript
- PHP非对称加密算法(RSA)加密机制设计
猜你喜欢

Calculate the distance between two points (2D, 3D)

Es snapshot based data backup and restore

8.Ribbon负载均衡服务调用

【MySQL从入门到精通】【高级篇】(二)MySQL目录结构与表在文件系统中的表示

What features are added to Photoshop 2022 23.4.1? Do you know anything

MySQL explanation (II)

古瑞瓦特沖刺港交所上市:創下“多個第一”,獲IDG資本9億元投資

Lamp compilation and installation

Composite mode

Beifu twincat3 can read and write CSV and txt files
随机推荐
C language: Exercise 2
Detailed sorting of HW blue team traceability process
Create your own cross domain proxy server
ES中索引别名(alias)的到底有什么用
【系统分析师之路】第十五章 复盘数据库系统(数据库案例分析)
Jenkins build prompt error: eacces: permission denied
MongoDB系列之Window环境部署配置
Wechat applet - bind and prevent event bubble catch
基于PyTorch的生成对抗网络实战(7)——利用Pytorch搭建SGAN(Semi-Supervised GAN)生成手写数字并分类
Composite mode
Mysql database explanation (III)
KITTI Tracking dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
Uva5009 error curves three points
Electron official docs series: Examples
MySQL数据库讲解(五)
Stack, LIFO
MySQL数据库讲解(四)
Es common grammar I
Analysis of state transition diagram of Beifu NC axis
Beifu PLC realizes data power-off maintenance based on cx5130