Home >Backend Development >C++ >How to check if a variable is NULL in C/C++?

How to check if a variable is NULL in C/C++?

王林
王林forward
2023-09-05 16:25:09963browse

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.

Sample code

#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);
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete