사자자리
[C언어] 문자열을 숫자로 변환하기 - atoi / atol / atoll / atof 함수 본문
헤더: <stdlib.h>
ASCII string to integer | atoi | int atoi (char const *str); | 문자열을 int로 변환 |
ASCII string to long | atol | long atol (char const *str); | 문자열을 long으로 변환 |
ASCII string to long long | atoll | long long atoll (char const *str); | 문자열을 long long으로 변환 |
ASCII string to float | atof | double atof (char const *str); | 문자열을 double로 변환 |
atoi
#include <stdio.h>
#include <stdlib.h>
int main(){
char *test = "500";
printf("%d\n", test); //4210688 출력
printf("%d\n", atoi(test)); //500 출력
return 0;
}
atol
#include <stdio.h>
#include <stdlib.h>
int main(){
char *test = "500";
printf("%ld\n", test); //4210688 출력
printf("%ld\n", atol(test)); //500 출력
return 0;
}
atoll
#include <stdio.h>
#include <stdlib.h>
int main(){
char *test = "9111222333444555666";
printf("%lld\n", test); //4210688 출력
printf("%lld\n", atoll(test)); //9111222333444555666 출력
return 0;
}
atof
#include <stdio.h>
#include <stdlib.h>
int main(){
char *test = "1.23456";
printf("%f\n", test); //0.000000 출력
printf("%f\n", atof(test)); //1.234560 출력
return 0;
}
'C언어 > C언어 이론' 카테고리의 다른 글
[C언어] 문자가 숫자인지 아닌지 판별하기 - isdigit 함수 (0) | 2022.08.10 |
---|---|
[C언어] 7주차 동적 메모리 할당 (0) | 2022.06.26 |
[C언어] 7주차 포인터의 활용 (0) | 2022.06.26 |
[C언어] 6주차 함수의 활용 (0) | 2022.06.25 |
[C언어] 6주차 구조체 (0) | 2022.05.27 |
Comments