character conversion

M

mdh

May I ask the learned board this. Is it correct to assume that a
character (in this case "c") is always converted to an integer, so
that line 2 below ( specifically c - '0' ) will always give the
value as an integer of it's digit representation? (Snippet from page
97 K&R). If the question does not make too much sense, it is because I
am missing something. :)


f(int *ptr){...

int c;

....

for ( *ptr = 0; isdigit(c); c = getch() ) /* getch gets the next
character from the input */
*ptr = *ptr * 10 + (c - '0'); <----Line 2

....}
 
W

Walter Roberson

May I ask the learned board this. Is it correct to assume that a
character (in this case "c") is always converted to an integer, so
that line 2 below ( specifically c - '0' ) will always give the
value as an integer of it's digit representation?

In C, characters are one of the integral types, so individual
characters are -already- an integer, rather than being "converted"
to an integer. But the integer value associated with a character
is the -encoding- of the character. For example, in the fairly
common ASCII character set, '6' is encoded as the number 54.

If the variable c is the encoding of a character, then
c - '0' is the difference between the encoding of that character and the
encoding of the character for the digit '0'. C guarantees that the
encoding of the digits '0', '1' up to '9' will be consequative and
increasing order, so if c is the encoding of a digit, c - '0'
will be the numerical difference between the encodings of the two
digits (the original and '0'), which by definition in C will work
out to the numeric value that the digit would be read as in base 10
notation.
 
B

Ben Bacarisse

mdh said:
May I ask the learned board this. Is it correct to assume that a
character (in this case "c") is always converted to an integer, so
that line 2 below ( specifically c - '0' ) will always give the
value as an integer of it's digit representation?

In your case, c is already an int and character constants like '0'
are also ints. When getchar() returns a character, it does so as an
int, so you wording a little odd, but the answer is that the code:
int c;
...
for ( *ptr = 0; isdigit(c); c = getch() ) /* getch gets the next
character from the input */
*ptr = *ptr * 10 + (c - '0'); <----Line 2

is fine. If isdigit(c) is true, c - '0' will evaluate to 0 when c ==
'0', to 1 when c == '1' and so on up to 9 when c == '9'. Is that
answer enough?
 
M

mdh

In C, characters are one of the integral types, so individual
characters are -already- an integer, rather than being "converted"
.......

Thanks...much cleaner thinking for me that way.
.
If the variable c is the encoding of a character, ....... which by definition in C will work
out to the numeric value that the digit would be read as in base 10 notation.

OK...thank you Walter.
Michael.
 
K

Keith Thompson

Richard Heathfield said:
Ben Bacarisse said:

To be excruciatingly pedantic, c /would/ be an int if he'd written it as
'c'. What he actually wrote - "c" - is a you-must-not-modify-this array of
two char with static storage duration.

To be slightly less excruciatingly pedantic but slightly more
excruciatingly correct, the code fragment being discussed, and quoted
in the article to which you responded, included the following
declaration:

int c;

The quotation marks in
(in this case "c")
were ordinary natural-language punctuation, not C string literal
delimiters.

(This is why I usually use ``this form'' when I want to quote C code,
unless I can use "quotation marks" without ambiguity.)

Let me think, what's the phrase? Oh, yes. Nyaah nyaah. Terribly
sorry, but you left me with no other choice.
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top