NULL - C函数

2026-07-27 17:40:47 情怀礼包

C库宏NULL的值是一个空指针常量。它可以被定义为 ((void*)0), 0 ,0或0L根据编译器厂商。

声明

可能是以下声明为NULL宏取决于编译器。

#define NULL ((char *)0)

or

#define NULL 0L

or

#define NULL 0

参数

NA

返回值

NA

例子

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

#include

#include

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

上一篇:

- C语言标准库

下一篇:

offsetof() - C函数