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

atoi query

Hi,

I have written this code to help me learn C but I'm not sure why gcc -
Wall is giving me an error when I compile

Basically I want to read in a character then a number and then
manipulate the number.
I've tried scanf such as scanf("%c%d",&letter,&number); but when I
type in say 2 letters instead of 1 letter and 1 number, I get a zero
for the 2nd value as it doesn't match what scanf is expecting so I'm
trying fgets.

#include <stdio.h>
#define LINESIZE 4

int main()
{
char line[LINESIZE];
int temp,temp2;

printf("Please enter a letter and a value: ");
fgets(line, LINESIZE, stdin);

printf("This line was entered: %s\n",line);

printf("line[0] is %c\n ",line[0]);
printf("line[1] is %c\n ",line[1]);
temp = atoi(line[1]);

printf("temp is %d\n ",temp);
temp2 = temp + 5;
printf("temp2 is %d\n ",temp2);

return 0;
}

Then on compile,
gcc -Wall getloc_val.c
getloc_val.c: In function `main':
getloc_val.c:17: warning: implicit declaration of function `atoi'

The book I'm refering to mentions that I can do this but why am I
getting the implicit warning ?
Pat

Apr 18 '07 #1
13 5735
pt*****@gmail.com wrote:
I've tried scanf such as scanf("%c%d",&letter,&number); but when I
type in say 2 letters instead of 1 letter and 1 number, I get a zero
for the 2nd value as it doesn't match what scanf is expecting so I'm
trying fgets.
Yup, that's correct, and that's the right solution.
#include <stdio.h>
#define LINESIZE 4
(This is a rather low value, though. Do you really expect to get A24
_all_ the time? You probably want to handle ABC23, A 24, and A7632 with
a slightly more useful error message than "input not understood", and
you don't want your program to accept the latter as A76.)
int main()
{
char line[LINESIZE];
int temp,temp2;

printf("Please enter a letter and a value: ");
fgets(line, LINESIZE, stdin);

printf("This line was entered: %s\n",line);

printf("line[0] is %c\n ",line[0]);
printf("line[1] is %c\n ",line[1]);
temp = atoi(line[1]);

printf("temp is %d\n ",temp);
temp2 = temp + 5;
printf("temp2 is %d\n ",temp2);

return 0;
}

Then on compile,
gcc -Wall getloc_val.c
getloc_val.c: In function `main':
getloc_val.c:17: warning: implicit declaration of function `atoi'
This, however, is not the right solution.
The book I'm refering to mentions that I can do this
Then either the book is wrong (or at best incomplete), or you missed a
bit.
but why am I getting the implicit warning ?
Because you didn't declare atoi(). Just as you have to #include
<stdio.hfor printf(), and <string.hfor strcpy(), you also have to
#include <stdlib.hfor atoi().
But you don't want to use atoi(), anyway. It has a few problems, because
its error handling is not so much bad as non-existent. Use strtol()
instead; you'll find it declared in <stdlib.has well.

Richard
Apr 18 '07 #2
pt*****@gmail.com wrote, On 18/04/07 12:57:
Hi,

I have written this code to help me learn C but I'm not sure why gcc -
Wall is giving me an error when I compile
You should be using "-ansi -pedantic -Wall -O -W" or for incomplete C99
support "-std=c99" instead of -ansi. This will generate warnings for a
lot more questionable practices.
Basically I want to read in a character then a number and then
manipulate the number.
I've tried scanf such as scanf("%c%d",&letter,&number); but when I
type in say 2 letters instead of 1 letter and 1 number, I get a zero
for the 2nd value as it doesn't match what scanf is expecting
It's worse than that. It won't have written anything to number, that it
contained 0 was merely luck. The return value of scanf will have told
you it only did one conversion.
so I'm
trying fgets.
This is a much better solution.
#include <stdio.h>
#define LINESIZE 4
That's a very short line limit!
int main()
{
char line[LINESIZE];
int temp,temp2;

printf("Please enter a letter and a value: ");
You should flush stdout otherwise the above prompt might not be
displayed before waiting for input.
fgets(line, LINESIZE, stdin);

printf("This line was entered: %s\n",line);

printf("line[0] is %c\n ",line[0]);
printf("line[1] is %c\n ",line[1]);
I think you will find that you need to identify the start of the number
(isdigit will help, but remember to allow for signs) before trying to
convert it.
temp = atoi(line[1]);

printf("temp is %d\n ",temp);
temp2 = temp + 5;
printf("temp2 is %d\n ",temp2);

return 0;
}

Then on compile,
gcc -Wall getloc_val.c
getloc_val.c: In function `main':
getloc_val.c:17: warning: implicit declaration of function `atoi'

The book I'm refering to mentions that I can do this but why am I
getting the implicit warning ?
atoi is declared in stdlib.h, if your book did not tell you this then it
is probably time to get a new book. Also, it is generally better to use
strtol so that you have a chance to do error checking.
--
Flash Gordon
Apr 18 '07 #3
pt*****@gmail.com said:
<snip>
char line[LINESIZE];
<snip>
temp = atoi(line[1]);
Others have noted that you should be using a longer buffer and that
strtol would serve your needs better than atoi, but nobody appears to
have noticed the problem with your argument to atoi.

atoi (and, when you bite the bullet and choose to use it instead,
strtol) must be supplied with a string, not a character, for converting
to an integer. line[1] is not a string, but a single char. A string is
a sequence of characters terminated by the first null character. If you
want to tell, say, atoi to start at the second character of your
string, you can do this by passing the address of that second character
of the string, like this:

temp = atoi(&line[1]);

Incidentally, to convert a single character to a digit is easy. Provided
it /is/ a digit, you can simply subtract '0' from it, because '0', '1',
'2', '3', ... '8', '9' (unlike the alphabet, alas) are guaranteed to
have contiguous and ascending coding points. In other words, the digit
characters are in a little block all of their own, and subtracting '0'
from any value in that block will give you the "distance" from '0'.
Thus, '4' - '0' gives you 4, '7' - '0' gives you 7, etc.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 18 '07 #4
pt*****@gmail.com wrote:
Hi,

I have written this code to help me learn C but I'm not sure why gcc -
Wall is giving me an error when I compile

Basically I want to read in a character then a number and then
manipulate the number.
I've tried scanf such as scanf("%c%d",&letter,&number); but when I
type in say 2 letters instead of 1 letter and 1 number, I get a zero
for the 2nd value as it doesn't match what scanf is expecting so I'm
trying fgets.

#include <stdio.h>
#define LINESIZE 4

int main()
{
char line[LINESIZE];
int temp,temp2;

printf("Please enter a letter and a value: ");
fgets(line, LINESIZE, stdin);

printf("This line was entered: %s\n",line);

printf("line[0] is %c\n ",line[0]);
printf("line[1] is %c\n ",line[1]);
temp = atoi(line[1]);

printf("temp is %d\n ",temp);
temp2 = temp + 5;
printf("temp2 is %d\n ",temp2);

return 0;
}

Then on compile,
gcc -Wall getloc_val.c
getloc_val.c: In function `main':
getloc_val.c:17: warning: implicit declaration of function `atoi'

The book I'm refering to mentions that I can do this but why am I
getting the implicit warning ?
Implict function declarations were legal under the c89/c90 standard.
The function had to return int and not be a variadic function. As
atoi() fits that, it's ok for a compiler conforming to that standard,
but bad practice. It's not legal under c99.

The compiler is allowed to issue any diagnostics it wants, as long as
it does the ones it must (in conforming mode). Compilers will often
issue diagnostics (a warning is a diagnostic for many compilers) for
things the designers thought might be programmer errors. Leaving out
the header is one such.


Brian
Apr 18 '07 #5
Thanks all for the advice.
I've done some further investigation using strtol and have modified my
program as such

#include <stdio.h>
#include <stdlib.h>

#define LINESIZE 10

int main()
{
char line[LINESIZE];
char *lptr;
int temp;

printf("Please enter a letter and a value: ");
fgets(line, LINESIZE, stdin);

temp = strtol(line, &lptr, 0);

printf("The original string was %s\n",line);
printf("The numeric portion is %ld\n",temp);

return 0;
}

After compiling, I ran the program
with these results:

Please enter a letter and a value: 123qwe
The original string was 123qwe
The numeric portion is 123

Please enter a letter and a value: qwe123
The original string was qwe123
The numeric portion is 0

strltol seems to work when I enter numbers first then chars but not
the other way around.

Now I'm confused in how I can use strtol on the 2nd example, ie qwe123
and extract the numeric portion, which was based on my original
intentions of getting a character then a numeric from a string.
Have I misunderstood the strtol function ?
Pat

Apr 20 '07 #6
pt*****@gmail.com said:
Thanks all for the advice.
I've done some further investigation using strtol and have modified my
program as such
<much snippage, both sides of this strtol call>
temp = strtol(line, &lptr, 0);

After compiling, I ran the program
with these results:

Please enter a letter and a value: 123qwe
The original string was 123qwe
The numeric portion is 123

Please enter a letter and a value: qwe123
The original string was qwe123
The numeric portion is 0

strltol seems to work when I enter numbers first then chars but not
the other way around.
Yeah - it's okay as long as it has numbers, but it stops when it runs
out. :-)
Now I'm confused in how I can use strtol on the 2nd example, ie qwe123
and extract the numeric portion, which was based on my original
intentions of getting a character then a numeric from a string.
Have I misunderstood the strtol function ?
Yes. First plough your way through the non-numeric stuff, capturing it
if you desire, but in any case ploughing through it. Once you hit a
digit (or a '-' character, if you like negative numbers), call strtol.

