Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int
- main(int argc, char *argv[])
- {
- int base;
- char *endptr, *str;
- unsigned long val;
- if (argc < 2) {
- fprintf(stderr, "Usage: %s str [base]\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- str = argv[1];
- base = 10;
- errno = 0; /* To distinguish success/failure after call */
- val = strtoul(str, &endptr, base);
- /* Check for various possible errors */
- if ((errno == ERANGE && (val == ULONG_MAX)) || (errno != 0 && val == 0)) {
- perror("strtoul");
- exit(EXIT_FAILURE);
- }
- if (!isdigit(str[0])) {
- fprintf(stderr,"- character were found\n");
- exit(EXIT_FAILURE);
- }
- if (endptr == str) {
- fprintf(stderr, "No digits were found\n");
- exit(EXIT_FAILURE);
- }
- /* If we got here, strtol() successfully parsed a number */
- printf("strtul() returned %lu\n", val);
- if (*endptr != '\0') /* Not necessarily an error... */
- printf("Further characters after number: %s\n", endptr);
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement