473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

character conversion

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

....}
Jun 27 '08 #1
6 1318
mdh <md**@comcast.netwrites:
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?

--
Ben.
Jun 27 '08 #2
mdh
On Apr 21, 5:08*pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
>
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.

Jun 27 '08 #3
mdh
On Apr 21, 5:09*pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
In your case, c is already an int and character constants like '0'
are also ints. *........ *Is that answer enough?

Yes...it is. Thank you.

Jun 27 '08 #4
Ben Bacarisse said:
mdh <md**@comcast.netwrites:
>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
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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #5
Richard Heathfield <rj*@see.sig.invalidwrites:
Ben Bacarisse said:
>mdh <md**@comcast.netwrites:
>>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

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.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #6
Keith Thompson said:
Richard Heathfield <rj*@see.sig.invalidwrites:
>Ben Bacarisse said:
>>mdh <md**@comcast.netwrites:

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

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,
Ah, it seems that I took it out of context (unwittingly). My mistake.
Apologies.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: WindAndWaves | last post by:
Hi Folk Here I am writing my first php / mysql site, almost ready, and now this... charactersets.... The encoding that I use on my webpage is: <META HTTP-EQUIV="content-type"...
0
by: Tumurbaatar S. | last post by:
Hi, I'm running MySQL 3.23.55 on WinXP and have some problem using non latin charset. I've added these 2 lines under group of my.ini: character-sets-dir = c:/mysql/share/charsets/...
4
by: Raquel | last post by:
Could someone explain to me what the reason is for having a character delimiter (which is double quotes by default) for performing Loads/Imports on UDB? I would think that column delimiter along...
11
by: Mars | last post by:
char c; int i; scanf("%d",&i); c=getchar(); I want to read a integer and a character in 2 lines. Why the getchar always get the \n character from the last line????? (Sorry for my newbie...
5
by: Stefan Krah | last post by:
Hello, I am currently writing code where it is convenient to convert char to int . The conversion function relies on a character set with contiguous alphabets. int set_mesg(Key *key, char...
18
by: james | last post by:
Hi, I am loading a CSV file ( Comma Seperated Value) into a Richtext box. I have a routine that splits the data up when it hits the "," and then copies the results into a listbox. The data also...
23
by: Akhil | last post by:
Since a character constant is an int value represented as a character in single quotes,so it is treated as a 1 byte integer now look at the following snippet. #include<stdio.h> int main(void)...
17
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, Wide character and multi-byte character are two popular encoding schemes on Windows. And wide character is using unicode encoding scheme. But each time I feel confused when...
6
by: ssetz | last post by:
Hello, For work, I need to write a password filter. The problem is that my C+ + experience is only some practice in school, 10 years ago. I now develop in C# which is completely different to me....
3
by: Evan Klitzke | last post by:
Although it is not present in ANSI C, the GNU version of stftime supports the conversion character %z, which is a time offset from GMT. The four digit time offset is required in RFC 2822...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.