当前位置:网站首页>[C language series] - constants and variables that confuse students
[C language series] - constants and variables that confuse students
2022-07-29 05:35:00 【Gancheng なつき】
꧁ Hello, everybody ! It is a great honor to have your visit , Let's have a long way to go in programming !꧂
* Blog column :【C All living things 】*
Introduction to this article : Detailed introduction “ Constant ” And “ Variable ”!
Get to know the author : Aspire to be a great programmer , Xiaobai, who is currently a sophomore in College .
Inspirational terms : The tediousness of programming , Let's study together and become interesting !
Text begins
List of articles
Priority of local variables and global variables
The scope and life cycle of variables
*const Modified constant variable *
*#define Defined identifier constant *
Preface
Constant in life : I think some values in life are constant ( such as : Everyone's blood type , Id card number , PI in mathematics and so on ) These cannot be changed
Variable : Some values can be changed ( such as : Age , Weight, etc ) 
And in the C In language , Constant values are used Constant The probability of , It's worth it Variable To express .
Variable
local variable
Definition of local variable : In a specific process ( For example, in the code block ) Or a quantity locally defined in the function !
First of all, let's write a code to understand the appearance of local variables ——————>>>
#include<stdio.h>
void test()
{
int x = 0;// local variable
int y = 0;// local variable
}
int main()
{
int a = 10;// local variable
int b = 20;// local variable
test();
printf("%d\n", a);
return 0;
}Like the code above , Inside the main function , In custom functions , The quantities defined are local variables !
Global variables
Definition of global variables : Global variables are also called External variables , Is a variable defined outside the function , It can also be created anywhere in the program !
Let's also recognize it through a small piece of code ———————>>>
#include<stdio.h>
int global = 20;// Global variables
int main()
{
return 0;
}It's not like this , Inside the main function , Quantities that are not in custom functions are local variables !
Priority of local variables and global variables
Let's think about it , When a program , When two local variables and global variables with the same name are defined , So who is the priority when using ? Who will become the chosen son ?
Look at this code ——————>>>
#include<stdio.h>
int global = 300;
int main()
{
int global = 200;
printf("global=%d\n", global);
return 0;
}Let's guess who will get the qualification of printout ?----> Without nonsense, practice directly to know , Just print it

Take a look , It's a local variable !
summary : When a local variable has the same name as a global variable , Local variables take precedence

The scope and life cycle of variables
Scope
The concept of scope , Namely , The names used in a program are not always valid / Usable , The scope of the code that defines the usability of the name is the scope of the name !( Generally speaking, the usable scope of the thing you write is the scope )
1. The scope of a local variable is the local scope of the variable
2. The scope of global variables is the entire project you create
Life cycle
The concept of variable life cycle : The life cycle of a variable is a period of time between the creation of a variable and its destruction
Let's take a piece of code to analyze ——————>>>

1. The life cycle of a local variable : Enter the scope lifecycle begins , Out of scope life cycle ends
2. The life cycle of global variables : The whole life cycle of the program
Constant
C Language constants are divided into 4 Kind of , I'll come next One Introduce to you
* Literal constants *
Literal constants are like numbers , The amount of letters
// Demonstration of literal constants
3.14;// decimal
1000;// Integers
'w'// Number of characters These are literal constants !
*const Modified constant variable *
Constant variable ? What the hell , I don't understand constants and variables , How about another constant variable ! ha-ha , Ok ok , A constant variable is itself a constant , But it also has the property of variables, which is called constant variables
Look at this code to verify ——————>>>
#include<stdio.h>
int main()
{
float a = 3.14;
a = 5.20;
printf("%f\n", a);
return 0;
}Quantities like this can be modified ; But add const After that ?

Here we can see the following a Want to be modified , Just give a warning , Description plus const Then it can't be modified ! Have the property of a constant .
What are the properties of variables ? Look at this code ——————>>>

*#define Defined identifier constant *
First hand code demonstration !
#include<stdio.h>
#define MAX 100 // Macro defines a MAX
int main()
{
int a = MAX;
printf("a=%d\n", a);
return 0;
}Look at the printed a value :

Is the value defined by the macro !
* Enumeration type *
Many things in our life are ok One One List out , such as : Color , Gender and so on , Then we need to define containers to contain them , You have enumeration variables --- for instance
#include<stdio.h>
enum colour
{
RED,
GREEN,
BLUE
};
int main()
{
printf("%d\n", RED);
printf("%d\n", GREEN);
printf("%d\n", BLUE);
return 0;
}The result of printing can be seen , The default value of enumeration type is from 0 At the beginning , Add down in turn 1

Conclusion
OK! Here we end the explanation of constants and variables , You will be ‘ Scrap ’ Yes !
边栏推荐
- Clickhouse learning (V) cluster operation
- Alibaba cloud architect details nine trends in the game industry
- One dimensional array exercise
- uniapp组件之tab选项卡滑动切换
- ·Let's introduce ourselves to the way of programming·
- Together with digital people, digital space and XR platform, Alibaba cloud and its partners jointly build a "new vision"
- Occt learning 002 - environment construction
- 第一周总结
- ClickHouse学习(五)集群操作
- 移动端-flex项目属性
猜你喜欢

【TypeScript】深入学习TypeScript函数

Together with digital people, digital space and XR platform, Alibaba cloud and its partners jointly build a "new vision"

无重复字符的最长字串

省市区三级联动(简单又完美)

Three handshakes and four waves for the interview summary

关于局部变量

科班同学真的了解未来的职业规划吗?

【剑指offer】— 详解库函数atoi以及模拟实现atoi函数

C language one-dimensional array

Day 2
随机推荐
D3d Shader Instruction
相对定位和绝对定位
B - identify floating point constant problems
【无标题】
ClickHouse学习(十)监控运行指标
Basic principle of recursion
TXT 纯文本操作
Solution: find the position of the first and last element in a sorted array (personal notes)
【C语言系列】— 字符串+部分转义字符详解+注释小技巧
实现简单的数据库查询(不完整)
利用Poi-tl在word模板表格单元格内一次插入多张图片和多行单元格相同数据自动合并的功能组件
uniapp之常用提示弹框
Wechat applet video upload component is directly uploaded to Alibaba cloud OSS
表格与表单相关知识点总结
Database operation day 6
Allocate memory: malloc() and free()
Summary of the first week
关于局部变量
科班同学真的了解未来的职业规划吗?
365 day challenge leetcode 1000 questions - day 041 two point search completion anniversary + nth magic number + online election