A variable cannot be "empty." It always has a value.

You can manually specify a value, which indicates "emptiness" as done with strings: a null byte (with the value 0) indicates the end of the string. Here, it's not emptiness per se but you get what I mean, hopefully.

Because a variable cannot be empty, there is no common NaN value for it; every value is a number, so NaN (not a number) does not make sense.
The floating-point format IEEE 754 supports NaN values but that is only possible because certain values of floating-point numbers have been assigned that meaning.

Answer from cadaniluk on Stack Overflow
Top answer
1 of 4
3

A variable cannot be "empty." It always has a value.

You can manually specify a value, which indicates "emptiness" as done with strings: a null byte (with the value 0) indicates the end of the string. Here, it's not emptiness per se but you get what I mean, hopefully.

Because a variable cannot be empty, there is no common NaN value for it; every value is a number, so NaN (not a number) does not make sense.
The floating-point format IEEE 754 supports NaN values but that is only possible because certain values of floating-point numbers have been assigned that meaning.

2 of 4
2

A variable resides in RAM. RAM is never empty. Each cell has a value. Either the value is known by the programmer or not, depends whether the programmer has initialized that variable or not.

I know two ways of signaling the "emptyness" of a variable. One is to have a value that means "empty", like "0", or MAX_INTEGER, or whatever you like. This works if the logic of your algorithm imposes your variable to have only certain values, so you can know when the value is valid or not. If not, you can say the variable is not valid, or empty.

If your variable can hold any value (within the limits of your type), then a solution may be to use a small struct, like this:

typedef struct
{
  int value;
  int is_empty;
} tVar;

So declaring variable i this way...

tVar i;

You can initialize as empty, like this:

i.is_empty = 1;

So, your program becomes:

int main( void )
{
  tVar someVar = {0,1};  // declaring and initializing it as empty

  ...
  ...
  ...    
  if (!someVar.is_empty) // if someVar is not empty...
  {
     someVar.value = -1;
     someVar.is_empty = 0; // is not empty any more
  }
  else if (someVar.is_empty && (some_other_condition))
  {
     someVar.value = 1;
     someVar.is_empty = 0;
  }

  return 0;
}
🌐
Reddit
reddit.com › r/learnprogramming › how would i check for an empty variable?
r/learnprogramming on Reddit: How would I check for an empty variable?
January 30, 2021 -

I need a bit of help with coding in C/C++. Right now Im working ahead on assignments since its been a slow week for me. I have to create a function that allows the user to input information to a structure within an array for a database of customers. The requirements is that all of the "fields" where the information needs to be entered have to be filled up. So you cant put in name, balance, and address, and leave the last several fields empty. This means that I am dealing with string, int, and double types of information. So far I have:

while (x==0)
{
    cust.customer_number = num_of_cust;
    cout << "Enter new customer name: ";
    cin >> cust.name;
    cout << "Enter new customer phone: ";
    cin >> cust.phone_number;
    cout << "Enter new customer street sddress: ";
    cin >> cust.street_address.street;
    cout << "Enter new customer city: ";
    cin >> cust.street_address.city;
    cout << "Enter new customer state: ";
    cin >> cust.street_address.state;
    cout << "Enter new customer zip code: ";
    cin >> cust.street_address.zip_code;
    cout << "Enter new customer balance: $";
    cin >> cust.balance;
    cout << "Enter new customers last payment date: ";
    cin >> cust.payment_date;
}

I will break the while loop by including an if statement at the end so that if everything is filled in it will increment x to be 1 which would then violate the while condition and break it out of the loop. so something like:

if(cust.name==blank||cust.phone_number==blank||...)
{
x=1;
}

Where I replace the blanks with whatever term I need to indicate an empty variable.

