Constants and variables of C language elementary

insert image description here

What are constants and variables

During the execution of a C program, a quantity whose value does not change is called a constant, and a quantity whose value can change is called a variable. They can be classified in conjunction with data types. For example, it can be divided into integer constants, integer variables, floating point constants, floating point variables, character constants, character variables, enumeration constants, and enumeration variables. In a program, direct constants can be directly referenced without description, while symbolic constants and variables must be defined first and then used .
Explanation:
1. During program execution, the quantity whose value does not change is called constant .
2. A quantity whose value can be changed is called a variable . A variable should have a name and occupy a certain storage unit in memory. Variable definitions must come before variable usage. Usually placed at the beginning of the function body. To distinguish between variable names and variable values ​​are two different concepts.

constant

The definition forms of constants and variables in C language are different.
Constants in C language are divided into the following categories:
1. Literal constants
2. Constant variables modified by const
3. Identifier constants defined by #define
4. Enumeration constants

example

#include <stdio.h>
enum student
{
    
    
 NUM,
 NAME,
 SEX
};
//括号中的NUM,NAME,SEX是枚举常量
int main()
{
    
    
    //字面常量演示
    3.14;//字面常量
    1000;//字面常量
    
    //const 修饰的常变量
    const float pai = 3.14f;   //这里的pai是const修饰的常变量,是不能直接修改的!
    pai = 4.14;//这里就是个错误的赋值
    
    //#define的标识符常量 演示
 #define MAX 10000
    printf("max = %d\n", MAX);//可以理解为给10000换了个名字叫MAX
    
    //枚举常量演示
    printf("%d\n", NUM);//0
    printf("%d\n", NAME);//1
    printf("%d\n", SEX);//2
    //注:枚举常量的默认是从0开始,依次向下递增1的
    return 0;
}

How to define variables

int age = 22;
float weight = 57.5f;
char ch = 'a';

variable naming

1. Can only be composed of letters (including uppercase and lowercase), numbers and underscores ( _ ).
2. It cannot start with a number.
3. The length cannot exceed 63 characters.
4. Variable names are case-sensitive.
5. Variable names cannot use keywords.

Classification of variables

Divided into local variables and global variables

example

#include <stdio.h>
int a = 2023;//全局变量
int main()
{
    
    
    int a = 2024;//局部变量
    int b = 2022;//局部变量
    printf("a = %d\n", a);
    return 0;
}

The definition of the local variable a variable does not conflict with the definition of the global variable a variable.
When the local variable and the global variable have the same name, the local variable is used first,
so the output here should be 2024.

use of variables

For example, the following code demonstrates the use of variables very well:

#include <stdio.h>
int main()
{
    
    
    int num1 = 0;
   	int num2 = 0;
   	int sum = 0;
    printf("输入两个数进行相加:>");
    scanf("%d %d", &num1, &num2);
    sum = num1 + num2;
    printf("sum = %d\n", sum);
    return 0;
}

Input a new operand to modify the variable num1 and the variable num2, and the variable sum receives the added value of the modified two variables.

Variable scope and lifetime

scope

Scope is a programming concept. Generally speaking, a name used in a piece of program code is not always valid/available, and the code scope that limits the availability of this name is the scope of this name.

  1. The scope of a local variable is the local scope in which the variable resides.
  2. The scope of global variables is the entire project.

life cycle

Life cycle refers to a period of time between the creation of a variable and the destruction of the variable

  1. The life cycle of a local variable is: the life cycle of entering the scope begins, and the life cycle of the out of scope ends.
  2. The life cycle of global variables is: the life cycle of the entire program.

epilogue

I have been busy updating the data structure section before, and I haven’t written a C-level blog. I should update a lot of C-language blogs in the near future. I hope everyone will support it! ! !

Interested friends can pay attention to the author, if you think the content is good, please give a one-click triple link, you crab crab! ! !
It is not easy to make, please point out if there are any inaccuracies
Thank you for visiting, UUs watching is the motivation for me to persevere
. With the catalyst of time, let us all become better people! ! !
insert image description here

Guess you like

Origin blog.csdn.net/kingxzq/article/details/130523537