strtod() - strtof() - strtold() — 문자 스트링을 Double, Float, Long Double로 변환

형식

#include <stdlib.h>
double strtod(const char *nptr, char **endptr);
float strtof(const char *nptr, char **endptr);
long double strtold(const char *nptr, char **endptr);

언어 레벨

ANSI

스레드세이프

로케일 감지

이 함수의 작동은 현재 로케일의 LC_CTYPE 및 LC_NUMERIC 범주로 영향을 받을 수 있습니다. 자세한 정보는 CCSID 및 로케일 이해의 내용을 참조하십시오.

설명

strtod(), strtof(), strtold() 함수는 문자 스트링을 double, float 또는 long double 값으로 변환합니다. 매개변수 nptr은 숫자 2진 부동 소수점 값으로 해석할 수 있는 문자의 순서를 가리킵니다. 이 함수는 숫자의 일부로 인식되지 않는 첫 번째 문자에서 스트링 읽기를 중단합니다. 이 문자는 스트링 끝에서 널 문자일 수 있습니다.

strtod(), strtof(), strtold() 함수는 다음 양식의 스트링을 가리키는 nptr을 예상합니다.

구문 도표 읽기시각적 구문 도표 생략
>>-+------------+--+-----+-------------------------------------->
   '-whitespace-'  +- + -+   
                   '- – -'   

>--+-+-digits--+---+--+--------+-+--+------------------------+-----------------+-><
   | |         '-.-'  '-digits-' |  '-+-e-+--+-----+--digits-'                 |   
   | '-.--digits-----------------'    '-E-'  +- + -+                           |   
   |                                         '- – -'                           |   
   '-0--+-x-+--+-hexdigits--+---+--+-----------+-+--+------------------------+-'   
        '-X-'  |            '-.-'  '-hexdigits-' |  '-+-p-+--+-----+--digits-'     
               '-.--hexdigits--------------------'    '-P-'  +- + -+               
                                                             '- – -'               

이 양식에 맞지 않는 첫 번째 문자는 스캔을 중단합니다. 또한 INFINITY 또는 NAN의 순서(대소문자 무시)가 허용됩니다.

지수가 16진수 양식으로 지정된 경우 지수는 2진 지수(밑이 2임)로 해석됩니다. 지수가 10진수 양식으로 지정된 경우 지수는 10진 지수(밑이 10임)로 해석됩니다.

리턴값

strtod(), strtof(), strtold() 함수는 표시로 인해 오버플로 또는 언더플로가 발생하는 경우를 제외하고 부동 소수점 값을 리턴합니다. 오버플로의 경우 strtof()HUGE_VALF 또는 -HUGE_VALF를, strtod()strtold()HUGE_VAL 또는 -HUGE_VAL을 리턴합니다. 언더플로에서 모든 함수는 0을 리턴합니다.

두 경우 모두 errnoERANGE로 설정됩니다. nptr로 가리키는 스트링이 예상되는 양식이 아니면 변환은 수행되지 않고 nptr 값은 endptrNULL 포인터가 아닌 경우 endptr로 가리키는 오브젝트에 저장됩니다.

strtod(), strtof(), strtold() 함수는 숫자 이외 문자 앞에 지수로 읽는 E 또는 e가 나오면 실패하지 않습니다. 예를 들어, 100elf는 부동 소수점 값 100.0으로 변환됩니다.

INFINITY의 문자 순서(대소문자 무시)는 INFINITY 값을 생성합니다. NAN의 문자 값은 비숫자(NAN) 값을 생성합니다.

이 예는 스트링을 double, float, long double 값으로 변환합니다. 그리고 변환이 중단된 서브스트링 및 변환된 값을 인쇄합니다.
#include <stdlib.h>
#include <stdio.h>
 
int main(void)
{
   char *string, *stopstring;
   double x;
   float f;
   long double ld;

   string = "3.1415926This stopped it";
   f = strtof(string, &stopstring);
   printf("string = \"%s\"\n", string);
   printf("   strtof = %f\n", f);
   printf(" Stopped scan at \"%s\"\n\n", stopstring);
   string = "100ergs";
   f = strtof(string, &stopstring);
   printf("string = \"%s\"\n", string);
   printf("   strtof = %f\n", f);
   printf(" Stopped scan at \"%s\"\n\n", stopstring);

   string = "3.1415926This stopped it";
   x = strtod(string, &stopstring);
   printf("string = \"%s\"\n", string);
   printf("   strtod = %f\n", x);
   printf(" Stopped scan at \"%s\"\n\n", stopstring);
   string = "100ergs";
   x = strtod(string, &stopstring);
   printf("string = \"%s\"\n", string);
   printf("   strtod = %f\n", x);
   printf(" Stopped scan at \"%s\"\n\n", stopstring);

   string = "3.1415926This stopped it";
   ld = strtold(string, &stopstring);
   printf("string = \"%s\"\n", string);
   printf("   strtold = %lf\n", ld);
   printf(" Stopped scan at \"%s\"\n\n", stopstring);
   string = "100ergs";
   ld = strtold(string, &stopstring);
   printf("string = \"%s\"\n", string);
   printf("   strtold = %lf\n", ld);
   printf(" Stopped scan at \"%s\"\n\n", stopstring);
}

/*****************  Output should be similar to:  *****************
string = "3.1415926This stopped it"
   strtof = 3.141593
   Stopped scan at "This stopped it"

string = "100ergs"
   strtof = 100.000000
   Stopped scan at "ergs"

string = "3.1415926This stopped it"
   strtod = 3.141593
   Stopped scan at "This stopped it"
 
string = "100ergs"
   strtod = 100.000000
   Stopped scan at "ergs"

string = "3.1415926This stopped it"
   strtold = 3.141593
   Stopped scan at "This stopped it"
 
string = "100ergs"
   strtold = 100.000000
   Stopped scan at "ergs"

*/