当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


C语言 宏 NULL()用法及代码示例


描述

C 库宏NULL是空指针常量的值。它可以定义为((void*)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\n");
      fclose(fp);
   }

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

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

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

相关用法


注:本文由纯净天空筛选整理自 C library macro - NULL()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。