473,382 Members | 1,445 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,382 software developers and data experts.

K&R2 section 2.7 type conversions

this is the programme which converts a string of digits into its
numeric equivalent, given in section 2.7:

/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;

n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n + (s[i] - '0');
return n;
}
i ahve 2 questions:

Q 1: i want to know how this function will know that it has reached
the end of string ?
Q 1a. last element of an a string (array of char) is '\0' which is
zero so programme will proceed.
Q 1b. what if element next to the array is a number like 2, 4 or 8.
the for loop will not stop there.
i have put this programme to test:

----------------- PROGRAMME ------------------------------

/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;

n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n + (s[i] - '0');
return n;
}
int main(void) {

char str[] = "102";

atoi(str);
}

Q 2. this does NOT output anything at all, i expected /102/ as
output,as K&R2 said. what is wrong ?

Mar 13 '07 #1
7 1697
arnuld wrote:
this is the programme which converts a string of digits into its
numeric equivalent, given in section 2.7:

/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;

n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n + (s[i] - '0');
return n;
}
i ahve 2 questions:

Q 1: i want to know how this function will know that it has reached
the end of string ?
When a character < '0' or '9' is read.
>
Q 1a. last element of an a string (array of char) is '\0' which is
zero so programme will proceed.
It is zero, which is less than '0' for any character set I've seen.
Don't confuse the character '0' with the value 0.
Q 1b. what if element next to the array is a number like 2, 4 or 8.
the for loop will not stop there.
Please explain that a bit more.
>
i have put this programme to test:

----------------- PROGRAMME ------------------------------

/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;

n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n + (s[i] - '0');
return n;
}
int main(void) {

char str[] = "102";

atoi(str);
}

Q 2. this does NOT output anything at all, i expected /102/ as
output,as K&R2 said. what is wrong ?
Try printing something!

--
Ian Collins.
Mar 13 '07 #2
arnuld wrote:
this is the programme which converts a string of digits into its
numeric equivalent, given in section 2.7:

/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;

n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n + (s[i] - '0');
return n;
}
i ahve 2 questions:

Q 1: i want to know how this function will know that it has reached
the end of string ?
It doesn't. It knows when the next character is out of the range of
digits, '0'...'9'. If it reaches the end of the string before reaching
some other non-digit character, it will encounter the '\0' which is at
the end of every string, and that '\0' character is also out of the
range of digits.
Q 1a. last element of an a string (array of char) is '\0' which is
zero so programme will proceed.
The last element of a string is always '\0'. The last element of an
array of char need not be. And, no, the value of '0' (the digit zero)
and of '\0' (numeric zero) are not the same.
Q 1b. what if element next to the array is a number like 2, 4 or 8.
the for loop will not stop there.
It will, as I said, consume chars until it reaches a non-digit. Note
that not only does this function have a problem with a char array
without a '\0' (so not a string), but it has a problem with overflow
when a string of digits interpreted to have a value greater than INT_MAX
is passed to it.
>

i have put this programme to test:

----------------- PROGRAMME ------------------------------

/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;

n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n + (s[i] - '0');
return n;
}
int main(void) {

char str[] = "102";

atoi(str);
}

Q 2. this does NOT output anything at all, i expected /102/ as
output,as K&R2 said. what is wrong ?
You have no output statements. Why should you think that a program that
makes no attempt to output anything should, quite pervesely, output
something?

Mar 13 '07 #3
Ian Collins said:
arnuld wrote:
<snip>
>>
Q 1a. last element of an a string (array of char) is '\0' which is
zero so programme will proceed.

It is zero, which is less than '0' for any character set I've seen.
Yes. In fact, C *requires* that the integer value of '\0' is 0, and that
the integer value of '0' is positive.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 13 '07 #4
On Mar 13, 10:49 am, Martin Ambuhl <mamb...@earthlink.netwrote:

Q 1a. last element of an a string (array of char) is '\0' which is
zero so programme will proceed.

The last element of a string is always '\0'. The last element of an
array of char need not be. And, no, the value of '0' (the digit zero)
and of '\0' (numeric zero) are not the same.
ok, i got it now. '\0' has different integer value from '0'.
----------------- PROGRAMME ------------------------------
/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;
n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n + (s[i] - '0');
return n;
}
int main(void) {
char str[] = "102";
atoi(str);
}
Q 2. this does NOT output anything at all, i expected /102/ as
output,as K&R2 said. what is wrong ?

You have no output statements. Why should you think that a program that
makes no attempt to output anything should, quite pervesely, output
something?
i have an output statement: /return i/

why it does not put "i" on my Terminal ?

Mar 13 '07 #5
arnuld wrote:
>On Mar 13, 10:49 am, Martin Ambuhl <mamb...@earthlink.netwrote:
----------------- PROGRAMME ------------------------------
/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;
n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n + (s[i] - '0');
return n;
}
int main(void) {
char str[] = "102";
atoi(str);
}
Q 2. this does NOT output anything at all, i expected /102/ as
output,as K&R2 said. what is wrong ?

You have no output statements. Why should you think that a program that
makes no attempt to output anything should, quite pervesely, output
something?

i have an output statement: /return i/
(a) You have no `return i` statement.

(b) `return i` isn't an output statement. It's a return-this-value-as-
the-result-of-this-function statement.
why it does not put "i" on my Terminal ?
(fx:echo) You have no output statements.

--
Chris "electric hedgehog" Dollin
"A facility for quotation covers the absence of original thought." /Gaudy Night/

Mar 13 '07 #6
arnuld wrote:
this is the programme which converts a string of digits into its
numeric equivalent, given in section 2.7:

