Easy Tutorial
❮ C Macro Offsetof C Function Difftime ❯

C Library Macro - NULL

C Standard Library - <stddef.h>

Description

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

Declaration

Below is the declaration for the NULL macro depending on the compiler.

#define NULL ((char *)0)

or

#define NULL 0L

or

#define NULL 0

Parameters

Return Value

Example

The following example demonstrates the usage of the NULL macro.

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

int main ()
{
   FILE *fp;

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

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

   return(0);
}

Assuming the file file.txt exists, but nofile.txt does not. Let's compile and run the above program, which will produce the following result:

Successfully opened file file.txt
Cannot open file nofile.txt

C Standard Library - <stddef.h>

❮ C Macro Offsetof C Function Difftime ❯