+ (addition)
Description
Addition is one of the four primary arithmetic operations. The operator
+
(plus) operates on two operands to produce the sum.Syntax
sum = operand1 + operand2;
Parameters
: variable. Allowed data types:sum
,int
,float
,double
,byte
,short
.long
: variable or constant. Allowed data types:operand1
,int
,float
,double
,byte
,short
.long
: variable or constant. Allowed data types:operand2
,int
,float
,double
,byte
,short
.long
Example Code
1int a = 5;2 int b = 10;3 int c = 0;4 c = a + b; // the variable 'c' gets a value of 15 after this statement is executed
Notes and Warnings
- The addition operation can overflow if the result is larger than that which can be stored in the data type (e.g. adding 1 to an integer with the value 32,767 gives -32,768).
- If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the calculation.
- If the operands are of float / double data type and the variable that stores the sum is an integer, then only the integral part is stored and the fractional part of the number is lost.
1float a = 5.5;2 float b = 6.6;3 int c = 0;4 c = a + b; // the variable 'c' stores a value of 12 only as opposed to the expected sum of 12.1
See also
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.