sheriff Idowu
sheriff Idowu

Reputation: 21

How do I do arithmetic operations between a character and a digit in C without using the ascii code

I want to add the character ‘4’ and the digit 3 and have it equal 7 without any library methods if possible I.e ‘4’ + 3 = 7

Upvotes: 2

Views: 151

Answers (1)

Fractal
Fractal

Reputation: 814

For all single digits you can do like:

char c = '4';

// If you want to add 3 now you can do like:
int result = c - '0' + 3;

Subtracting '0' is the key here. That's it

Upvotes: 11

Related Questions