K&R2 section 2.7 type conversions

A

arnuld

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 >= '0' && s <= '9'; ++i)
n = 10 * n + (s - '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 >= '0' && s <= '9'; ++i)
n = 10 * n + (s - '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 ?
 
I

Ian Collins

arnuld said:
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 >= '0' && s <= '9'; ++i)
n = 10 * n + (s - '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 said:
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 >= '0' && s <= '9'; ++i)
n = 10 * n + (s - '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!
 
M

Martin Ambuhl

arnuld said:
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 >= '0' && s <= '9'; ++i)
n = 10 * n + (s - '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 >= '0' && s <= '9'; ++i)
n = 10 * n + (s - '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?
 
R

Richard Heathfield

Ian Collins said:
arnuld wrote:

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.
 
A

arnuld

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 >= '0' && s <= '9'; ++i)
n = 10 * n + (s - '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 ?
 
C

Chris Dollin

arnuld said:
----------------- PROGRAMME ------------------------------
/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;
n = 0;
for (i = 0; s >= '0' && s <= '9'; ++i)
n = 10 * n + (s - '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.
 
S

santosh

arnuld said:
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 >= '0' && s <= '9'; ++i)
n = 10 * n + (s - '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 >= '0' && s <= '9'; ++i)
n = 10 * n + (s - '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);
 
S

santosh

arnuld said:
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 >= '0' && s <= '9'; ++i)
n = 10 * n + (s - '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.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top