Solved

strange strol() behaviour

Posted on 2000-03-08
7
243 Views
Last Modified: 2010-04-15
anyone have any idea why this:

printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
strtol("01", NULL, NULL),
strtol("02", NULL, NULL),
strtol("03", NULL, NULL),
strtol("04", NULL, NULL),
strtol("05", NULL, NULL),
strtol("06", NULL, NULL),
strtol("07", NULL, NULL),
strtol("08", NULL, NULL),
strtol("09", NULL, NULL),
strtol("10", NULL, NULL),
strtol("11", NULL, NULL),
strtol("12", NULL, NULL));

gives me this output:

1,2,3,4,5,6,7,0,0,10,11,12

??

thanks and regards.

0
Comment
Question by:meverest
  • 3
  • 2
  • 2
7 Comments
 
LVL 2

Accepted Solution

by:
bbousquet earned 150 total points
ID: 2598644
Your 'number base' (third parameter) seems to default to octal (base 8). Therefore, strtol stops parsing as it encounters a number outside of 0-7. Specify 10 as the third parameter to all strtol calls. This should fix the problem.
0
 
LVL 37

Author Comment

by:meverest
ID: 2598717
thanks,

that is probably it, but i have worked around it like this:

int m = (month[0]-48)*10 + (month[1]-48);

still doesn't explain why it returns 0 for '09' as well, or why it returns correct values for '10', '11', '12'

but thanks for your response.

regards.


0
 
LVL 2

Expert Comment

by:bbousquet
ID: 2598761
Yes, it explains your problem easily.

Octal (base-8) uses digits 0-7. Thus:

"00" becomes 0
"01" becomes 1
"02" becomes 2
"03" becomes 3
"04" becomes 4
"05" becomes 5
"06" becomes 6
"07" becomes 7
"08" becomes 0 because 8 is not recognized as a valid digit (0-7). Thus, strtol stops converting and keeps 0 as the result.
etc.

If you would have tried strtol("108", NULL, NULL) you probably would've ended up with 10 as a result.

Just use strtol("somevaluehere", NULL, 10) and it will work flawlessly.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 37

Author Comment

by:meverest
ID: 2598800
well how about that?

so it does make sense after all! ;-)

thanks indeed.

regards.
0
 
LVL 84

Expert Comment

by:ozo
ID: 2599606
No, the result of strtol("108", NULL, NULL) would be 108
strtol("0108", NULL, NULL) would be 8
strtol("012", NULL, NULL) would be 10
0
 
LVL 2

Expert Comment

by:bbousquet
ID: 2599613
ozo,

Well, it looks like meverest's runtime library behaves otherwise, from the results obtained.
0
 
LVL 84

Expert Comment

by:ozo
ID: 2601385
meverest's runtime library behaves exactly the way the standard requires strtol to behave
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now