Unexpected endptr with strtod () / strtold ()

I expect to endptrpoint to the same meaning with both c strtod()and c strtold(). But they are different. I suspect that I am strtold()wrong. OTOH, this may be the case when the specification is not clear and any result is acceptable.

Is this an error (and with which function) or an undefined / unspecified behavior?

Usage: gcc \ x86_64-pc-cygwin \ 4.8.3

#include <math.h>
#include <stdio.h>

// Edit - This include is in my true code, but was missing in original post
#include <stdlib.h>

int main(void) {
    char *s = "123ez";
    char *endptr;
    double d = strtod(s, &endptr);
    printf("     double %f '%s'\n", d, endptr);
    long double ld = strtold(s, &endptr);
    printf("long double %Lf '%s'\n", ld, endptr);
    return 0;
    }

Output:
     double 123.000000 'ez'
long double 123.000000 'z'

[change]

gcc comand gcc -I"C:\cygwin64\lib\gcc\x86_64-pc-cygwin\4.8.3\include" -O0 -g3 -Wall -c -fmessage-length=0 -std=c99 -MMD -MP -MF"string_fp.d" -MT"string_fp.d" -o "string_fp.o" "../string_fp.c"

GCC 4.9.2

+4
source share
2 answers

Nothing should be used e. To allow use e, there emust be a non-empty sequence of digits after . And GCC 4.9.0 with Glibc 2.12 behaves correctly.

[2:29pm][wlynch@apple /tmp] ./a.out 
     double 123.000000 'ez'
long double 123.000000 'ez'

N1570 C 2011...

7.22.1.3 3: strtod, strtof strtold

- , :

  • , , , 6.4.4.2;
  • ...

6.4.4.2

exponent-part:
    e signopt digit-sequence
    E signopt digit-sequence
digit-sequence:
    digit
    digit-sequence digit

C, .

+4

strtold - (6.4.4.2):

exponent-part:
    e sign(optional) digit-sequence
    E sign(optional) digit-sequence

:

digit-sequence:
    digit
    digit-sequence digit

digit , . , e .

strtold ( ):

strtod, strtof strtold , nptr double, float long double , . -, : , , ( isspace), NaN; , . .

:

, , , 6.4.4.2.

e , .

+3

All Articles