当前位置:网站首页>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 :
边栏推荐
- Brute force method / task assignment
- Kcon 2022 topic public selection is hot! Don't miss the topic of "favorite"
- Leetcode advanced road - 136 A number that appears only once
- 异步、线程池(CompletableFuture)
- Leetcode advanced path - reverse string
- Leetcode advanced path - the first unique character in a string
- 面试必备——synchronized底层原理的基础知识
- 堆排序与加强堆代码 用于记忆
- Connexion MySQL errorcode 1129, State hy000, Host 'xxx' is Blocked because of many Connection Errors
- Read the source code of micropyton - add the C extension class module (3)
猜你喜欢

LeetCode:497. Random points in non overlapping rectangles -- medium

【北大青鸟昌平校区】职教与普教协调发展,今年的中考会容易吗?

初中毕业生,选择中职学校也可以升入高等学府

数据库系统概论 ---- 第一章 -- 绪论(重要知识点)

Redis cache avalanche

CET-6 - Business English - the last recitation before the test

Lengsuanling, a 30-year tortuous history of IPO of a domestic brand

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

Introduction to database system -- Chapter 1 -- Introduction (important knowledge points)

软件测试工程师是做什么的?
随机推荐
Power consumption development experience sharing: design power consumption board
LeetCode 进阶之路 - 删除排序数组中的重复项
防抖和节流
Brute force method /k integers out of 1~n integers
Pytorch deep learning -- convolution operation and code examples
Lengsuanling, a 30-year tortuous history of IPO of a domestic brand
蛮力法/任务分配
Brute force method / task assignment
牛客网:数组中出现次数超过一半的数字
Redis缓存击穿
Quick start to elastic job, three minutes to experience distributed scheduled tasks
72. editing distance ●●
Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
Unity analyzes the rendering of built-in terrain and does some interesting things
Read the source code of micropyton - add the C extension class module (4)
Redis set password command (temporary password)
北大青鸟昌平校区:高中学历可以学UI吗?
【生成对抗网络学习 其一】经典GAN与其存在的问题和相关改进
在手机上买基金安全吗?会不会被吞本金?