Solved

strange strol() behaviour

Posted on 2000-03-08
7
289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.

636 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