Learn how to a build a cloud-first strategyRegister Now


How to discern 0 from NULL

Posted on 2002-07-06
Medium Priority
Last Modified: 2010-04-15

I'm trying to write a simple program that takes 1 and only 1 commandline argument, and it must be an integer.  I couldn't find a function to help me, the only one I found, atoi, returned 3 if the argument was 3dddf (which is just dumb, IMHO).

I decided to write my own function, and it looks like so:

int CmdlineConverter(char * arg)
int i = 0;

  while (arg[i] != NULL)
     if (isdigit((int) arg[i]) != 0)
       return NULL;

     return atoi(arg);

Then, in main, I call this function as CmdlineConverter(argv[1]) and check to make sure it is not == NULL before continuing.

If I am correct, my code above checks to make sure that the argument is all digits, in which case it returns the number, or it returns NULL.  It seems to work, except when the argument is zero.  The isdigit returns OK, so I know it recognizes zero as a digit.  It was my understanding that in C, zero and NULL are different values, but if I have return(0) instead of return atoi(arg), my IF (cmdlineconverter == NULL) code gets executed.  

What the heck am I doing wrong?

Question by:Sketchy

Expert Comment

ID: 7134260
NULL is usually a #define like this

#define NULL ((void *)0)

So in effect NULL is a pointer that points to memory location zero. Assigning a pointer to an integer usually just transfers the pointer value to the integer so in your case you cannot tell the difference between NULL and 0.
It is not considered goo practice to retrun a pointer when your function is declared as returning an integer.

Maybe you could try something like this instead:
int CmdlineConverter(int *result,char * arg)
  if not a valid number
    return -1;
  *result = atoi(arg);
    return 0;

LVL 11

Expert Comment

ID: 7135640
In order to check if your string can be converted to integer without any problem you can use strtol() function.
You need to include <stdlib.h>

Function format:
 long strtol( char *nptr, char **endp, int base )

The strtol function converts 'nptr' to a long. strtol stops reading the string 'nptr' at the first character it cannot recognize as part of a number. This may be the terminating null character, or it may be the first numeric character greater than or equal to 'base'.

If you want it to check 'base' automatically, use 0.
'endp' will return pointer to character that stops scan.
If *endp != NULL then you have a problem in your string.

LVL 11

Accepted Solution

griessh earned 200 total points
ID: 7136122

Your misunderstanding lies in the return value of your function. You are trying to return an integer if there is an integer available ann a Boolean FALSE if there is an error in the input. That doesn't work.
As alraedy explained, NULL is a #define, that means it can be everything, string, integer, char ... For your function NULL and '0' is the same, an integer!

To stay close to what you have:

int CmdlineCheck(char * arg)
int i = 0;

while (arg[i] != NULL)
    if (isdigit((int) arg[i]))
      return (0);

  return (1);


if (CmdlineCheck(arg)) result = atoi(arg)
else { printf "Illegal argument\n"; }
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Expert Comment

ID: 7140388
i dont see why you cant use a simple algorythm.
you can simply get the string,and check each character by its ascii value if its a number or not.
you can find the ascii table in any book or site.
once you establish that all your argument is made up of ints,you can simply use the (int*) to change it.
or you can do the same thing as before,but depleting the char by the difference between the numbers to their ascii value.
as for the ascii values:

Expert Comment

ID: 7140549
Instead of checking for NULL, just check the number of arguments.  

if ( argc != 2 )
    return -1;  /* or print out an error message */

argv[0] will be the program name, and argv[1] will be the integer you asked for.

Also, dimitry gave what I think is the best answer for converting the data to a number and checking to see if the data is valid.  strtol (and strtoul) give you the information you need, and they are part of the Standard.


#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
    long i;
    char **ptr;

    if ( argc != 2 ) {
        printf("Usage: %s integer\n", argv[0]);
     return -1;

    /* Using strtol as dimitry suggested... */
    i = strtol(argv[1], ptr, 0);
    if ( **ptr != '\0' ) {
        printf("ERROR: argument '%s' contained non-numeric characters\n", argv[1]);
        return -2;
    printf("You entered %ld\n", i);

    return 0;

Expert Comment

ID: 7163229
Everyone has given very good answers already which are more to the point of your actual question...

Personally, I don't see what is wrong with allowing your program to be a little more robust and accept an argument like "3ddf" as the integer argument 3 without throwing an error and failing.  Though a warning would be nice.  I would do it something like this:

int main(int argc, char *argv[])
  if (argc<1)
    printf("ERROR: not enough arguments");
    return 1;
  else if (argc > 2)
    printf("WARNING: too many arguments, all but the first will be ignored");

  //convert the argument to an integer
  int nArgument = atoi(argv[1]);

  //now check to see that the number returned by atoi
  //can be turned back into the same string supplied on the command line
  if (strcmp(itoa(nArgument), argv[1]))
    //there was some discrepancy in the format of the number string
    //but we have a value so it isn't critical and we can continue
    printf("WARNING: integer argument poorly formatted, assuming you meant to use %d", nArgument);
    //atoi("some non-number string") returns 0
    //so if the argument is zero, you could assume
    //that no number at all was supplied and fail utterly
    if (nArgument == 0)
       printf("ERROR: actually, it seems that the argument was probably not a number at all, please try again");
       return 1;
  } //end if (argument didn't turn back into string)

  //continue with actual work...

  return 0; //indicates success
} //end main()
LVL 46

Expert Comment

by:Kent Olsen
ID: 9480446
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:
Accept griessh's comment as answer

Please leave any comments here within the next seven days.


Kent (Kdo)
EE Cleanup Volunteer

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question