当前位置:网站首页>[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 !
边栏推荐
- 【C语言系列】— 不创造第三个变量,实现两个数的交换
- 实现简单的数据库查询(不完整)
- Clickhouse learning (x) monitoring operation indicators
- 弹性盒子相关知识
- shell基本操作(上)
- The function of using wechat applet to scan code to log in to the PC web of the system
- [C language series] - detailed explanation of file operation (Part 1)
- 用sql-client.sh生成的job在cancle过后 如何实现断点续传?
- 用threejs 技术做游戏跑酷
- 相对定位和绝对定位
猜你喜欢
Longest string without duplicate characters
【C语言系列】— 字符串+部分转义字符详解+注释小技巧
Clickhouse learning (VIII) materialized view
表格与表单相关知识点总结
Alibaba cloud Zhang Xintao: heterogeneous computing provides surging power for the digital economy
Day 5
Pyqt5: Chapter 1, Section 1: creating a user interface using QT components - Introduction
浅谈范式
Occt learning 001 - Introduction
Clickhouse learning (V) cluster operation
随机推荐
组件传参与生命周期
[C language series] - storage of deep anatomical data in memory (I) opening of summer vacation
Clickhouse learning (VII) table query optimization
Detailed explanation of exit interrupt
B - 识别浮点常量问题
用threejs 技术做游戏跑酷
【C语言系列】—深度解剖数据在内存中的存储(一) 暑假开篇
ClickHouse学习(五)集群操作
link与@import导入外部样式的区别
ClickHouse学习(十一)clickhouseAPI操作
rem与px与em异同点
【C语言系列】— 把同学弄糊涂的 “常量” 与 “变量”
Selenium实战案例之爬取js加密数据
Bubble sort c language
数组学习之入门简单题 两数之和
Three handshakes and four waves for the interview summary
C language n queen problem
Storage category
Alibaba cloud Zhang Xintao: heterogeneous computing provides surging power for the digital economy
Day 5