[C] stdlib.h/atoi

2021. 6. 18. 16:11🧑🏻‍💻/C & C++

문자열을 정수로 변환
공백이 아닌 문자가 나오기 전까지 공백 문자들을 무시한다.

 

#include <stdio.h>
#include <stdlib.h>
void main(void)
{
int test = 0;
test = atoi("ch0505kim");
printf("%d \r\n", test);
test = atoi("-chkim55");
printf("%d \r\n", test);
test = atoi("55chkim");
printf("%d \r\n", test);
test = atoi("-55chkim");
printf("%d \r\n", test);
test = atoi("\t \r\n-55chkim");
printf("%d \r\n", test);
}

 

 

사용 예)

#include <stdio.h>
#include <stdlib.h>
void main(void)
{
int test;
char testInput[256];
printf("input your number: ");
fgets(testInput, 256, stdin);
test = atoi(testInput);
printf("The entered value %d. \r\n", test);
}