+ (addition)

説明

加算は、四則演算の一つです。演算子 +(プラス)は、2 つのオペランドの合計を計算します。

構文

sum = operand1 + operand2;

sum:変数。許容されるデータ型:int、float、double、byte、short、long。
operand1: 変数または定数。許されるデータ型:int、float、double、byte、short、long。
オペランド2:変数または定数。可能なデータ型:int、float、double、byte、short、long。

プログラム例

int a = 5;
int b = 10;
int c = 0;
c = a + b;  // the variable 'c' gets a value of 15 after this statement is executed

注意点と警告

加算は、結果がデータ型に格納できる値よりも大きい場合、オーバーフローする可能性があります(例:値が32,767の整数に1を加えると、-32,768になります)。

オペランドが float 型または double 型の場合は、浮動小数点演算が行われます。

オペランドが float または double 型で、合計を格納する変数が整数の場合、積分部分のみが格納され、数値の小数部分は失われます。

float a = 5.5;
float b = 6.6;
int c = 0;
c = a + b;  // the variable 'c' stores a value of 12 only as opposed to the expected sum of 12.1
タイトルとURLをコピーしました