Character Arithmetic in C++
Last Updated :
09 Feb, 2024
Character arithmetic in C++ involves performing mathematical operations on characters. In C++, characters are represented using the char data type, which is essentially an integer type that stores ASCII values. In this article, we will discuss character arithmetic and how to perform character arithmetic in C++.
What is Character Arithmetic in C++?
Character arithmetic involves performing arithmetic operations on characters based on their ASCII values. So when some operations are performed on the characters, they are automatically promoted to integers. This process is called integer promotion. Due to this, we can perform some common mathematical operations on the character type variables.
Following are some common character arithmetic operations:
- Increment and Decrement: The increment (++) operator increments the value, while the decrement (--) operator decrements it by one.
- Addition and Subtraction: We can add or subtract integers to/from characters, and the result will be a new character based on the modified ASCII value.
- Multiplication and Division: While multiplication and division may not have intuitive meanings when applied to characters, they still operate based on ASCII values. This operation is less commonly used due to less relevance.
Note: While doing character arithmetic, keep in mind the size of the character type is 1 byte so it can only store the values from 0 to 255.
Example of Character Arithmetic in C++
The below example demonstrates the common arithmetic operations on characters.
C++
// C++ program to demonstrate charcater arithmetic
#include <iostream>
using namespace std;
int main()
{
char charA = 'A';
// Increment
cout << "Original character: " << charA << endl;
charA++;
cout << "After increment (++): " << charA << endl;
// Decrement
charA--;
cout << "After decrement (--): " << charA << endl;
// Addition
// Moving 3 characters forward from 'A'
char resultAdd = charA + 3;
cout << "After adding 3: " << resultAdd << endl;
// Subtraction
// Moving 2 characters backward from 'D'
char resultSub = resultAdd - 2;
cout << "After subtracting 2: " << resultSub << endl;
return 0;
}
OutputOriginal character: A
After increment (++): B
After decrement (--): A
After adding 3: D
After subtracting 2: B
Conclusion
Character arithmetic in C++ provides a way to manipulate characters through their ASCII values. This knowledge is particularly useful in scenarios where characters need to be transformed, encrypted, or manipulated in various ways.
Similar Reads
Character Arithmetic in C As already known character range is between -128 to 127 or 0 to 255. This point has to be kept in mind while doing character arithmetic. What is Character Arithmetic?Character arithmetic is used to implement arithmetic operations like addition, subtraction, multiplication, and division on characters
2 min read
What is a Character (CHAR)? In progrаmmiÕ¸g, dаtа is stored аոd mаnupulаted ÑÕ¸ vаrious forms, kÕ¸owÕ¸ аs dаtа types. OÕ¸e fuÕ¸dаmeÕ¸tаl dаtа type is the chаrаcter, ofteÕ¸ly аbbreviаted аs chаr. UÕ¸derstаոdiÕ¸g chаrаcters аոd their use ÑÕ¸ progrаmmiÕ¸g is crucÑаl for hаոdliÕ¸g text, symbols, аոd more complex dаtа structures effectively. Wh
3 min read
Character Classification in C++ : cctype Character classification in C++ is possible using functions specified in function library. These functions are included in the <cctype> header file. Numerous functions to classify characters are discussed below:1. isalpha(): This function returns true if the character is an alphabet else retur
7 min read
getline() Function and Character Array in C++ The C++ getline() is a standard library function that is used to read a string or a line from an input stream. It is a part of the <string> header. The getline() function extracts characters from the input stream and appends it to the string object until the delimiting character is encountered
2 min read
Data type of character constants in C and C++ In C, data type of character constants is int, but in C++, data type of same is char. If we save below program as test.c then we get 4 as output (assuming size of integer is 4 bytes) and if we save the same program as test.cpp then we get 1(assuming size of char is 1 byte) C++ // C++ program demonst
1 min read