tags:

views:

243

answers:

7

NULL in C programming can anyone tell me how NULL is handled in C ? and output of program is 3 how with NULL concept ?

#include "stdio.h"

void main() {
    int i;
    static int count;
    for(i=NULL;i<=5;) {
        count++;
        i+=2;
    }

    printf("%d",count);
}
+1  A: 

NULL is should be synonymous with 0. It's more correctly used to indicate a null pointer.

In this case the code will actually be:

for (i = 0; i <= 5)
{
    count++;
    i += 2;
}
ChrisF
correction, it depends on the standard library, not the compiler. The language specs for both C and C++ are very clear about what `NULL` may be.
Evan Teran
@Evan - thanks, it's a while since I did C in anger. Answer updated.
ChrisF
+14  A: 

For C, "NULL" is traditionally defined to be (void *)0 - in other words, it's a pointer alias to address 0. For C++, "NULL" is typically defined to be "0". The problem with NULL in C++ and C is that it's not type safe - you can build bizarre constructs like the one you included in your code sample.

For C++, the language designers fixed this in C++0x by adding a new "nullptr" type which is implicitly convertable to any pointer type but which cannot be converted to an integer type.

Larry Osterman
NULL is not an alias to address 0 (which could be a valid address). It's an integer *constant* zero in a pointer context.
mpez0
There is no concept of "pointer zero" because pointer addition does not have an identity element. `NULL` is a macro which expands to a canonical invalid pointer, which happens not to be strongly typed.
Potatoswatter
+2  A: 

NULL has most of its meaning when dealing with pointers. When dealing with integers, you would be better off using simply zero.

Strictly speaking, NULL is simply the value zero with a fancy name, but the most important part about it is indeed its fancy name. It exists because it's less ambiguous to write int* p = NULL; than int* p = 0;. Since we know NULL is a pointer, we're sure that I really meant p to be a pointer.

So, when you deal with pointers and want to represent the address 0, use NULL. And when you deal with numbers and want to represent the number 0, use 0. (In your example, you should use 0 instead of NULL.)

zneak
A: 

NULL is defined as 0 by C. In your program, it counts from 0 to 5 (counting every 2nd number). That's why count is 3 (i = 0, 2, 4).

NULL shouldn't be used this way. NULL should be used with pointers.

Starkey
+1  A: 

NULL is just a macro defined in stdio.h or a file that stdio includes. It can variously be defined as some variation of zero.

If you run your code through the C pre-processor (usually cc -E) you can see what it translates to on your implemnentation:

void main(){
    int i;
    static int count;
    for(i=((void *)0); i<=5 ;){
       count++;
       i+=2;
    }
    printf("%d",count);
}

which is not only an unnecessary use of NULL but is wildly un-idiomatic C code, more ordinary would be:

int main(){
    int i;
    int count = 0;
    for(i = 0; i <= 5; i += 2){
       count++;
    }
    printf("%d\n",count);
    return 0;
}
msw
bt output is 3 .. how ?
mr_eclair
@me_eclair: it's `3` because you are counting to `5` by `2`'s look at the last part of the `for` loop. It is adding `2` each iteration, so `i` goes like this: `0` -> `2` -> `4` -> `6`. The loop is not entered when `i == 6` so the body of the loop is only run 3 times.
Evan Teran
NULL is defined in <stddef.h>. You don't need to include <stdio.h>. In particular, you can use NULL in a freestanding environment without including <stdio.h> (which ought to be unavailable).
Alek
A: 

The type of NULL and the type of i are different, but C forgives you :)

C is (mostly) not a "type-safe" language. I mean: it lets you mix types which other languages would complain.

With C you can add chars to doubles, you can multiply chars and ints, ...

What you are doing is assigning a null pointer constant (of type void *) to an int. C allows that, but you need to be very careful when mixing types like this.

The result of assigning the null pointer constant to an int is the same as assigning 0 to it. So you start your loop with i being 0.

A static auto variable, in the absence of an initializer, is initialized to 0. This happens to the variable count in your program.

So, the loop goes first (count becomes 1) with i being 0, then i becomes 2.
Now the loop goes (count becomes 2) with i being 2, then i becomes 4.
Now the loop goes (count becomes 3) with i being 4, then i becomes 6.
And the loop terminates ... and you print the value of count.


Notes

  • to be Standard compliant main should be declared int main(void)
  • you should output a newline after every complete line ( printf("%d\n", count); )
  • and you should return 0; to indicate successful completion of your program to the Operating System
pmg
Thank U .. pmg problem solved.
mr_eclair
A: 

Don't confuse the idea of a NULL pointer in c with that of a nullable type or quantities in some dynamic languages. There are similar, but have distinct uses and meaning.

In c the concept of NULL is only used natively in the context of pointer where it is a pointer to a known invalid memory location (often, but not always, represented by zero).

This is not (quite) the same as a type or variable which can represent non-setness. Note that types like this can be defined-and-managed by the user in c, but they are not supplied by the language.

dmckee