Libft(15) - atoi
2021. 6. 26. 18:54ㆍ42seoul/42 Cursus
Libft(15) - atoi
ATOI(3) BSD Library Functions Manual ATOI(3)
NAME
atoi, atoi_l -- convert ASCII string to integer
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <stdlib.h>
int
atoi(const char *str);
#include <xlocale.h>
int
atoi_l(const char *str, locale_t loc);
DESCRIPTION
The atoi() function converts the initial portion of the string pointed to by str to int
representation.
It is equivalent to:
(int)strtol(str, (char **)NULL, 10);
While the atoi() function uses the current locale, the atoi_l() function may be passed a
locale directly. See xlocale(3) for more information.
IMPLEMENTATION NOTES
The atoi() and atoi_l() functions are thread-safe and async-cancel-safe.
The strtol() and strtol_l() functions are recommended instead of atoi() and atoi_l() func-
tions, especially in new code.
ERRORS
The function atoi() need not affect the value of errno on an error.
SEE ALSO
atof(3), atol(3), strtod(3), strtol(3), strtoul(3), xlocale(3)
STANDARDS
The atoi() function conforms to ISO/IEC 9945-1:1990 (``POSIX.1''), ISO/IEC 9899:1990
(``ISO C90''), and ISO/IEC 9899:1999 (``ISO C99'').
BSD June 4, 1993 BSD
- 문자열을 정수 타입으로 바꿈.
- strtol()과 동일. strtol은 문자열을 long 형태로 바꿔주는 것.
오버플로우 이슈
- atoi 함수는 int 형이지만 실제로는 strtol을 따르므로 long 범위 이후부터 오버플로우가 발생함.
- 그런데 int형 오버플로우를 굳이 처리하거나 overflow를 굳이 해결해야 할까??
- atoi 함수가 strtol을 따를 뿐이지 오버플로우 결과값에 특별한 이유가 존재하지 않는 undefined behavior 이므로 atoi는 int 오버플로우를 처리하지 않음.
기본 함수 구조 및 매개변수
#include <stdlib.h>
int
atoi(const char *str);
- str: 정수로 바꿀 문자열
에러
atoi()는 에러 시 errno에 영향을 주지 않음.
이 말은 오버플로우된 리턴 값이 의미없다는 것으로 해석됨.
위의 오버플로우 이슈가 man에 잘 적혀 있는 것!
'42seoul > 42 Cursus' 카테고리의 다른 글
Libft(17) - isdigit (0) | 2021.06.26 |
---|---|
Libft(16) - isalpha (0) | 2021.06.26 |
Libft(14) - strncmp (0) | 2021.06.26 |
Libft(13) - strnstr (0) | 2021.06.26 |
Libft(12) - strrchr (0) | 2021.06.26 |