NULL - C函數

C庫宏NULL的值是一個空指針常量。它可以被定義為 ((void*)0), 0 ,0或0L根據編譯器廠商。

聲明

可能是以下聲明為NULL宏取決於編譯器。

#define NULL ((char *)0)

or

#define NULL 0L

or

#define NULL 0

參數

  • NA

返回值

  • NA

例子

下麵的例子演示了如何使用NULL宏。

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

int main ()
{
   FILE *fp;

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

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

   return(0);
}

假設我們有一個現有的檔file.txt,和一個不存在檔nofile.txt。讓我們編譯和運行上面的程式,這將產生以下結果:

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

上一篇: <stddef.h> - C語言標準庫 下一篇: offsetof() - C函數