In C and C++, character arithmetic encompasses performing arithmetic operations with characters and symbols. Characters can be treated as numerical entities, despite their primary usage in representing text. This characteristic opens up interesting possibilities for manipulating characters through addition and subtraction operations. This guide will walk you through the utilization of characters in C and C++ for computational purposes, as well as shed light on the internal character handling mechanisms of these programming languages.
What is Character Arithmetic?
In C and C++, character arithmetic is a technique for performing mathematical computations on characters such as addition, subtraction, multiplication, and division. ASCII values are used to represent characters as numerical values for these operations. This approach is particularly useful when working with strings or individual characters in these programming languages. By treating characters as numerical entities, a wide range of text processing and computational tasks can be efficiently carried out.
Example: 1
Let's consider a scenario to demonstrate the process of character arithmetic in the C programming language.
#include <stdio.h>
int main()
{
char ch1 = 125, ch2 = 10;
ch1 = ch1 + ch2;
printf("%d\n", ch1);
printf("%c\n", ch1 - ch2 - 4);
return 0;
}
Output:
In C/C++, the placeholders %d and %c are employed for displaying integer and character values. It's essential to keep in mind that the %c specifier treats the given number as the ASCII code of a character. The ASCII codes representing letters, digits, special characters, and control characters for standard characters typically fall within the range of 0 to 127.
As a consequence, when providing an integer exceeding 127 with %c, it may not map to a recognizable ASCII character and the output may not match your intentions. This could lead to the display of a unique character, control code, or unpredictable behavior, varying based on the compiler and system in use.
- Therefore, it is crucial to verify that the integer value supplied with %c aligns with a valid ASCII character code falling between 0 and 127. This ensures reliable and expected results in your C and C++ applications.
Example: 2
Let's consider a scenario to demonstrate the character arithmetic operation in C++.
#include <bits/stdc++.h>
using namespace std;
int main()
{
char ch = 65;
cout << ch << endl;
cout << ch + 0 << endl;
cout << char(ch + 32) << endl;
return 0;
}
Output:
Example: 3
Let's consider another instance to demonstrate character arithmetic operations using ASCII values in C++.
#include <bits/stdc++.h>
using namespace std;
int main(){
char value1 = 'a';
char value2 = 'b';
char value3 = 'z';
char num1 = value1 + 3;
char num2 = value2 - 1;
char num3 = value3 + 2;
cout<<"numerical value = "<<(int)num1<<endl;
cout<<"numerical value = "<<(int)num2<<endl;
cout<<"numerical value = "<<(int)num3<<endl;
return 0;
}
Output:
Example: 4
#include <stdio.h>
int main() {
char a = 'A';
char b = 'B';
printf("a = %c\n", a);
printf("b = %c\n", b);
printf("a + b = %c\n", a + b);
return 0;
}
Output:
Explanation:
In this instance, the values "A" and "B" are assigned to the character variables "a" and "b" respectively. By employing character arithmetic, the software combines "a" and "b", resulting in the character "â". This phenomenon arises as characters are perceived as integers based on their ASCII encoding. For instance, the ASCII representations of "A" and "B" sum up to 131 (65 + 66), corresponding to the ASCII symbol "â". The printf method is utilized to display the outcome.