当前位置:网站首页>[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 !
边栏推荐
猜你喜欢
eggjs 创建应用知识点
【C语言系列】—深度解剖数据在内存中的存储(一) 暑假开篇
Detailed explanation of serial port communication
Day 3
ClickHouse学习(十)监控运行指标
Realize simple database query (incomplete)
[C language series] - three methods to simulate the implementation of strlen library functions
MySQL解压版windows安装
【C语言系列】—三种方法模拟实现strlen库函数的方法
Alibaba cloud Zhang Xintao: heterogeneous computing provides surging power for the digital economy
随机推荐
Differences between texture2d and texture2dproj under webgl1.0
shell基本操作(下)
uniapp组件之倒计时(如阅读协议倒计时、完成学习倒计时)
Selenium实战案例之爬取js加密数据
Introduction to array learning simple question sum of two numbers
Realize simple database query (incomplete)
Application of Huffman tree and Huffman coding in file compression
VIM editor use
Pointer
第三课threejs全景预览房间案例
Day 5
Li Kou 994: rotten orange (BFS)
【C语言系列】—文件操作详解(上)
Cryengine Technology
Best practices for elastic computing in the game industry
【TypeScript】深入学习TypeScript函数
微信小程序-组件传参,状态管理
ClickHouse学习(五)集群操作
【剑指offer】— 详解库函数atoi以及模拟实现atoi函数
C language file operation