• C

Checking arguments in argv

I am trying to go through the argument vector and check whether each argument is either an integer or a string that was entered.  How would I be able to tell if It was a string or integer?  Thanks.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Every argument u recieve would be in the form of char*

Then to distinguish if a particular arg is Numeric or not, u can may use atoi() function and depedning on the return, u can decide whether the arg is a number

   loop through all the chars in the argument and call isdigit to see if they are all '0' - '9'.
If the all are then the argument is an integer. You should also check for a '-' for negative numbers.

A function would look like this:

int isintvalue(char* str)
      if (*str == '-')
     for ( ; *str; str++)
        if (! isdigit(*str))
             return 0;
     return 1;

NOTE: there are problems if you just call atoi and check for non 0 value.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Check to see if it is an integer first.  If not, it is a string.
The strtol() function can handle this for you.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Interperet arg as integer or string.
 * If the returned *asStr is non-NULL, it is the argument as a string.
 * If the returned *asStr is NULL, then asInt holds the integer value of the arg.
void intOrStr(char * arg, char ** asStr, long * asInt)
  char *endp = NULL;

  if (arg) {
    /* first attempt to parse the arg as an integer */
    *asInt = strtol(arg, &endp, 0);

    /* if there was no integer, then (endp == arg) */
    if ((endp == arg) || (*endp)) {
      *asInt = 0;
      *asStr = arg;
    } else
      *asStr = NULL;

int main (int argc, char ** argv)
  char *asStr;
  long asInt;

  int i;
  for (i = 0; i < argc; i++) {
    intOrStr(argv[i], &asStr, &asInt);
    if (asStr)
      printf("argv[%d] is a string: %s\n", i, asStr);
      printf("argv[%d] is an integer: %ld\n", i, asInt);
  return 0;

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

I just want people to know atoi("100J") returns 100 even though "100J" is not a number
int main(int argc,char * argv[])
      char * t;
      int i,is_numeric;
      for(i = 1; i < argc; i++)
            t = argv[i];
            is_numeric = 0;
            while(*t!='\0') {if (isdigit(*t)) is_numeric = 1 ; else {is_numeric = 0;break;} }
                printf("ARGUMENT NUMBER %d :: '%s' is  %s",i,argv[i],is_numeric?"NUMERIC":"ALPHA NUMERIC");       
sorry !!
Plz  replace
>>while(*t!='\0') {if (isdigit(*t)) is_numeric = 1 ; else {is_numeric = 0;break;} }
>>while(*t!='\0') {if (isdigit(*t)) is_numeric = 1 ; else {is_numeric = 0;break;} t++;}
Kent OlsenData Warehouse Architect / DBACommented:

Hi aomega,

I like guitardude101's answer.  It also covers the case where the string represents a negative number.  For whatever it's worth, here's another variation:

int isintvalue(char* str)
      if (*str == '-' || *str == '+')
      while (isdigit(*str))
      return *str == '\0';

Though none of the examples in this thread handle hex constants.  ;)
> I just want people to know atoi("100J") returns 100 even though "100J" is not a number
> [guitardude101's answer] covers the case where the string represents a negative number.
> Though none of the examples in this thread handle hex constants.

That is why I used strtol().  The if (*endp) test checks for the "100J" case.
strtol() handles leading +/- and whitespace.  strtol() with a radix of 0 (third param)
will even understand numbers in octal or hex.  Set the radix to "10" if you don't
want that behavior.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.