73

Which is the most reliable way to check if a character array is empty?

char text[50];

if(strlen(text) == 0) {}

or

if(text[0] == '\0') {}

or do i need to do

 memset(text, 0, sizeof(text));
 if(strlen(text) == 0) {}

Whats the most efficient way to go about this?

ZPS
  • 1,566
  • 4
  • 16
  • 20
  • 1
    So you're wondering if you need to test for null or if you need to set the string to all 0s and then test for 0? – Jeremy L Nov 25 '09 at 00:52
  • Yes, I wasn't sure if I could test a string to be NULL if I just declared it and did not do anything with it first. – ZPS Nov 28 '09 at 05:45

7 Answers7

91

Given this code:

char text[50];
if(strlen(text) == 0) {}

Followed by a question about this code:

 memset(text, 0, sizeof(text));
 if(strlen(text) == 0) {}

I smell confusion. Specifically, in this case:

char text[50];
if(strlen(text) == 0) {}

... the contents of text[] will be uninitialized and undefined. Thus, strlen(text) will return an undefined result.

The easiest/fastest way to ensure that a C string is initialized to the empty string is to simply set the first byte to 0.

char text[50];
text[0] = 0;

From then, both strlen(text) and the very-fast-but-not-as-straightforward (text[0] == 0) tests will both detect the empty string.

waldyrious
  • 3,683
  • 4
  • 33
  • 41
bbum
  • 162,346
  • 23
  • 271
  • 359
  • 1
    I structured that badly, I meant for the memset(text, 0, sizeof(text)); to come immediately after char text[50]; because I was not sure if it was bad practice to strlen a char array before it was assigned anything. – ZPS Nov 25 '09 at 00:24
  • 1
    It certainly is a bad idea to strlen an array before it is assigned - strlen will advance through memory until it reaches a 0 byte, which may be well beyond the end of the array. – Graeme Perrow Nov 25 '09 at 01:22
22

Depends on whether or not your array is holding a null-terminated string. If so, then

if(text[0] == '\0') {}

should be sufficient.

Edit: Another method would be...

if (strcmp(text, "") == 0)

which is potentially less efficient but clearly expresses your intent.

Parappa
  • 7,566
  • 3
  • 34
  • 38
8

This will work to find if a character array is empty. It probably is also the fastest.

if(text[0] == '\0') {}

This will also be fast if the text array is empty. If it contains characters it needs to count all the characters in it first.

if(strlen(text) == 0) {}
Peter Stuifzand
  • 5,084
  • 1
  • 23
  • 28
4

The second method would almost certainly be the fastest way to test whether a null-terminated string is empty, since it involves one read and one comparison. There's certainly nothing wrong with this approach in this case, so you may as well use it.

The third method doesn't check whether a character array is empty; it ensures that a character array is empty.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
4

The second one is fastest. Using strlen will be close if the string is indeed empty, but strlen will always iterate through every character of the string, so if it is not empty, it will do much more work than you need it to.

As James mentioned, the third option wipes the string out before checking, so the check will always succeed but it will be meaningless.

Graeme Perrow
  • 56,086
  • 21
  • 82
  • 121
2
if (text[0] == '\0')
{
    /* Code... */
}

Use this if you're coding for micro-controllers with little space on flash and/or RAM. You will waste a lot more flash using strlen than checking the first byte.

The above example is the fastest and less computation is required.

Ed The ''Pro''
  • 875
  • 10
  • 22
Tony
  • 21
  • 1
0
if (!*text) {}  

The above dereferences the pointer 'text' and checks to see if it's zero. alternatively:

if (*text == 0) {} 
fja0568
  • 53
  • 4