🌐
TutorialsPoint
tutorialspoint.com › how-to-check-if-a-variable-is-null-in-c-cplusplus
How to check if a variable is NULL in C/C++?
We can check whether it is NULL or not using an if statement. In this example, we check whether the file is exist or not using NULL. #include<stdio.h> int 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 ...
🌐
Bytes
bytes.com › home › forum › topic
Checking for an Empty Variable - C / C++
#include <limits.h> int i=INT_MAX; in this case i is initialised with the maximum value of int. You can then test if it has changed. Otherwise you can have two variables one which holds the value and a second which indicates if the first has been changed. Clearly you would need to be careful if your application has multiple threads accessing the same data storage. ... C/C++ variables are not like PHP variables, they have to be declared with a type and because of that they are never "empty".
🌐
LiveCode Forums
forums.livecode.com › viewtopic.php
How to test whether a variable is empty - LiveCode Forums.
But if you "put undeclaredVariable ... You can test whether a variable is empty as long as you use it first, by either putting a value in it at some point, or declaring it ("local tVariable")....
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 162676-check-if-variable-null.html
Check if a variable is null
However, I think you have a very specific use case in mind, in which case you may be able to just have an extra flag variable you use that keeps track of whether variable x has a value yet. You initialize that flag to false, and once x gets a value, you set it to true. You check that flag, rather than x itself, to display the message. ... Another generic way is to initialize x to a sentinal value which you know means "no value entered" for your program. For example, if your program does not require that a user be able to enter the value INT_MIN, then you could initialize x to this value and then use the test x == INT_MIN to determine whether a value has been recorded for x yet.
🌐
Quora
quora.com › How-do-I-check-if-a-string-is-empty-in-C
How to check if a string is empty in C - Quora
Answer (1 of 10): Many of the answers I have seen here are well-intentioned, but also use outdated methods. Assuming that all strings are ANSI formatted is an incomplete answer. Well, first of all, C does not have a string type. A string in C is essentially a set of characters in contiguous mem...
Find elsewhere
🌐
W3Schools
w3schools.com › cpp › ref_string_empty.asp
C++ String empty() Function
string txt1 = "Hello World!"; string txt2 = ""; cout << txt1.empty(); cout << txt2.empty(); Try it Yourself » · The empty() function checks wheter a string is empty or not. It returns 1 if the string is empty, otherwise 0.
🌐
Wikihow
wikihow.com › computers and electronics › software › programming › c programming languages › how to check null in c: 7 steps (with pictures) - wikihow
How to Check Null in C: 7 Steps (with Pictures) - wikiHow
June 9, 2025 - The reverse of this is if (!ptr), which will return TRUE if ptr is FALSE. ... Set a pointer before checking for NULL. One common mistake is to assume that a newly created pointer has a NULL value. This is not true. An unassigned pointer still points to a memory address, just not one that you have specified.
🌐
Landbot
community.landbot.io › c › formulas › this-is-how-you-can-check-if-variable-isempty-with-formulas
This is how you can check if variable ISEMPTY with formulas | Landbot
I decided than when I learn something new I will share with the community here. My goal: I needed to retrieve data from cells on a spreadsheet and, based on whether the cell was empty or not, write it down or move to the next variable. First attempt...
Top answer
1 of 6
30

