char math?

M

mdh

Can someone enlighten me?

let "s" be a string array and "i, n" be an integers.

Why does the expression:

s= n % 10 + '0' put the "string value" of the modulo in the array.

I thought by placing the 0 in a single quote, you are telling the
compiler to treat 0 as a character.
I fail to see how "n % 10" added to that produces the correct result,
which it does!
Thanks in advance.

Ref to this is page 64 of K&R(2);
 
R

Richard Heathfield

mdh said:
Why does the expression:

s= n % 10 + '0' put the "string value" of the modulo in the array.


Because of 5.2.1(3) which says, in part, "...the value of each character
after 0 in the above list of decimal digits shall be one greater than the
previous".

(It means '0', by the way.)

So - 0 + '0' is '0', obviously. But the above text means that 1 + '0' is
guaranteed to be '1', 2 + '0' is guaranteed to be '2', etc.
 
M

mdh

Richard said:
mdh said:
Because of 5.2.1(3) which says, in part, "...the value of each character
after 0 in the above list of decimal digits shall be one greater than the
previous".

(It means '0', by the way.)

So - 0 + '0' is '0', obviously. But the above text means that 1 + '0' is
guaranteed to be '1', 2 + '0' is guaranteed to be '2', etc.

OK...thanks...
I was trying to make a much bigger deal out of it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top