
在 C 或 C++ 中,没有特殊的方法来比较 NULL 值。我们可以使用 if 语句来检查变量是否为 null。
这里我们将看到一个程序。我们将尝试以读取模式打开系统中不存在的文件。所以该函数将返回空值。我们可以使用 if 语句来检查它。请参阅代码以更好地理解。
#include <stdio.h>
main() {
//try to open a file in read mode, which is not present
FILE *fp;
fp = fopen("hello.txt", "r");
if(fp == NULL)
printf("File does not exists");
fclose(fp);
}File does not exists
以上就是如何在C/C++中检查变量是否为NULL?的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号