473,666 Members | 2,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1713
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...@earthl ink.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...@earthl ink.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...@earthl ink.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
1676
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 somtimes confused about the difference between a two-dimensional array and an array of pointers..." then continues to explain int *b; to be...
14
2852
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 message. may you tell me what is wrong and how to make that right ? ------------------------------ INPUT --------------------- #include <stdio.h> int main() {
12
2213
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: ------------------------------- PROGRAMME -------------------------- /* Section 2.7 type conversions we are asked to write a function "htoi(an array)" that accomplishes this:
4
1565
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 -------------- /* K&R2 section 1.5.3, exercise 1-8 write a programme to count blanks, tabs and newlines */
16
1799
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 looking at CLC-Wiki for K&R2 solutions: --------------- PROGRAMME ------------ /* K&R2 section 1.5.3, exercise 1-9 STATEMENT: write a programme to copy its input to output replacing
1
1382
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
1810
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> there are some ambiguous errata, for which I propose solutions. Any comments are welcome.
82
2796
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
1582
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; };
0
8448
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8552
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2773
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.