当前位置:网站首页>[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 !
边栏推荐
猜你喜欢
随机推荐
[event preview] cloud digital factory and digital transformation and innovation forum for small and medium-sized enterprises
On Paradigm
The function of using wechat applet to scan code to log in to the PC web of the system
题解:在一个排序数组中查找元素第一个和最后一个的位置 (个人笔记)
第三课threejs全景预览房间案例
【剑指offer】— 详解库函数atoi以及模拟实现atoi函数
【C语言系列】— 不创造第三个变量,实现两个数的交换
Best practices for elastic computing in the game industry
Global components component registration
抽象类与接口
ANSI C type qualifier
ClickHouse学习(三)表引擎
Basic use of redis
【C语言系列】—三种方法模拟实现strlen库函数的方法
ClickHouse学习(二)ClickHouse单机安装
Talking about Servlet
牛客网编程题—【WY22 Fibonacci数列】和【替换空格】详解
Day 1
阿里云架构师细说游戏行业九大趋势
公众号不支持markdown格式文件编写怎么办?









