Character arithmetic in C

C++ProgrammingServer Side Programming

Character arithmetic 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.

Here is an example of character arithmetic in C language,

Example

 Live Demo

#include <stdio.h>

int main(){
   char s = 'm';
   char t = 'z' - 'y';

   printf("%d\n", s);
   printf("%c\n", s);
   printf("%d\n", (s+1));
   printf("%c\n", (s+1));
   printf("%d\n", (s-1));
   printf("%c\n", (s-1));
   printf("%d\n", t);
   // printf("%c", t);

   return 0;
}

Output

Here is the output −

109
m
110
n
108
l
1
raja
Published on 23-Oct-2018 16:11:06
Advertisements