C library Macro - NULL()

Advertisements


Description

The C library Macro NULL is the value of a null pointer constant.It may be defined as ((void*)0), 0 or 0L depending on the compiler vendor.

Declaration

Following may be the declaration for NULL Macro depending on the compiler.

#define NULL ((char *)0)

or

#define NULL 0L

or

#define NULL 0

Parameters

  • NA

Return Value

  • NA

Example

The following example shows the usage of NULL Macro.

#include <stddef.h>
#include <stdio.h>

int main ()
{
   FILE *fp;

   fp = fopen("file.txt", "r");
   if( fp != NULL ) 
   {
      printf("Opend file file.txt successfully\n");
      fclose(fp);
   }

   fp = fopen("nofile.txt", "r");
   if( fp == NULL ) 
   {
      printf("Could not open file nofile.txt\n");
   }
   
   return(0);
}

Assuming we have an existing file file.txt but nofile.txt does not exist. Let us compile and run the above program, this will produce the following result:

Opend file file.txt successfully
Could not open file nofile.txt


Advertisements
Advertisements