Latest web development tutorials

C library macro - NULL

C standard library - <stddef.h> C standard library - <stddef.h>

description

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

statement

The following is dependent on the compiler NULL macro statement.

#define NULL ((char *)0)

或

#define NULL 0L

或

#define NULL 0

parameter

  • NA

return value

  • NA

Examples

The following example demonstrates NULL macro usage.

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

int main ()
{
   FILE *fp;

   fp = fopen("file.txt", "r");
   if( fp != NULL ) 
   {
      printf("成功打开文件 file.txt\n");
      fclose(fp);
   }

   fp = fopen("nofile.txt", "r");
   if( fp == NULL ) 
   {
      printf("不能打开文件 nofile.txt\n");
   }
   
   return(0);
}

Assuming that the filefile.txt already exists, but nofile.txtdoes not exist. Let's compile and run the above program, which will result in the following:

成功打开文件 file.txt
不能打开文件 nofile.txt

C standard library - <stddef.h> C standard library - <stddef.h>