C 库宏 - NULL

C 语言教程 · 2019-02-23 12:58:01

描述

C 库宏 NULL 是一个空指针常量的值。它可以被定义为 ((void*)0), 0 0L,这取决于编译器供应商。

声明

下面是取决于编译器的 NULL 宏的声明。

#define NULL ((char *)0)

或

#define NULL 0L

或

#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("成功打开文件 file.txt\n");
      fclose(fp);
   }

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

假设文件 file.txt 已存在,但是 nofile.txt 不存在。让我们编译并运行上面的程序,这将产生以下结果:

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

点击查看所有 C 语言教程 文章: https://codercto.com/courses/l/17.html

查看所有标签

Out of their Minds

Out of their Minds

Dennis Shasha、Cathy Lazere / Springer / 1998-07-02 / USD 16.00

This best-selling book is now available in an inexpensive softcover format. Imagine living during the Renaissance and being able to interview that eras greatest scientists about their inspirations, di......一起来看看 《Out of their Minds》 这本书的介绍吧!

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具