atoi, _atoi_l, _wtoi, _wtoi_l
Convert a string to integer.
int atoi( const char *str ); int _wtoi( const wchar_t *str ); int _atoi_l( const char *str, _locale_t locale ); int _wtoi_l( const wchar_t *str, _locale_t locale );
- str
String to be converted.
- locale
Locale to use.
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 매개 변수 유효성 검사. If execution is allowed to continue, these functions set errno to EINVAL and return 0.
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 atoiand _wtoi has the following form:
[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 로캘.
Generic-Text Routine Mappings
TCHAR.H routine | _UNICODE & _MBCS not defined | _MBCS defined | _UNICODE defined |
---|---|---|---|
_tstoi | atoi | atoi | _wtoi |
_ttoi | atoi | atoi | _wtoi |
요구 사항
Routines | Required header |
---|---|
atoi | <stdlib.h> |
_atoi_l, _wtoi, _wtoi_l | <stdlib.h> or <wchar.h> |
예제
This program shows how numbers stored as strings can be converted to numeric values using the atoi functions. // crt_atoi.c // This program shows how numbers // stored as strings can be converted to // numeric values using the atoi functions. #include <stdlib.h> #include <stdio.h> #include <errno.h> int main( void ) { char *str = NULL; int value = 0; // An example of the atoi function. str = " -2309 "; value = atoi( str ); printf( "Function: atoi( \"%s\" ) = %d\n", str, value ); // Another example of the atoi function. str = "31412764"; value = atoi( str ); printf( "Function: atoi( \"%s\" ) = %d\n", str, value ); // Another example of the atoi function // with an overflow condition occuring. str = "3336402735171707160320"; value = atoi( str ); printf( "Function: atoi( \"%s\" ) = %d\n", str, value ); if (errno == ERANGE) { printf("Overflow condition occurred.\n"); } } |
atol, _atol_l, _wtol, _wtol_l
Convert a string to a long integer.
long atol( const char *str ); long _atol_l( const char *str, _locale_t locale ); long _wtol( const wchar_t *str ); long _wtol_l( const wchar_t *str, _locale_t locale );
- str
String to be converted.
- locale
Locale to use.
Each function returns the long value produced by interpreting the input characters as a number. The return value is 0L for atol if the input cannot be converted to a value of that type.
In the case of overflow with large positive integral values, atol returns LONG_MAX; in the case of overflow with large negative integral values, LONG_MIN is returned. 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 매개 변수 유효성 검사. If execution is allowed to continue, these functions set errno to EINVAL and return 0.
These functions convert a character string to a long integer value (atol).
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 atol has the following form:
[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.
_wtol is identical to atol except that it takes a wide character string.
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 로캘.
Generic-Text Routine Mappings
TCHAR.H routine | _UNICODE & _MBCS not defined | _MBCS defined | _UNICODE defined |
---|---|---|---|
_tstol | atol | atol | _wtol |
_ttol | atol | atol | _wtol |
요구 사항
Routines | Required header |
---|---|
atol | <stdlib.h> |
_atol_l, _wtol, _wtol_l | <stdlib.h> and <wchar.h> |
예제
This program shows how numbers stored as strings can be converted to numeric values using the atol function. // crt_atol.c // This program shows how numbers stored as // strings can be converted to numeric values // using the atol functions. #include <stdlib.h> #include <stdio.h> #include <errno.h> int main( void ) { char *str = NULL; long value = 0; // An example of the atol function // with leading and trailing white spaces. str = " -2309 "; value = atol( str ); printf( "Function: atol( \"%s\" ) = %d\n", str, value ); // Another example of the atol function // with an arbitrary decimal point. str = "314127.64"; value = atol( str ); printf( "Function: atol( \"%s\" ) = %d\n", str, value ); // Another example of the atol function // with an overflow condition occurring. str = "3336402735171707160320"; value = atol( str ); printf( "Function: atol( \"%s\" ) = %d\n", str, value ); if (errno == ERANGE) { printf("Overflow condition occurred.\n"); } } Function: atol( " -2309 " ) = -2309 Function: atol( "314127.64" ) = 314127 Function: atol( "3336402735171707160320" ) = 2147483647 Overflow condition occurred. |
atof, _atof_l, _wtof, _wtof_l
Visual Studio 2015Convert a string to double.double atof( const char *str ); double _atof_l( const char *str, _locale_t locale ); double _wtof( const wchar_t *str ); double _wtof_l( const wchar_t *str, _locale_t locale );
- str
String to be converted.
- locale
Locale to use.
Each function returns the double value produced by interpreting the input characters as a number. The return value is 0.0 if the input cannot be converted to a value of that type.
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 매개 변수 유효성 검사. If execution is allowed to continue, these functions set errno to EINVAL and return 0.
These functions convert a character string to a double-precision, floating-point value.
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 atof and _wtof has the following form:
[whitespace] [sign] [digits] [.digits] [ {d | D | e | E }[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 decimal digits. If no digits appear before the decimal point, at least one must appear after the decimal point. The decimal digits may be followed by an exponent, which consists of an introductory letter (d, D, e, or E) and an optionally signed decimal integer.
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.
Generic-Text Routine Mappings
TCHAR.H routine
_UNICODE & _MBCS not defined
_MBCS defined
_UNICODE defined
_tstof
atof
atof
_wtof
_ttof
atof
atof
_wtof
요구 사항
Routine(s)
Required header
atof
<math.h> and <stdlib.h>
_atof_l
<math.h> and <stdlib.h>
_wtof, _wtof_l
<stdlib.h> or <wchar.h>
예제
This program shows how numbers stored as strings can be converted to numeric values using the atof function.
// crt_atof.c // // This program shows how numbers stored as // strings can be converted to numeric // values using the atof function. #include <stdlib.h> #include <stdio.h> int main( void ) { char *str = NULL; double value = 0; // An example of the atof function // using leading and training spaces. str = " 3336402735171707160320 "; value = atof( str ); printf( "Function: atof( \"%s\" ) = %e\n", str, value ); // Another example of the atof function // using the 'd' exponential formatting keyword. str = "3.1412764583d210"; value = atof( str ); printf( "Function: atof( \"%s\" ) = %e\n", str, value ); // An example of the atof function // using the 'e' exponential formatting keyword. str = " -2309.12E-15"; value = atof( str ); printf( "Function: atof( \"%s\" ) = %e\n", str, value ); }Function: atof( " 3336402735171707160320 " ) = 3.336403e+021 Function: atof( "3.1412764583d210" ) = 3.141276e+210 Function: atof( " -2309.12E-15" ) = -2.309120e-012
from MSDN.
본 글이 포함된 상위 정리 장소.
Visual Studio/VC++/C/C# 활용정리 -> http://igotit.tistory.com/11
///513.
'VisualStudio.C++.C# > 코딩팁,함수활용,단편' 카테고리의 다른 글
정수형 signed, unsigned 8, 16, 32, 64비트별 값 표현 범위. (0) | 2015.12.24 |
---|---|
char*, const char*, char* const (0) | 2015.12.23 |
CString, LPCTSTR, LPTSTR. (0) | 2015.12.21 |
VC++. 출력 언어 고정하기.예. #pragma setlocale("kor") (0) | 2015.12.18 |
VC++. LPCSTR, LPCWSTR, LPCTSTR, LPTSTR, TCHAR,char,wchar_t. 멀티바이트, 유니코드. (0) | 2015.12.18 |