当前位置:网站首页>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 .
 Insert picture description here
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 .
 Insert picture description here
 Insert picture description here
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 :
 Insert picture description here

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 .
     Insert picture description here  Insert picture description here
    The running result is :
     Insert picture description here
原网站

版权声明
本文为[Try!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101730495782.html