There are several ways to remove commas (or any other character that you want to skip). One of the easiest (and most flexible) is to simply skip two pointers down the input line, shifting all the numbers together, ignoring the commas. (you must be sure to nul-terminate after the final digit).
Example:
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <limits.h> #define MAXC 25 long xstrtol (char *p, char **ep, int base); int main (void) { char string[MAXC] = ""; char *rp, *wp; /* a read pointer and write pointer */ int c; long n = 0; rp = wp = string; printf ("enter number with ',': "); if (!fgets (string, MAXC, stdin)) { fprintf (stderr, "error: insufficient input.\n"); return 1; } /* work down each char in string, shifting number together */ while (*rp && (('0' <= *rp && *rp <= '9') || *rp == ',')) { if (*rp == ',') { rp++; continue; } /* skip commas */ *wp++ = *rp; /* shift digits together */ rp++; } *wp = 0; /* nul-terminate */ if (*rp != '\n') /* flush input buffer */ while ((c = getchar()) != '\n' && c != EOF) {} printf ("\n string : %s\n", string); n = xstrtol (string, &rp, 10); /* convert string to long */ printf(" n : %ld\n\n", n); return 0; } /** a simple strtol implementation with error checking. * any failed conversion will cause program exit. Adjust * response to failed conversion as required. */ long xstrtol (char *p, char **ep, int base) { errno = 0; long tmp = strtol (p, ep, base); /* Check for various possible errors */ if ((errno == ERANGE && (tmp == LONG_MIN || tmp == LONG_MAX)) || (errno != 0 && tmp == 0)) { perror ("strtol"); exit (EXIT_FAILURE); } if (*ep == p) { fprintf (stderr, "No digits were found\n"); exit (EXIT_FAILURE); } return tmp; }
(note: xstrtol just provides error checking when converting string to long )
Using / Exit
$ ./bin/nwithcomma enter number with ',': 1,234,567 string : 1234567 n : 1234567
Take a look and let me know if you have any questions. You can check with INT_MIN and INT_MAX if you want the result to be obtained with int , but it is just as easy to leave the answer as long .
As a function
As indicated, this would be much more useful as a function. You can move the code into a simple conversion function and configure the main code as follows:
... #define MAXC 25 long fmtstrtol (char *s); long xstrtol (char *p, char **ep, int base); int main (void) { char string[MAXC] = ""; printf ("enter number with ',': "); if (!fgets (string, MAXC, stdin)) { fprintf (stderr, "error: insufficient input.\n"); return 1; } printf(" number : %ld\n\n", fmtstrtol (string)); return 0; } long fmtstrtol (char *s) { if (!s || !*s) { fprintf (stderr, "fmtstrtol() error: invalid string.\n"); exit (EXIT_FAILURE); } char *rp, *wp; int c; rp = wp = s; while (*rp && (('0' <= *rp && *rp <= '9') || *rp == ',')) { if (*rp == ',') { rp++; continue; } *wp++ = *rp; rp++; } *wp = 0; if (*rp != '\n') while ((c = getchar()) != '\n' && c != EOF) {} return xstrtol (s, &rp, 10); } ...