Useful function in <ctype.h>: isdigit()

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 20 '07 #7
pt*****@gmail.com wrote:
>
.... snip ...
>
strltol seems to work when I enter numbers first then chars but
not the other way around.

Now I'm confused in how I can use strtol on the 2nd example, ie
qwe123 and extract the numeric portion, which was based on my
original intentions of getting a character then a numeric from a
string. Have I misunderstood the strtol function ?
Yes. strtol starts at the beginning. It can't extract a number
from chars, so it fails. It will skip leading blanks only.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
--
Posted via a free Usenet account from http://www.teranews.com

Apr 20 '07 #8
Richard Heathfield wrote, On 20/04/07 12:37:
pt*****@gmail.com said:
>Thanks all for the advice.
I've done some further investigation using strtol and have modified my
program as such
<much snippage, both sides of this strtol call>
> temp = strtol(line, &lptr, 0);
temp should be of type long when using strtol. You will have problems if
it is of type int and the value read is too large to fit in an int.

<snip>
>Now I'm confused in how I can use strtol on the 2nd example, ie qwe123
and extract the numeric portion, which was based on my original
intentions of getting a character then a numeric from a string.
Have I misunderstood the strtol function ?

Yes. First plough your way through the non-numeric stuff, capturing it
if you desire, but in any case ploughing through it. Once you hit a
digit (or a '-' character, if you like negative numbers), call strtol.

