Open in App
Not now

# Character arithmetic in C and C++

• Difficulty Level : Easy
• Last Updated : 20 Mar, 2023

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 ,division on characters in C and C++ language.
In character arithmetic character converts into integer value to perform task. For this ASCII value is used.
It is used to perform action the strings.

To understand better let’s take an example.

## C

 `// C program to demonstrate character arithmetic.` `#include `   `int` `main()` `{` `    ``char` `ch1 = 125, ch2 = 10;` `    ``ch1 = ch1 + ch2;` `    ``printf``(``"%d\n"``, ch1);` `    ``printf``(``"%c\n"``, ch1 - ch2 - 4);` `    ``return` `0;` `}`

## C++

 `#include ` `using` `namespace` `std;`   `int` `main()` `{` `    ``char` `ch1 = 125, ch2 = 10;` `    ``ch1 = ch1 + ch2;` `    ``cout << ``static_cast``<``int``>(ch1) << endl;` `    ``cout << ``static_cast``<``char``>(ch1 - ch2 - 4) << endl;` `    ``return` `0;` `}`

Output

```-121
y```

So %d specifier causes an integer value to be printed and %c specifier causes a character value to printed. But care has to taken that while using %c specifier the integer value should not exceed 127.
So far so good.
But for c++ it plays out a little different.

Look at this example to understand better.

## C++

 `// A C++ program to demonstrate character` `// arithmetic in C++.` `#include ` `using` `namespace` `std;`   `int` `main()` `{` `    ``char` `ch = 65;` `    ``cout << ch << endl;` `    ``cout << ch + 0 << endl;` `    ``cout << ``char``(ch + 32) << endl;` `    ``return` `0;` `}`

Output:

```A
65
a```

Without a ‘+’ operator character value is printed. But when used along with ‘+’ operator behaved differently. Use of ‘+’ operator implicitly typecasts it to an ‘int’. So to conclude, in character arithmetic, typecasting of char variable to ‘char’ is explicit and to ‘int’ it is implicit.

let’s take one more example.

## C++

 `#include ` `using` `namespace` `std;`   `// driver code` `int` `main(){` `    ``char` `value1 = ``'a'``;` `    ``char` `value2 = ``'b'``;` `    ``char` `value3 = ``'z'``;` `  `  `    ``// perform character arithmetic` `    ``char` `num1 = value1 + 3;` `    ``char` `num2 = value2 - 1;` `    ``char` `num3 = value3 + 2;` `  `  `    ``// print value` `      ``cout<<``"numerical value = "``<<(``int``)num1<

## C

 `#include ` `// driver code` `int` `main(``void``)` `{` `    ``char` `value1 = ``'a'``;` `    ``char` `value2 = ``'b'``;` `    ``char` `value3 = ``'z'``;` `    ``// perform character arithmetic` `    ``char` `num1 = value1 + 3;` `    ``char` `num2 = value2 - 1;` `    ``char` `num3 = value3 + 2;` `    ``// print value` `    ``printf``(``"numerical value=%d\n"``, num1);` `    ``printf``(``"numerical value=%d\n"``, num2);` `    ``printf``(``"numerical value=%d\n"``, num3);` `    ``return` `0;` `}`

Output:

```numerical value=100
numerical value=97
numerical value=124```

## C

 `#include `   `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;` `}`

```a = A
b = B
a + b = â```

#### Explanation

• In this program, two character variables a and b are declared and assigned the values ‘A’ and ‘B’, respectively. The program then adds a and b using character arithmetic, which results ‘â’. The result is then printed using the printf() function.
• Note that in character arithmetic, the characters are treated as integers based on their ASCII code values. For example, the ASCII code for ‘A’ is 65 and for ‘B’ is 66, so adding ‘A’ and ‘B’ results in 65 + 66 = 131, which is the ASCII code for ‘â’.

My Personal Notes arrow_drop_up
Related Articles