[ create a new paste ] login | about

Link: http://codepad.org/yJxmhYFa    [ raw code | output | fork ]

slevy1ster - C, pasted on Jul 11:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
#include<errno.h>
#define MAX_LENGTH_OF_LONG 11
int main()
{
    char *endptr;
    char yytext[50]="012387";
    int yyleng = strlen(yytext);
    long int lval = 0;
    double dval = 0.0;
    
    if (yyleng < MAX_LENGTH_OF_LONG - 1) { /* Won't overflow */
          lval = strtol(yytext, &endptr, 8);
    }
    else
    {
       errno = 0;
       lval = strtol(yytext, &endptr, 8); 
       if (errno == ERANGE) { /* Overflow */
         if (*endptr != '\0') {
             printf("Invalid character(s) and any subsequent: %s\n", endptr);
             return 0;
         } else {
          dval = strtod(yytext, NULL);
          puts("number is a double with token T_DNUMBER");
          return 0;
         }// end if-else
       }// end if errno
    }// end outer else

    if (*endptr != '\0') {
          printf("Invalid character(s) and any subsequent: %s\n", endptr);
          return 0;
    } else {  
          printf("lval is %d\n",lval);  
          puts("number is a long with token L_DNUMBER");
          return 0;
    }

}


Output:
1
Invalid character(s) and any subsequent: 87


Create a new paste based on this one


Comments: