473,325 Members | 2,342 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,325 software developers and data experts.

How standard atoi works?

hi all,

Hereunder two versions of reading a word from stdin stream:

/* code starts */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

char *
getnumber1(void); /* return a word from stdin */

void
getnumber2(char *); /* read a word from stdin and store it in the char*
*/

int main()
{
int i = 0;
char w[20];

i = atoi(getnumber1());
printf("getnumber1 = %d\n", i);
i = 0;
getnumber2(w);
i = atoi(w);
printf("getnumber2 = [%s]\n", w);
printf("getnumber2 = %d\n", i);
return 0;
}

char *
getnumber1(void)
{
char w[20];
char *p;
int c;

p = w;
while (isspace(c = getchar()))
;
if (c != EOF)
*p++ = c;
for (;;) {
if (isspace(c = getchar())) {
ungetc(c, stdin);
break;
}
*p++ = c;
}
*p = '\0';
printf("getnumber1 = [%s]\n", w);
return w;
}

void
getnumber2(char *w)
{
char *p;
int c;

p = w;
while (isspace(c = getchar()))
;
if (c != EOF)
*p++ = c;
for (;;) {
if (isspace(c = getchar())) {
ungetc(c, stdin);
break;
}
*p++ = c;
}
*p = '\0';
return;
}
/* code ends */

I use bc++5.5 as compiler.
the input and output of this small program is like this:

D:\c\scan>getnum
12
getnumber1 = [12]
getnumber1 = 1
12
getnumber2 = [12]
getnumber2 = 12

You can see that results of getnumber1 and getnumber2 are the same.
(12)
but the output of atoi are diffrent.
Can anyone do me a favor to explain the diffrence?

Thanx and
Regards,

jigsaw

Jan 15 '06 #1
2 2569
In article <11**********************@o13g2000cwo.googlegroups .com>,
<ji****@gmail.com> wrote:
int main()
{
int i = 0;
char w[20]; i = atoi(getnumber1()); char *
getnumber1(void)
{
char w[20];
char *p;
int c; p = w; return w;


When you return a char[n], the result is not that the character string
contents are somehow transfered back: the results are that the
address of the char[n] array is transferred back. Unfortunately for
you, char w[20] in this context is a local variable of automatic
duration, so its address is not valid after the return of the routine
it is created in. You are thus attempting to employ undefined
behaviour.
--
Prototypes are supertypes of their clones. -- maplesoft
Jan 15 '06 #2
Got it. Thank you Roberson.

Thanx and
Regards,

jigsaw

Jan 16 '06 #3

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

Similar topics

19
by: Mike Moum | last post by:
I think there may be a bug in string.atoi and string.atol. Here's some output from idle. > Python 2.3.4 (#2, Jan 5 2005, 08:24:51) > on linux2 > Type "copyright", "credits" or "license()"...
6
by: Marco Chiarandini | last post by:
Dear all, I am experiencing a problem in the deallocation of STL data structures. In paritcular I create a vector of sets and insert integers in each set. I do not define any deallocator since...
6
by: Allan Bruce | last post by:
Hi there, I am loading in a 3d object from file. The code I have works fine but it is very slow for objects with many vertices, i.e. >5000 I make use of the atoi function 3 times for each vertex,...
15
by: puzzlecracker | last post by:
does anyone know how to implement this function efficiently?
3
by: Benjamin Rutt | last post by:
Does anyone have a database of C standard library functions? or even clean header files with just the prototypes? when I look at my system's header files, I see a bunch of "implementation...
47
by: sudharsan | last post by:
Hi could you please explain wat atoi( ) function is for and an example how to use it?
2
by: useasdf4444 | last post by:
I have the following program: #include <stdio.h> #include <string.h> #include <stdlib.h> void main () { char command; char *com,*pos,*stack1,*stack2; int boxes,boxtable,i,j,k,st1,st2; ...
4
by: Ram | last post by:
Hi All, Firstly i am a newbie and trying to learn C. The background of the problem is Program: Presently I am working on a program of numerology and the I/P will be the name and output...
50
by: Bill Cunningham | last post by:
I have just read atoi() returns no errors. It returns an int though and the value of the int is supposed to be the value of the conversion. It seems to me that right there tells you if there was...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.