In C and C++, pointers are inherently unsafe, that is, when you dereference a pointer, it is your own responsibility to make sure it points somewhere valid; this is part of what "manual memory management" is about (as opposed to the automatic memory management schemes implemented in languages like Java, PHP, or the .NET runtime, which won't allow you to create invalid references without considerable effort).

A common solution that catches many errors is to set all pointers that don't point to anything as NULL (or, in correct C++, 0), and checking for that before accessing the pointer. Specifically, it is common practice to initialize all pointers to NULL (unless you already have something to point them at when you declare them), and set them to NULL when you delete or free() them (unless they go out of scope immediately after that). Example (in C, but also valid C++):

void fill_foo(int* foo) {
    *foo = 23; // this will crash and burn if foo is NULL
}

A better version:

void fill_foo(int* foo) {
    if (!foo) { // this is the NULL check
        printf("This is wrong\n");
        return;
    }
    *foo = 23;
}

Without the null check, passing a NULL pointer into this function will cause a segfault, and there is nothing you can do - the OS will simply kill your process and maybe core-dump or pop up a crash report dialog. With the null check in place, you can perform proper error handling and recover gracefully - correct the problem yourself, abort the current operation, write a log entry, notify the user, whatever is appropriate.

2 of 6
8

The other answers pretty much covered your exact question. A null check is made to be sure that the pointer you received actually points to a valid instance of a type (objects, primitives, etc).

I'm going to add my own piece of advice here, though. Avoid null checks. :) Null checks (and other forms of Defensive Programming) clutter code up, and actually make it more error prone than other error-handling techniques.

My favorite technique when it comes to object pointers is to use the Null Object pattern. That means returning a (pointer - or even better, reference to an) empty array or list instead of null, or returning an empty string ("") instead of null, or even the string "0" (or something equivalent to "nothing" in the context) where you expect it to be parsed to an integer.

As a bonus, here's a little something you might not have known about the null pointer, which was (first formally) implemented by C.A.R. Hoare for the Algol W language in 1965.

I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.

🌐
UiPath Community
forum.uipath.com › help › studio
How the variable is check is empty or not - Studio - UiPath Community Forum
January 13, 2022 - I have variable data type as double … how we can check that this variable is empty or not
Top answer
1 of 4
2

The behavior of isInteger should be defined (or explicitly undefined) for any inputs. It's not unreasonable to leave the behavior of isInteger(NULL) undefined, requiring the caller to pass a valid string pointer.

As unwind's answer suggests, isInteger should probably return false (0) for an empty string.

But if you want to check for an empty string yourself, you can check for a length of 0:

char *aux;
aux=getenv("MAX_OUTPUT");
if (aux==NULL || strlen(aux) == 0 || !(isInteger(aux))){
/*code*/
}

Or, equivalently, you can check whether the first (0th) character of the string is the terminating null '\0' character:

char *aux;
aux=getenv("MAX_OUTPUT");
if (aux==NULL || *aux == '\0' || !(isInteger(aux))){
/*code*/
}

*aux can also be written as aux[0].

Note that this doesn't check for, for example, aux containing a single blank: " ", but again, isInteger should probably handle that.

Assuming that isInteger behaves reasonably, returning a true result for any string that looks like an integer and a false result for any string that doesn't (and perhaps with undefined behavior for a null pointer or a pointer that doesn't point to a valid string), then this:

if (aux == NULL || !isInteger(aux)) {
    /* it's not an integer */
}

should be sufficient.

2 of 4
0

If isInteger() doesn't say false for an empty string, I would hesitate to claim that it works. Of course, without the code for isInteger() included, it's hard to be more specific.

I think a function like bool isInteger(const char *str) must return false for both NULL and an empty string, so the aux == NULL test should not even be needed.

🌐
TutorialKart
tutorialkart.com › c-programming › how-to-check-if-a-string-is-empty-in-c
How to Check if a String is Empty in C - Examples
February 21, 2025 - In C, we can check if a string is empty using various methods, such as checking the first character, using the strlen() function, or comparing it with an empty string.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
It is a valid operation in pointer arithmetic to check whether the pointer is NULL. We just have to use isequal to operator ( == ) as shown below: ... The above equation will be true if the pointer is NULL, otherwise, it will be false.
Published   January 10, 2025
🌐
Baeldung
baeldung.com › home › scripting › determine whether a shell variable is empty
Determine Whether a Shell Variable Is Empty | Baeldung on Linux
March 18, 2024 - This is similar to the script we saw earlier. But, in the if condition, we’ve used the -n option to test for string being not empty. The above two methods are the simplest way to check if a variable is empty. But in some cases, we might need to check whether a variable is declared.