Home > Backend Development > C++ > body text

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

王林
Release: 2023-09-05 16:25:09
forward
893 people have browsed it

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);
}
Copy after login

Output

File does not exists
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!