#include <stdlib.h>
#include <locale.h>
#include <errno.h>
#include <stdio.h>
 
extern int errno;

/*
 * The following example uses the wcstod subroutine to convert
 * a wide character string to a signed long integer.
 */
int main(int argc, char **argv) {
    wchar_t   *pwcs, *endptr;
    long int  retval;
 
    setlocale(LC_ALL, "");
    
    /*
     * Let pwcs point to a wide character null terminated
     * string containing a signed long integer value.
     */
    errno = 0; /* set errno to  zero  */
    retval = wcstol(pwcs, &endptr, 0);
 
    if (errno != 0) { /* errno has changed, so error has occurred */
        if (errno == ERANGE) {
            /*  
             * Correct value is outside range of
             * representable values. Case of overflow
             * error
             */
             if ((retval == LONG_MAX) || (retval == LONG_MIN)) {
                /*  Error case. Handle accordingly.     */
             } else if(errno == EINVAL) {
                /*  The value of base is not supported  */
                /*  Handle appropriately                */
            }
        }
    }
    /*  retval contains the long integer. */
    printf("Double from wide-character: %l\n", retval);
    return (0);
}
