Null Versus Empty Strings

The C language offers some delicious predicaments you may not find in other languages. Many of these odd situations involve variables, which are strongly typed in other languages. As a case in point, consider the difference between an empty string and a null string.

If you’ve only coded C, the question arises, “Aren’t an empty string and a null string the same thing?” They aren’t. In other programming languages, they’re definitely different.

A null string has no values. It’s an empty char array, one that hasn’t been assigned any elements. The string exists in memory, so it’s not a NULL pointer. It’s just absent any elements.

An empty string has a single element, the null character, '\0'. That’s still a character, and the string has a length of zero, but it’s not the same as a null string, which has no characters at all.

As an example:

char name[32];

The name array is null string. That doesn’t mean that it has a null character ('\0') at element zero. It means that name hasn’t been assigned a value. Had it been assigned a value, and the contents removed or replaced by a null character at element zero, then it becomes an empty string.

So how can you tell the difference between an empty string and a null string?

You must compare the two: Create an empty string or null string as a sample, then use the strcmp() function to compare them. A null string compares to another null string, and an empty string to an empty one.

#include <stdio.h>
#include <string.h>

int main()
{
    char empty[5] = { '\0' };
    char null[5];

    if( strcmp(empty,null)==0 )
        puts("Strings are the same");
    else
        puts("Strings are not the same");

    return(0);
}

Array empty[] at Line 6 is created as an empty string. It has a single element, a null character. The length of this string is zero, though it has one element.

Array null[] at Line 7 is a null string. It’s declared, allocated 5 elements, but never assigned any values.

The strcmp() function at Line 9 compares the two char arrays.

Here’s the program’s output:

Strings are not the same

You might think that you could use the strlen() function to obtain the string lengths and compare them, but that logic doesn’t apply: strlen() returns zero for the empty[] array, even though that array has a single element. The string is empty.

The strlen() returns an unpredictable value for the null[] array because it’s not initialized. Therefore the string length is wherever the next random null character is found in memory. By the way, this function wouldn’t work most other programming languages where variables must be initialized before you assault them with a function.

Though C doesn’t really have official typing for empty and null strings, you can still tell the difference between them: compare. That’s the secret, should the need ever arise.

Leave a Reply