사자자리

[C언어] 문자열을 숫자로 변환하기 - atoi / atol / atoll / atof 함수 본문

C언어/C언어 이론

[C언어] 문자열을 숫자로 변환하기 - atoi / atol / atoll / atof 함수

renne 2022. 8. 10. 23:14

헤더: <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;
}

 

Comments