在C或C ++中,没有用于比较NULL值的特殊方法。我们可以使用if语句来检查变量是否为null。
在这里,我们将看到一个程序。我们将尝试以读取模式打开文件,该文件在系统中不存在。因此该函数将返回空值。我们可以使用if语句检查它。请参阅代码以获得更好的理解。
#include <stdio.h>
main() {
//尝试以读取模式打开文件,该模式不存在
FILE *fp;
fp = fopen("hello.txt", "r");
if(fp == NULL)
printf("File does not exists");
fclose(fp);
}输出结果
File does not exists