ใน C หรือ C++ ไม่มีเมธอดพิเศษสำหรับเปรียบเทียบค่า NULL ใช้ if statement ตรวจสอบว่าตัวแปรเป็น null หรือไม่
ตัวอย่างโปรแกรม พยายามเปิดไฟล์อ่านที่ไม่มีในระบบ ฟังก์ชันจะคืนค่า null ตรวจสอบด้วย if statement
โค้ดตัวอย่าง
#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