Useful function in <ctype.h>: isdigit()
Also consider how you should handle things like "A-B-23".
--
Flash Gordon
Apr 20 '07 #9
Flash Gordon <sp**@flash-gordon.me.ukwrites:
Richard Heathfield wrote, On 20/04/07 12:37:
>pt*****@gmail.com said:
[...]
>>Now I'm confused in how I can use strtol on the 2nd example, ie qwe123
and extract the numeric portion, which was based on my original
intentions of getting a character then a numeric from a string.
Have I misunderstood the strtol function ?
Yes. First plough your way through the non-numeric stuff, capturing
it if you desire, but in any case ploughing through it. Once you hit
a digit (or a '-' character, if you like negative numbers), call
strtol.
Useful function in <ctype.h>: isdigit()

Also consider how you should handle things like "A-B-23".
And "123xyz456". You probably just want to return 123, but your
specification should cover all cases.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 20 '07 #10
On Apr 18, 1:07 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
ptq2...@gmail.com wrote:
But you don't want to use atoi(), anyway. It has a few problems, because
its error handling is not so much bad as non-existent. Use strtol()
instead; you'll find it declared in <stdlib.has well.
But annoyingly there's no strtoi(), so you have to first do strtol()
then cast down to int. Plus strtol has tedious extra parameters to
pass.
Richard

