How to assign values in C language?
In the C language, variables can be assigned values using the assignment operator (=). This operator assigns the value on the right side of the equal sign to the variable on the left side.
For example, to assign the integer variable x a value of 10, you can use the following code:
int x;
x = 10;
To assign the floating point variable y a value of 3.14, you can use the following code:
float y;
y = 3.14;
It is also possible to assign the result of an expression to a variable. For example, assigning the result of adding two integers to a variable z.
int a = 5;
int b = 3;
int z;
z = a + b;
It is important to note that in C language, assignment works from right to left. This means that the value on the right side of the equal sign is calculated first, and then assigned to the variable on the left side of the equal sign. Therefore, in an assignment expression, the expression on the right side of the equal sign is evaluated first. For example:
int a = 5;
int b = 3;
int c = a + b; // 先计算a + b的结果,然后将结果赋给c
Assignment operators can also be used in conjunction with other operators, such as +=, -=, *=, and /=. These operators perform the respective operation first, and then assign the result to the variable. For example, to increase the value of variable x by 5, you can use the += operator.
int x = 10;
x += 5; // 相当于x = x + 5
In this way, the value of x will become 15.