In C language, x (prefix auto-increment) first increments the variable value and then assigns the value, x (suffix auto-increment) first assigns the value and then increments the variable value; the expression value of the former is x 1 and the latter is x.
The difference between x and x in C language
In C language, x and x are both suffixes since Increment operator, but they perform slightly differently.
x (prefix auto-increment)
- This operator increments the value of variable x by 1 and then assigns the result to x.
- The value of expression x is x 1.
- After execution, the value of variable x is increased by 1.
x (suffix auto-increment)
- This operator increases the value of variable x by 1, but it first assigns the original value of x Gives the result of the expression.
- The value of expression x is x.
- After execution, the value of variable x also increases by 1.
Example:
1 2 3 4 5 6 7 8 9 10 11 | <code class = "c" >int main() {
int x = 5;
printf( "x before ++x: %d\n" , x);
++x;
printf( "x after ++x: %d\n" , x);
printf( "\nx before x++: %d\n" , x);
x++;
printf( "x after x++: %d\n" , x);
}</code>
|
Copy after login
Summary:
- x increases the variable value first and then assigns it, while x is assigned first and then the variable value is incremented.
- The expression for x evaluates to x 1, and the expression for x evaluates to x.
The above is the detailed content of The difference between ++x and x++ in c language. For more information, please follow other related articles on the PHP Chinese website!