문자열을 int 로 변환한다.
#include <stdlib.h> int atoi(const char *nptr);
atoi 함수는 nptr 로 지정된 문자열을 int 형으로 변환한다. 이때 변환범위는 숫자로 인식가능한 선까지이다.
예를들어 아규먼트로 "1234ab" 가 주어졌다면 숫자로 인식가능한 문자열범위는 "1234" 이므로 1234 로 변환되게 된다.
만약 변환시킬만한 적당한 문자가 존재하지 않는다면 0을 리턴한다.
변환가능한 숫자, 변환가능한 숫자가 없을경우에는 0
#include <stdio.h> #include <string.h> #include <time.h> #include <unistd.h> #include <sys/types.h> int main() { struct tm tm_ptr; time_t now_time; time_t last_time; char year[5]; char month[3]; char day[3]; char null[5]; char deff; memset(year, 0x00, 5); memset(month, 0x00, 3); memset(day, 0x00, 3); fscanf(stdin, "%[0-9]%[^0-9]%[0-9]%[^0-9]%[0-9]", year, null,month,null,day); tm_ptr.tm_year = atoi(year)-1900; tm_ptr.tm_mon = atoi(month)-1; tm_ptr.tm_mday = atoi(day); tm_ptr.tm_hour = 0; tm_ptr.tm_min = 0; tm_ptr.tm_sec = 0; tm_ptr.tm_isdst = 0; time(&now_time); last_time =mktime(&tm_ptr); printf("만난지 %d일\n",(now_time - last_time)/(3600*24)); }
[root@lcoalhost test]#./date 2002 8 2 만난지 30일 [root@lcoalhost test]#
Copyrights © - Joinc, All Rights Reserved. Inherited From - Yundream Rebranded By - Joonphil
1장. atoi(3)
문자열을 int 로 변환한다.
1.1절. 사용법
1.2절. 설명
atoi 함수는 nptr 로 지정된 문자열을 int 형으로 변환한다. 이때 변환범위는 숫자로 인식가능한 선까지이다.
예를들어 아규먼트로 "1234ab" 가 주어졌다면 숫자로 인식가능한 문자열범위는 "1234" 이므로 1234 로 변환되게 된다.
만약 변환시킬만한 적당한 문자가 존재하지 않는다면 0을 리턴한다.
1.3절. 반환값
변환가능한 숫자, 변환가능한 숫자가 없을경우에는 0
1.4절. 예제
Recent Posts
Archive Posts
Tags