当前位置:网站首页>C language -- 3 variables for beginners
C language -- 3 variables for beginners
2022-06-10 21:29:00 【Try!】
1、 Constant 、 Variable
In the life , We can come across a variety of data , In these data , Some variable ( Such as age ), Some are immutable ( such as : Blood type 、 Gender, etc ). If you use C Language to describe , How to describe it ? In this case, constant is used ( Constant value ) And variables ( Values that can be changed ) 了 .
2、 How to define variables
Suppose you describe a person's information , How to describe it ?
#include<stdio.h>
int main()
{
int age = 20;
double weight = 55.5;
age = age + 5;
weight = weight - 10;
printf("%d\n",age);
printf("%lf\n",weight);
return 0;
}
The running result is :
25
45.500000
- After executing this procedure , The age obtained is 25, The weight is 45.500000, It is different from the value set at the beginning , Prove that these two variables are variable .
- stay
int age = 20;( The type of variable The name of the variable = The assignment of a variable ) in ,int It refers to the type of variable ,age It refers to the type of variable ,20 It refers to the assignment of variables . In principle , Or just write “ The type of variable ”+“ The name of the variable ”, Do not assign a value to it , But it's not recommended . - %d— integer ;%f—float type ;%lf—double type
3、 Classification of variables
Variables can be divided into Global variables ( In function body {} Externally defined ) and local variable ( In function body {} Internally defined ). When the names of local variables and global variables conflict , Local variables take precedence . However, it is not recommended to set local variables and global variables to the same name .
4、 Use of variables
Write a simple piece of code ( seek 2 The sum of integers ) To experience the use of variables .
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>;
int main()
{
int a = 0;
int b = 0;
int sum = 0;
scanf("%d %d",&a,&b);
sum = a + b;
printf("sum = %d",sum);
return 0;
}
The operation results are as follows :
2 6
sum = 8
Be careful : When this error message appears , Add this statement at the beginning of the code :#define _CRT_SECURE_NO_WARNINGS. Or you can scanf Change it to “scanf_s”scanf_s("%d %d",&a,&b);. however scanf_s The function is vs Compile provided , It is not C The language standard prescribes . If used in the code scanf_s function , So the code can only be in vs Run inside , Can't run on other platforms , This function is not recommended . If you want to use this function , Just study how to use this function . In order to make the code have good cross platform , Use to add... At the beginning of the code #define _CRT_SECURE_NO_WARNINGS Methods .
It is tedious to add this statement at the beginning of the code every time , How can I do it ?
find newc++ file.cpp, Write in the defined statement , Every time a new project is created , This sentence will be automatically added . If you directly open the file to add a statement, it will show that you do not have permission , Will Notepad “ Run as administrator ”, Then add the words to save it .

Stop “ Source file ”–“ add to ”—“ New item ”— Assume that the project name is test.c, Once I've created it , You can see the contents of the project :
5、 Scope and life cycle of variables
(1) Scope (scope)
This is the concept of programming , Generally speaking , The names used in a program are not always valid , The scope of the code that defines the usability of the name is the scope of the name .( Simply put, where can I use , Where is its scope )
- The scope of a local variable is the local scope of the variable
- The scope of global variables is the whole project
(2) Life cycle
The life cycle of a variable is the period between the creation of a variable and its destruction . - The life cycle of a local variable : Into the local range, life begins , Out of range, end of life
- The life cycle of global variables : Is the life cycle of the program ( A procedural main Function is the life cycle of the program )
Here's a point to make , Suppose a global variable has been declared in the project , This global variable is also used in another source file in the project , You need to declare in another source file that this global variable exists , Otherwise, it will report a mistake .

The running result is :
边栏推荐
猜你喜欢

Monitoring is easy to create a "quasi ecological" pattern and empower Xinchuang to "replace"

2021年平均工资出炉,IT行业不出所料

pytorch深度学习——卷积操作以及代码示例

Error code 1129, state HY000, host 'xxx' is blocked because of many connection errors

「运维有小邓」自助帐户解锁工具

Can you still have a wonderful life if you are laid off at the age of 35?

Practical | how to use burp suite for password blasting!

72. 编辑距离 ●●●

Kcon 2022 topic public selection is hot! Don't miss the topic of "favorite"

Understanding deep learning attention
随机推荐
详解MATLAB中与矩阵运算有关的算术运算符(加、减、乘、除、点乘、点除、乘方)
Redis cluster configuration
异步、线程池(CompletableFuture)
72. 编辑距离 ●●●
Obtained network time + time zone (+8)
Game compatibility test (general scheme)
Leetcode advanced road - plus one
软件测试工程师是做什么的?
Brute force method / adjacency table depth first directed weighted graph undirected weighted graph
LeetCode 进阶之路 - 反转字符串
在YUV图像上根据背景色实现OSD反色
Pytorch deep learning -- neural network convolution layer conv2d
Power set V4 recursion of brute force method /1~n
Asynchronous, thread pool (completablefuture)
shell实现ssh登录并执行命令
pytorch深度学习——神经网络卷积层Conv2d
用一个性能提升了666倍的小案例说明在TiDB中正确使用索引的重要性
H.264中NALU、RBSP、SODB的关系
Nodejs: official document 3 Dgram stream
保姆级教程:如何成为Apache Linkis文档贡献者