C 語言中的字首自增(或字首自減)


自增運算子用於將值增加 1,而自減運算子的作用與自增相反。自減運算子將值減少 1。

以下是 C 語言中字首自增運算子的語法:

++variable_name;

以下是 C 語言中字首自減運算子的語法:

--variable_name;

我們來看看字首自增和字首自減運算子之間的區別。

字首自增 - 在將值賦給變數前,值加 1。

以下是 C 語言中字首自增的一個示例:

示例

 即時演示

#include <stdio.h>
int main() {
   int i = 5;
   printf("The pre-incremented value : %d
",i);    while(++i < 10 )    printf("%d\t",i);    return 0; }

輸出

The pre-incremented value : 5
6789

字首自減 - 在將值賦給變數前,值減 1。

以下是 C 語言中字首自減的一個示例:

示例

 即時演示

#include <stdio.h>
int main() {
int i = 10;
   printf("The pre-decremented value : %d
",i);    while(--i > 5 )    printf("%d\t",i);    return 0; }

輸出

The pre-decremented value : 10
9876

更新於: 2020-06-24

2K+ 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.