Solved

strange strol() behaviour

Posted on 2000-03-08
7
273 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

840 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