In this C++ tutorial, you will learn how to use the arithmetic addition operator + with integers, floating-point values, characters, and Boolean values. You will also learn how to add user input, add numbers using a function, chain multiple additions, and understand the difference between +, +=, and ++.
C++ Addition Operator +
In C++, addition is performed using the arithmetic operator +. It takes two operands and produces their sum.
result = operand1 + operand2;
The operands can be variables, numeric literals, or expressions. When the operands have different arithmetic types, C++ applies its usual arithmetic conversions before performing the addition. This is why an expression containing an int and a float, for example, can produce a floating-point result.
1. Addition of Integers in C++
You can add two integer values using the addition operator. When both operands are int values, the addition expression also has integer type in this example.
int = int + int
In the following program, we initialize two integer variables and add them using addition operator.
C++ Program
#include <iostream>
using namespace std;
int main() {
int a = 12;
int b = 7;
int sum = a + b;
cout << sum << endl;
}
Output
19
The expression a + b evaluates to 12 + 7, so the value 19 is stored in sum.
2. Addition of Floating-Point Numbers in C++
The + operator also adds floating-point values. In this example, both operands and the variable receiving the result are of type float.
float = float + float
In the following program, we initialize two floating point variables and add them using addition operator.
C++ Program
#include <iostream>
using namespace std;
int main() {
float a = 1.2;
float b = 0.6;
float sum = a + b;
cout << sum << endl;
}
Output
1.8
3. Addition of an Integer and Floating-Point Number
C++ can add operands of different arithmetic types. When an int and a float participate in this addition, the integer value is converted as required by the usual arithmetic conversions so that the addition can be performed using a common type.
float = int + float
In the following program, we initialize an integer variable and a floating point variable and add them using addition operator.
C++ Program
#include <iostream>
using namespace std;
int main() {
int a = 1;
float b = 2.6;
float sum = a + b;
cout << sum << endl;
}
Output
3.6
Here, the mathematical result is 3.6. Storing it in a floating-point variable preserves the fractional part.
4. Addition of char Values in C++
A char is an integral type in C++. When two char operands are used with arithmetic +, integer promotions normally take place and the addition itself is performed using an integer type. If the result is then assigned to a char, it is converted back to char.
char = char + char
In the following program, we initialize two character variables and add them using addition operator.
C++ Program
#include <iostream>
using namespace std;
int main() {
char ch1 = 12;
char ch2 = 56;
char ch = ch1 + ch2;
cout << ch << endl;
}
Output
D
The numeric values 12 and 56 add to 68. On an ASCII-compatible system, character code 68 represents D, which explains the shown output. If your actual goal is to display the numeric sum of character values rather than interpret the result as a character, store the result in an int.
5. Addition of Boolean Values in C++
bool values can participate in arithmetic expressions. During arithmetic addition, false is promoted to 0 and true to 1. The result of the addition is an integer value before any assignment back to bool.
bool = bool + bool
When the arithmetic result is assigned to a bool, zero becomes false and any nonzero value becomes true. Therefore, the assignment shown in this example produces a Boolean result, even though the + expression itself is arithmetic rather than a logical OR operation.
In the following program, we initialize two boolean variables and add them using addition operator.
C++ Program
#include <iostream>
using namespace std;
int main() {
bool a = true;
bool b = false;
bool c = a + b;
cout << c << endl;
}
Output
1
Here, true + false first evaluates numerically as 1 + 0. The value 1 is then assigned to c as true, and the default stream representation of that Boolean value is 1.
Add Two Numbers in C++ Using User Input
Instead of assigning fixed values in the source code, you can read two numbers from the user with cin, add them, and print the result with cout.
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two integers: ";
cin >> a >> b;
int sum = a + b;
cout << "Sum = " << sum << endl;
return 0;
}
If the user enters 15 and 8, the program stores those values in a and b, evaluates a + b, and stores 23 in sum.
Enter two integers: 15 8
Sum = 23
C++ Program for Addition of Two Numbers Using a Function
The addition can also be placed inside a function. This is useful when the same operation needs to be called from different parts of a program.
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(20, 7);
cout << result << endl;
return 0;
}
Output
27
The values 20 and 7 are passed to add(). The function returns their sum, which is then stored in result.
Chaining the C++ Addition Operator for Multiple Values
You can chain Addition Operator and perform the addition of more than two operands in a single statement. The sudo code is given below.
result = operand_1 + operand_2 + operand_3 + ... + operand_n
In the following example, we shall take four integer variables and add them altogether in a single statement using arithmetic addition operator.
C++ Program
#include <iostream>
using namespace std;
int main() {
int a = 12;
int b = 25;
int c = 61;
int d = 37;
int sum = a + b + c + d;
cout << sum << endl;
}
Output
135
The expression adds the four values from left to right according to the associativity of the arithmetic + operator. For these integer operands, the final result is 135.
Difference Between +, +=, and ++ in C++
The operators +, +=, and ++ are related to addition, but they serve different purposes.
a + bcalculates a sum without modifyingaorb.a += baddsbtoaand stores the new value back ina.++aora++incrementsaby exactly one.
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = a + 5;
a += 5;
a++;
cout << b << endl;
cout << a << endl;
return 0;
}
Output
15
16
Initially, a is 10. The expression a + 5 produces 15 without changing a. Then a += 5 changes a to 15, and a++ changes it to 16. Therefore, += is not the same as ++: += can add an arbitrary value, whereas ++ adds one.
C++ Addition Type Conversion Considerations
The type of an addition expression matters when operands have different types or when the result is assigned to another type. A few practical cases are worth checking:
- Adding two integers performs integer arithmetic for ordinary
intoperands. - Combining integer and floating-point operands can convert the integer operand to a floating-point type before addition.
charandbooloperands undergo integral promotions in arithmetic expressions.- Assigning a floating-point result to an integer discards the fractional part through conversion.
- Adding sufficiently large integer values can exceed the range of the chosen integer type, so the data type should match the expected range of values.
C++ Addition Editorial Review Checklist
- Verify that each
+example distinguishes the type of the operands from the type of the variable receiving the result. - Check mixed integer and floating-point examples for the expected arithmetic conversions.
- Do not describe arithmetic addition of Boolean operands as the logical OR operator; explain the integer promotion and subsequent conversion to
boolwhen applicable. - For
charaddition, distinguish numeric character values from string concatenation and note when the result is converted back tochar. - Confirm that examples of
+=explain that the left operand is modified, while ordinary+does not modify its operands. - Confirm that
++is described as an increment by one rather than as another spelling of+=. - Test user-input addition examples with both positive and negative integer values.
C++ Addition Operator Summary
In this C++ Tutorial, we learned how to use C++ Addition Operator on values of different datatypes, etc.
The arithmetic + operator can add integer and floating-point values directly and can also operate on integral types such as char and bool after the applicable promotions. We also covered adding values from user input, placing addition in a function, chaining several operands, and distinguishing ordinary addition from += and ++.
TutorialKart.com