/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;

n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n + (s[i] - '0');
return n;
}
i ahve 2 questions:

Q 1: i want to know how this function will know that it has reached
the end of string ?
When the test condition of the for loop fails, i.e. when a character
with a value less than '0' or greater than '9' is read. That need not
correspond to the end of the string. For example in the following char
sequence:

987df'\0'

the loop will stop when it reads 'd' tests it against '0' and '9'. C
gives a guarantee that the character sequence '0', '1', ..., '9' will
occupy successively higher character code values. That guarantee is
the basis for this function, both in it's loop test condition and it's
actual condition.

If the function is fed:

gd677326'\0'

it'll return immediately.

On the other hand, if it's passed:

12345'\0'

it'll return when it encounters the end of the string.
Q 1a. last element of an a string (array of char) is '\0' which is
zero so programme will proceed.
No, the value of the character '0' is not 0. Similarly the value of
the character '9' is not 9. In fact, in C, the value zero is always
treated as the null character, so if the underlying system's character
set maps '0' to 0, then the C implementation will be forced to remap
the character set, but this state of affairs has, to my knowledge,
never happened, since no system seems to assign value zero to
character '0'.
Q 1b. what if element next to the array is a number like 2, 4 or 8.
the for loop will not stop there.
You're confused between a character and it's representation code.
i have put this programme to test:

----------------- PROGRAMME ------------------------------

/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;

n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n + (s[i] - '0');
return n;
}

int main(void) {
char str[] = "102";
atoi(str);
}

Q 2. this does NOT output anything at all, i expected /102/ as
output,as K&R2 said. what is wrong ?
Where're are you invoking an I/O function like printf to display the
value returned by atoi. You simply call atoi and discard it's return
value. Do:

int number;
number = atoi(str);
printf("%d\n", number);

Mar 13 '07 #7
arnuld wrote:
On Mar 13, 10:49 am, Martin Ambuhl <mamb...@earthlink.netwrote:
Q 1a. last element of an a string (array of char) is '\0' which is
zero so programme will proceed.
The last element of a string is always '\0'. The last element of an
array of char need not be. And, no, the value of '0' (the digit zero)
and of '\0' (numeric zero) are not the same.

ok, i got it now. '\0' has different integer value from '0'.
----------------- PROGRAMME ------------------------------
/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;
n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n + (s[i] - '0');
return n;
}
int main(void) {
char str[] = "102";
atoi(str);
}
Q 2. this does NOT output anything at all, i expected /102/ as
output,as K&R2 said. what is wrong ?
You have no output statements. Why should you think that a program that
makes no attempt to output anything should, quite pervesely, output
something?

i have an output statement: /return i/
Actually it's 'return n;', and it's not an output statement. What it
does is to return a value to the calling function, in an
implementation defined manner. A function that returns a value
resolves to it's return value in any expression. For example assume
the function fx always returns the value 5. So in the expression
below, the call to fx resolves to it's return value, i.e. 5.

int a = 5, b;
b = a + fx();

Where do you see any output. Input/Output, (I/O), is generally
considered to happen when data is received by the program from
external sources, (a keyboard, mouse, network interface etc.), or data
is sent from the program to it's external environment, (screen, disk,
network interface etc.).

The communication of data from one place in memory to another, (which
is what is happening in that return n statement), is not considered as
I/O. In any case, it's a transfer between and within the processor and
system memory, not to any I/O device, like the display device, so no
output is visible.

In C, to print output to the screen, (more precisely to the stdout
stream), use the functions printf, puts and putchar. To send data to
an arbitrary output stream, (i.e. other than stdout, example a disk
file), use one of fputc, putc, fputs, fprintf. Similarly to get input
from the stdin stream, use scanf, getchar. To get input from an
arbitrary input stream, use getc, fgetc, fgets, fscanf etc.

Mar 13 '07 #8

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

Similar topics

16
by: TTroy | last post by:
I FOUND MAJOR ERRORS in K&R2 (making it almost useless for the herein mentioned topics). K&R2 Section 5.9 Pointers vs. Multidimension Arrays starts of like this... "Newcomers to C are...
14
by: arnuld | last post by:
i have slightly modified the programme from section 1.5.1 which takes the input frm keyboard and then prints that to the terminal. it just does not run and i am unable to understand the error...
12
by: arnuld | last post by:
this is exercise 2-3 from the mentioned section. i have created a solution for it but i see errors and i tried to correct them but still they are there and mostly are out of my head: ...
4
by: arnuld | last post by:
as i said, i have restarted the book because i overlooked some material. i want to have some comments/views on this solution. it runs fine, BTW. ------------------ PROGRAMME -------------- /*...
16
by: arnuld | last post by:
i have created solution which compiles and runs without any error/ warning but it does not work. i am not able to understand why. i thought it is good to post my code here for correction before...
1
by: arnuld | last post by:
this is "word counting" example programme from K&R2 section 1.5.4. it runs without any error/warning but does not work properly, i.e. the OUTPUT 1 should be: NEWLINES: 1 WORDS: 1 CHARs: 5
0
by: Ioannis Vranos | last post by:
Although not about C++ only, I think many people here have K&R2, so I post this message in clc++ too. In K&R2 errata page <http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html>...
82
by: arnuld | last post by:
PURPOSE :: see statement in comments GOT: Segmentation Fault I guess the segfault is sourced in the compile-time warning but I am giving a char* to the function already.
8
by: arnuld | last post by:
This is the code form section 6.5 of K&R2: struct tnode { char *word; int count; struct tnode *left; struct tnode *right; };
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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.