Compare to ZERO

Which are the differences?

if number==‘0’;
if number==0;
if number==NULL;

Which type of variable has to be ‘number’?

Hi Atingenieria,

  1. The statement, (number==‘0’) compares the value contained in the variable “number” with the ascii value of zero. As you are using C programming, you can use any datatype for “number” and use the typecasting to cast it to character to perform the comparison.
  2. The statement, (number==0), compares, the value contained in the variable “number” with zero. In this case, also, the datatype can be anything and if the compilation gives you problem, you can typecast number to int and then perform the compilation.
  3. The statement, (number==NULL) also compares the value contained in number with 0. (Note that NULL is internally defined as 0). Hence, this is the same as the statement (number==0).

Hope this clarifies.

Best Regards,
Open AT Fan.

Actually, NULL is defined as ((void *)0) (pointer to 0) by ANSI C. Good practice is to only compare with NULL if you are working with pointers.

In C++ NULL can be defined as simply 0 or 0L but not ((void *)0) and should supposedly not be used anyway. I prefer C.