mattes@azu.informatik.uni-stuttgart.de (Eberhard Mattes) (11/19/90)
Is the behavior of strtol defined in the ANSI standard? What should strtol ("089", NULL, 0) return? -- Eberhard Mattes (mattes@azu.informatik.uni-stuttgart.de)
darcy@druid.uucp (D'Arcy J.M. Cain) (11/20/90)
In article <MATTES.90Nov19122748@azu.informatik.uni-stuttgart.de> mattes@azu.informatik.uni-stuttgart.de (Eberhard Mattes) writes: >Is the behavior of strtol defined in the ANSI standard? Yes. >What should > strtol ("089", NULL, 0) >return? It should return 0. Since it is an octal number (based on the leading 0) the scan stops at the first character not a digit in base 8 which is the '8' thus the scanned string is "0" and hence 0 is returned. -- D'Arcy J.M. Cain (darcy@druid) | D'Arcy Cain Consulting | I support gun control. West Hill, Ontario, Canada | Let's start with the government! + 416 281 6094 |
steve@taumet.com (Stephen Clamage) (11/21/90)
mattes@azu.informatik.uni-stuttgart.de (Eberhard Mattes) writes: >Is the behavior of strtol defined in the ANSI standard? Yes. Section 4.10.1.5. >What should > strtol ("089", NULL, 0) >return? It should return a long zero (0L). When you pass in a zero for the "base" parameter, strtol uses the rules in section 3.1.3.2 for evaluating the number in the string. Section 3.1.3.2 says an integer beginning with 0 is an octal constant, and consists only of the digits 0-7. Therefore, the 8 is not part of the integer, and the string is processed as if it were "0". Strtol ignores trailing characters which cannot be part of the integer it converts. -- Steve Clamage, TauMetric Corp, steve@taumet.com