Home > Article > Backend Development > How to check if a variable is NULL in C/C++?
In C or C, there is no special way to compare NULL values. We can use if statement to check if a variable is null.
Here we will see a program. We will try to open a file in read mode that does not exist on the system. So the function will return null value. We can check it using if statement. Please see the code for better understanding.
#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
The above is the detailed content of How to check if a variable is NULL in C/C++?. For more information, please follow other related articles on the PHP Chinese website!