Apr 22 '07 #11
hi,
#include <stdio.h>
#include <stdlib.h>

#define LINESIZE 4

int main()
{
char line[LINESIZE];
int temp,temp2;

printf("Please enter a letter and a value: ");
fgets(line, LINESIZE, stdin);

printf("This line was entered: %s\n",line);

printf("line[0] is %c\n ",line[0]);
printf("line[1] is %c\n ",line[1]);
temp = atoi(&line[1]);

printf("temp is %d\n ",temp);
temp2 = temp + 5;
printf("temp2 is %d\n ",temp2);

return 0;

}

you have to add a #include <stdlib.hbcz you are using linb function
called atoi
and you have to pass address inside the atoi ; like temp =
atoi(&line[0]);

regards
basavaraj
On Apr 18, 4:57 pm, ptq2...@gmail.com wrote:
Hi,

I have written this code to help me learn C but I'm not sure why gcc -
Wall is giving me an error when I compile

Basically I want to read in a character then a number and then
manipulate the number.
I've tried scanf such as scanf("%c%d",&letter,&number); but when I
type in say 2 letters instead of 1 letter and 1 number, I get a zero
for the 2nd value as it doesn't match what scanf is expecting so I'm
trying fgets.

#include <stdio.h>
#define LINESIZE 4

int main()
{
char line[LINESIZE];
int temp,temp2;

printf("Please enter a letter and a value: ");
fgets(line, LINESIZE, stdin);

printf("This line was entered: %s\n",line);

printf("line[0] is %c\n ",line[0]);
printf("line[1] is %c\n ",line[1]);
temp = atoi(line[1]);

printf("temp is %d\n ",temp);
temp2 = temp + 5;
printf("temp2 is %d\n ",temp2);

return 0;

}

Then on compile,
gcc -Wall getloc_val.c
getloc_val.c: In function `main':
getloc_val.c:17: warning: implicit declaration of function `atoi'

The book I'm refering to mentions that I can do this but why am I
getting the implicit warning ?
Pat

Apr 23 '07 #12
Fr************@googlemail.com wrote:
On Apr 18, 1:07 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
But you don't want to use atoi(), anyway. It has a few problems, because
its error handling is not so much bad as non-existent. Use strtol()
instead; you'll find it declared in <stdlib.has well.

But annoyingly there's no strtoi(), so you have to first do strtol()
then cast down to int. Plus strtol has tedious extra parameters to
pass.
Those aren't tedious; one of those _is_ the error handling, and the
other one can be made to do useful work, as well.

Richard
Apr 23 '07 #13
Basu wrote:
hi,
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
Apr 23 '07 #14

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: John Smith | last post by:
What's wrong with the use of atoi in the following code? Why do I get the error message: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *' char cBuffer; void...
5
by: Bansidhar | last post by:
atoi() function seems not to have any support for Hex, octal number. Usually when I read from a text file then it contain number like 0x232 etc. In this case atoi() fells. In case of itoa() there...
15
by: puzzlecracker | last post by:
does anyone know how to implement this function efficiently?
47
by: sudharsan | last post by:
Hi could you please explain wat atoi( ) function is for and an example how to use it?
9
by: Would | last post by:
Hey, hopefully one of you can help me... I keep getting an unresolved external 'atoi(char)' and I dont know why.. here is the code #include <iostream> #include <stdlib.h> using namespace std; ...
11
by: Nezhate | last post by:
Hi all, Can you help me? Why this warning appears in the next simple code ? warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast. #include <stdio.h> #include...
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...
10
by: 66650755 | last post by:
First,thanks for all who have answered my last question. if char string="12345"; how could I convert the string(that is "3") to an int by using atoi? I only want to convert...
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: 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:
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.