next up previous contents
Next: 關係運算子 Up: 運算子 Previous: 一元運算子   Contents

算術運算子

  1. 算術運算子 (Arithmetic Operators) 說明
    符號 意義 範例 說明
    + a+6 計算 a + 6
    - a-6 計算 a - 6
    * a*6 計算 a * 6
    / a/6 計算 a / 6
    % 取餘數 a%6 計算 a / 6 的餘數
    ++ 遞增 a++, ++a 計算 a 增加 1
    -- 遞減 a--, --a 計算 a 減少 1
  2. 實例說明:
    [dywang@dyw219 zzz]$ vim op-ari1.c
    [dywang@dyw219 zzz]$ cat op-ari1.c
    #include <stdio.h>
    
    main() {
        int a, c=5;
    	a = c + 3;
        printf("a = c + 3 = %d\n", a);
    	a = c - 3;
        printf("a = c - 3 = %d\n", a);
    	a = c * 3;
        printf("a = c * 3 = %d\n", a);
    	a = c / 3;
        printf("a = c / 3 = %d\n", a);
    	a = c % 3;
        printf("a = c % 3 = %d\n", a);
    	a = c++;
        printf("a = c++ = %d, c = %d\n", a, c);
    	a = ++c;
        printf("a = ++c = %d, c = %d\n", a, c);
    	a = c--;
        printf("a = c-- = %d, c = %d\n", a, c);
    	a = --c;
        printf("a = --c = %d, c = %d\n", a, c);
    }
    
  3. 編譯成功
    [dywang@dyw219 zzz]$ gcc -o op-ari1 op-ari1.c
    
  4. 執行結果
    [dywang@dyw219 zzz]$ ./op-ari1
    a = c + 3 = 8
    a = c - 3 = 2
    a = c * 3 = 15
    a = c / 3 = 1
    a = c % 3 = 2
    a = c++ = 5, c = 6
    a = ++c = 7, c = 7
    a = c-- = 7, c = 6
    a = --c = 5, c = 5
    



De-Yu Wang 2019-09-19