如何在 C/C++ 中檢查變數是否為 NULL?”
在 C 或 C++ 中,沒有特殊方法來比較 NULL 值。我們可以使用 if 語句來檢查變數是否為 null。
這裡我們會看到一個程式。我們將嘗試以讀取模式開啟一個不在系統中的檔案。因此,該函式將返回 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP