Close
Close

explain this code and tell its output?

   prasannakalahastri

include <stdio.h>
    int main()
    {
        char *p = NULL;
        char *q = 0;
        if (p)
            printf(" p ");
        else
            printf("nullp");
        if (q)
            printf("q
");
        else
            printf(" nullq
");
    }

 

  • Studying 'pointers' in c will help you bro.
    - Gurbaj Sidhu
  • Both *p= NULL; and *q = 0 are legal null pointer declaration in C. Since a null pointer doesn't point to any address in the memory, it stores nothing. Hence the else statement is called.
    - smileabhishek

Answers

  •   

    Assigning the value NULL to a pointer variable makes it a NULL pointer. Another way in which we can define a pointer variable as NULL pointer is by assigning it a value 0.

    In your code, both the pointer variables p and q are defined as NULL pointers and so the body of else will be executed in both the cases. Also make the first line of your code as #include <stdio.h>.

    
     


Ask Yours
Post Yours
Write your answer