Joinc 팀블로그 리눅스 메뉴얼 정리 Joinc 위키
댓글

Recent Comments

Powered by Disqus
팀블로그 카테고리
  전체 (1105)
   공지사항 (1)
   검색엔진 (21)
   기술동향 (58)
   게임 (2)
   독서 (6)
   리눅스 (12)
   보안 (1)
   사회문제 (22)
   어셈블리 (43)
   영화 (3)
   오픈소스 (10)
   음악 (9)
   인물 (1)
   포인터 (4)
   프로그래머 (23)
   팀블로그 (20)
   테터툴즈 (29)
   C/C++ (152)
   FireFox (11)
   Gimp (2)
   Google (98)
   Java (13)
   Perl (2)
   Pthread (11)
   STL (13)
   TCP/IP (8)
   Tools (31)
   Web2.0 (42)
   Wiki (1)
«   2010/03   »
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
2007/08/09 23:13

linux man page : atoi - 문자열을 int 데이터 타입으로 변환한다.

1장. atoi(3)

차례
1.1절. 사용법
1.2절. 설명
1.3절. 반환값
1.4절. 예제

문자열을 int 로 변환한다.


1.1절. 사용법

#include <stdlib.h>

int atoi(const char *nptr);
		


1.2절. 설명

atoi 함수는 nptr 로 지정된 문자열을 int 형으로 변환한다. 이때 변환범위는 숫자로 인식가능한 선까지이다.

예를들어 아규먼트로 "1234ab" 가 주어졌다면 숫자로 인식가능한 문자열범위는 "1234" 이므로 1234 로 변환되게 된다.

만약 변환시킬만한 적당한 문자가 존재하지 않는다면 0을 리턴한다.


1.3절. 반환값

변환가능한 숫자, 변환가능한 숫자가 없을경우에는 0


1.4절. 예제

#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]#
		


:::