
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
How to check if a variable is NULL in C/C++?
In C or C++, there is no special method for comparing NULL values. We can use if statements to check whether a variable is null or not.
Here we will see one program. We will try to open a file in read mode, that is not present in the system. So the function will return null value. We can check it using if statement. See the code for better understanding.
Example 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
- Related Articles
- In MySQL stored procedures, how to check if a local variable is null?
- How to check if a variable is NaN in JavaScript?
- How to check if a variable is boolean in JavaScript?
- How to check if data is NULL in MySQL?
- How to check if a variable is an integer in JavaScript?
- How to check if a variable is an array in JavaScript?
- How to check if a variable exists in JavaScript?
- How to check if type of a variable is string in Python?
- How to check if a C/C++ string is an int?
- How to check if field is null or empty in MySQL?
- How to determine if a variable is 'undefined' or 'null'?
- How to check if a C# list is empty?
- How to check if input is numeric in C++?
- Python - Check if a variable is string
- How to check if any value is Null in a MySQL table single row?

Advertisements