Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-Integer numbers in an String and using atoi

Tags:

c++

c

math

atoi

If there are non-number characters in a string and you call atoi [I'm assuming wtoi will do the same]. How will atoi treat the string?

Lets say for an example I have the following strings:

  1. "20234543"
  2. "232B"
  3. "B"

I'm sure that 1 will return the integer 20234543. What I'm curious is if 2 will return "232." [Thats what I need to solve my problem]. Also 3 should not return a value. Are these beliefs false? Also... if 2 does act as I believe, how does it handle the e character at the end of the string? [Thats typically used in exponential notation]

like image 223
monksy Avatar asked Nov 28 '22 04:11

monksy


1 Answers

You can test this sort of thing yourself. I copied the code from the Cplusplus reference site. It looks like your intuition about the first two examples are correct, but the third example returns '0'. 'E' and 'e' are treated just like 'B' is in the second example also.

So the rules are

On success, the function returns the converted integral number as an int value. If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX or INT_MIN is returned.

like image 124
Mike Avatar answered Dec 23 '22 08:12

Mike