Atoi Dev C++

This function returns the converted integral number as an int value. If no valid conversion could be performed, it returns zero. C atoi method parses the character array/c-string and interprets the string value as a value of integer type. The atoi method returns a value of type integer.

-->

The atoi function converts str into an integer, and returns that integer. Str should start with a whitespace or some sort of number, and atoi will stop reading from str as soon as a non-numerical character has been read. In this case atoi is a legacy C function but C has plenty of better options that you could use (actually the legacy C standard library has better options to use other than atoi). In a good C design you should be avoiding dropping down to C constructs like arrays if at all possible favouring vector or array instead. Yes, that’s right. If atoi cannot perform a conversion, it will return a valid result. Which means that if atoi ever returns 0, you have no idea whether it was because the string is actually “0”, or the string was invalid. I wonder if there are similar 'gotcha's in C. I haven't worked with C too much, but I love C.

Convert a string to integer.

Syntax

Ati Device

Parameters

str
String to be converted.

locale
Locale to use.

Return Value

Each function returns the int value produced by interpreting the input characters as a number. The return value is 0 for atoi and _wtoi, if the input cannot be converted to a value of that type.

In the case of overflow with large negative integral values, LONG_MIN is returned. atoi and _wtoi return INT_MAX and INT_MIN on these conditions. In all out-of-range cases, errno is set to ERANGE. If the parameter passed in is NULL, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, these functions set errno to EINVAL and return 0.

Remarks

These functions convert a character string to an integer value (atoi and _wtoi). The input string is a sequence of characters that can be interpreted as a numerical value of the specified type. The function stops reading the input string at the first character that it cannot recognize as part of a number. This character may be the null character ('0' or L'0') terminating the string.

The str argument to atoi and _wtoi has the following form:

Atoi

[whitespace] [sign] [digits]]

A whitespace consists of space or tab characters, which are ignored; sign is either plus (+) or minus (-); and digits are one or more digits.

The versions of these functions with the _l suffix are identical except that they use the locale parameter passed in instead of the current locale. For more information, see Locale.

C++ Atoi Implementation

By default, this function's global state is scoped to the application. To change this, see Global state in the CRT.

Generic-Text Routine Mappings

TCHAR.H routine_UNICODE & _MBCS not defined_MBCS defined_UNICODE defined
_tstoiatoiatoi_wtoi
_ttoiatoiatoi_wtoi

Requirements

RoutinesRequired header
atoi<stdlib.h>
_atoi_l, _wtoi, _wtoi_l<stdlib.h> or <wchar.h>

Example

This program shows how numbers stored as strings can be converted to numeric values using the atoi functions.

Device

See also

Dev C++ Online

Data Conversion
Floating-Point Support
Locale
_ecvt
_fcvt
_gcvt
setlocale, _wsetlocale
_atodbl, _atodbl_l, _atoldbl, _atoldbl_l, _atoflt, _atoflt_l