Variables and constants in C language

Knowledge point one: variables and constants

          1) What does not change and cannot change during the entire running process of the program is called a constant ;

          2) What can and may change during the running of the program is called a variable ;

Knowledge point 2: Declare variables

short s;
int n;
long l;
float f;
double d;

        1) Formula for declaring variables: type + identifier + semicolon

        2) Identifier: An identifier named by ourselves, used to represent the name of a variable, function or other entity . For example: in the above code, s, n, l, f, d are identifiers named by ourselves to represent a variable;

        3) Keywords: Vocabulary is specified in the language standard and has special meaning and use in the code . For example: short, int, long, float, and double all indicate the type of variables, and they are vocabulary provided by the language standard;

        4) Identifiers must be declared or defined before they can be recognized by the compiler, while keywords can be used directly ;

        5) Therefore, in order for the identifier to be regarded as a variable by the compiler, the identifier must be declared as a variable before use;

Example: a) Correct, variables are declared before use

#include <stdio.h>
int main()
{
int a;
printf("%d\n", a);    // 正确, 变量使用前被声明了。
return 0;
}

         b) Error, variable a is not declared

#include <stdio.h>
int main()
{
printf("%d\n", a);    // 错误, 变量未声明。
return 0;
}

        c) Error, variable a is used before declaration

#include <stdio.h>
int main()
{
printf("%d\n", a);    // 错误, 变量在声明前使用。
int a;
return 0;
}

Knowledge point 3: Variable name naming rules

          1) The variable name is an identifier, so the variable name must comply with the naming rules for identifiers;

          2) Identifier naming rules: Identifiers consist of uppercase and lowercase letters, numbers and underscores. The identifier cannot start with a number and must be different from an existing keyword ;

short apple;      //正确
int 88fruit;      //错误,不能以数字开头
long _pencil;     //正确,可以以下换线或字母开头
float love_you;   //正确,字母开头,标识符可以使用下划线
double int;       //错误,不能与现有关键词相同

Knowledge point 4: Initialization and assignment of variables

          1) Only through assignment can the third element of the variable (the value of the variable) be meaningful;

          2) Variable assignment requires the assignment operator "=";

          3) There are two ways to assign values ​​to variables: a variable is initialized immediately after it is declared. The second variable is declared first, and then the variable is assigned a value;

a) Initialize variables immediately after declaration

#include <stdio.h>
int main()
{
int a = 100; // 变量声明后,立即装入100。
printf("%d\n", a);
return 0;
}

b) After the variable is declared, it is not initialized. Subsequent assignment using the assignment operator

#include <stdio.h>
int main()
{
int a; // 变量声明后,为一个随机值
a = 100; // 这里我们使用赋值运算符,将100给了a。
printf("%d\n", a);
return 0;
}

Knowledge point 5: The difference between initialization and assignment

          1) Assignment:

a = 100;

The left side of the assignment operator is the declared variable, and the right side is the numerical value. This way of writing is assigning a value to a variable.

         2) Initialization:

int a = 100;

 The left side of the assignment operator is a variable declaration and the right side is a numerical value. This way of writing declares and initializes the variable.

        3) Variables can be assigned multiple times, but cannot be initialized multiple times. Multiple initialization is equivalent to declaring variable a twice. When declaring variable a for the second time, the compiler finds that identifier a has already been used, and will report a compilation error;

#include <stdio.h>
int main()
{
int a;
a = 100;
printf("%d\n", a);
a = 200;
printf("%d\n", a);
return 0;
}
正确;输出结果为100,200
#include <stdio.h>
int main()
{
int a;
a = 100;
printf("%d\n", a);
int a = 200;
printf("%d\n", a);
return 0;
}
错误:变量a被重复声明

Knowledge point 6: Constants

       1) A constant is a quantity in a program that is hard-coded in the code from the beginning and cannot be changed;

       2) Literal constants: For example: 100, 200, 1.3344, "HelloWorld", "I love motherland" are called literal constants.

                   a) Literal constants do not need to be declared, and the compiler can immediately determine its type by how it is written;

100 = 101;   //错误,常量不能被更改
100 = a;    //错误,常量不能被更改

                  b) What is wrapped in double quotes is called a string literal;

"HelloWorld"            //正确

"HelloWorld" = "你好";  //错误,常量不能被更改

      3) Literal constant type:

             a) The type of a variable is determined at the time of declaration. The type of a constant can also be determined when the constant is written in the code.

            b) Integer literal constants are usually of type int. Unless the value of the integer literal constant is too large and exceeds the range of the int type, the compiler will try to treat it as an unsigned int type. If it is larger, then in turn it is deduced to a larger range of integer types, such as long, unsigned long, longgned long long.

            c) Literal constants with decimals are of double type.

      4) Symbolic constants:

            a) Formula to define symbolic constants:

#define 符号常量 值

Example: We have the price of a product, which is currently 3 yuan. Now we need to calculate the price of 10 pieces of this product.

#include <stdio.h>
# define PRICE 3
int main()
{
int num = 10;
int total;
total = num * PRICE;
printf("total:%d", total);
return 0;
}

           We did not write num * 3 directly, but defined a symbolic constant PRICE. If one day, the price of this commodity changes, we define the price of the commodity as a symbolic constant, so that we only need to modify the value represented by this symbolic constant.

Guess you like

Origin blog.csdn.net/m0_73463638/article/details/126864373