문자열을 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]#
:::
