目录

NULL

描述 (Description)

C库Macro NULL是空指针常量的值。 它可以定义为((void*)0), 00L具体取决于编译器供应商。

声明 (Declaration)

以下可能是NULL宏的声明,具体取决于编译器。

#define NULL ((char *)0)
or
#define NULL 0L
or
#define NULL 0

参数 (Parameters)

  • NA

返回值 (Return Value)

  • NA

例子 (Example)

以下示例显示了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\n");
      fclose(fp);
   }
   fp = fopen("nofile.txt", "r");
   if( fp == NULL ) {
      printf("Could not open file nofile.txt\n");
   }
   return(0);
}

假设我们有一个现有的文件file.txt但是nofile.txt不存在。 让我们编译并运行上面的程序,它将产生以下结果 -

Opend file file.txt successfully
Could not open file nofile.txt
↑回到顶部↑
WIKI教程 @2018