-

名称

-

説明

減算は四則演算の一つである。演算子-(マイナス)は、二つのオペランドの差を計算する。

書式

difference = operand1 - operand2;

引数

difference変数。利用できるデータ型: int, float, double, byte, short, long。
operand1変数か定数。利用できるデータ型: int, float, double, byte, short, long。
operand2変数か定数。利用できるデータ型: int, float, double, byte, short, long。

使用例

1
2
3
4
int a = 5;
int b = 10;
int c = 0;
c = a - b;  // the variable 'c' gets a value of -5 after this statement is executed

注意

  1. 減算演算子は、結果の型が格納可能な値よりも小さくなった時にオーバーフローする。 例えば、-32768から1を引くと、32767になる。
  2. オペランドの一つがfloatかdoubleの場合は、浮動小数点計算が使われる。
  3. オペランドがfloatかdoubleで、減算の結果を格納する変数が整数型の場合、結果の整数部分だけが格納され、小数点以下は捨てられる。
1
2
3
4
float a = 5.5;
float b = 6.6;
int c = 0;
c = a - b;  // the variable 'c' stores a value of -1 only as opposed to the expected difference of -1.1

参照

オリジナルのページ

https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/subtraction/

Last Revision: 2019/02/19

最終更新日

January 4, 2024

inserted by FC2 system