当前位置:网站首页>[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 !
边栏推荐
- 365 day challenge leetcode 1000 questions - day 039 full binary tree inserter + find peak II + snapshot array
- Day 2
- link与@import导入外部样式的区别
- Selenium实战案例之爬取js加密数据
- Time complexity and space complexity
- Cmu15-213 malloc lab experiment record
- 携手数字人、数字空间、XR平台,阿里云与伙伴共同建设“新视界”
- Summary of the first week
- ClickHouse学习(九)clickhouse整合mysql
- 数据库操作 Day 6
猜你喜欢
随机推荐
【C语言系列】—文件操作详解(上)
Application of Huffman tree and Huffman coding in file compression
【TypeScript】深入学习TypeScript函数
Preemptive appointment | Alibaba cloud shadowless cloud application online conference appointment opens
Global components component registration
ClickHouse学习(一)ClickHouse?
ClickHouse学习(九)clickhouse整合mysql
【C语言系列】—深度解剖数据在内存中的存储(一) 暑假开篇
C language file operation
基础爬虫实战案例之获取游戏商品数据
全局components组件注册
冒泡排序 C语言
Differences between texture2d and texture2dproj under webgl1.0
AR虚拟增强与现实
用sql-client.sh生成的job在cancle过后 如何实现断点续传?
C language n queen problem
浅谈Servlet
数据库操作 Day 6
[event preview] cloud digital factory and digital transformation and innovation forum for small and medium-sized enterprises
第一周总结









