
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Character arithmetic in C
In C, character arithmetic is defined by char data type. This is used to implement arithmetic operations like addition and subtraction on characters in C language. It is used to manipulate the strings. When the characters are used with the arithmetic operations, it converts them into integer value automatically i.e. ASCII value of characters.
Understanding Characters Represention
To understand the character represented using char data type then learn the ASCII value (integer) of each character. Some values are provided for the references such as,
'A' has an ASCII value of 65 'B' has an ASCII value of 66 'a' has an ASCII value of 97 'b' has an ASCII value of 98 ... ...
Character Arithmetic Operations in C
In C, we can perform the task of arithmetic operations that contain character manipulation using the ASCII values. As per the operating system, characters are internally stored as integers which can be possible using operations like addition (+) and subtraction (-).
Example
The following is the C program to show the usage of character arithmetic operations.
#include <stdio.h> int main() { char s = 'm'; // 'z' has an ASCII value of 122 and 'y' has an ASCII value of 121, so 't' will be 1 char t = 'z' - 'y'; // display the ASCII value of 's' printf("%d\n", s); // display the character representation of 's' printf("%c\n", s); // display the ASCII value of the character (addition) printf("%d\n", (s + 1)); // display the addition of one character after 's' printf("%c\n", (s + 1)); // display the subtraction of one character before 's' printf("%d\n", (s - 1)); // display the ASCII value of the character (subraction) printf("%c\n", (s + 1)); // display the value of 't', which is the result of 'z' - 'y' printf("%d\n", t); return 0; }
Output
The above program produces the following result:
109 m 110 n 108 n 1