4

In the current code base I'm looking at there's , at least, these:

  • if(strlen(str) == 0)
  • if(str[0] != 0)
  • if(!*str)

And similar variants for it being empty/not empty . The first one reads better, but might waste time (which might nor might not matter). And I suppose one could make a #define STR_EMPTY(str) (*(str) == 0) macro.

But anyway, is there a commonly agreed upon way to check if a string is (not) empty in C ?

3
  • If the value of str is not known before hand (in terms of if it's null or not), two and three should really be combined. (You probably know that, just figured I should throw it out there.) Commented Mar 7, 2012 at 21:56
  • 4
    I utterly despise macros like that. Why make someone go look up a macro to see what it does when the code it wraps is perfectly clear on its own? Commented Mar 7, 2012 at 21:57
  • there's more than one wrong way of checking if a string is empty; defining a macro helps to guarantee other programmers editing the code keep doing it right. Commented Jul 2, 2023 at 23:00

5 Answers 5

4

No. I've seen all three. Personally, I use

if (!str[0])

Using strlen is a waste of processor time and not safe unless you can guarantee the string is terminated.

Sign up to request clarification or add additional context in comments.

14 Comments

Well, if it's not terminated, your method doesn't work either. In fact, if the string isn't terminated, it can never be empty, since by definition the empty string consists of only the terminating character.
The question is determining if the string is empty, not whether it's terminated.
If it is not terminated, it's not a string.
@CareyGregory The term "string" implies that it is null-terminated. So we shouldn't have to worry about whether the "string" is null-terminated when using strlen, but the unnecessary overhead of going through the entire set of characters rather than checking just the first one.
@CareyGregory Why does strlen() from the "standard C library" assume null-terminated strings if it's not part of the language? We can argue semantics (language vs. library, etc.), but anyone uttering the word "string" in the context of C means null-terminated strings.
|
3
  1. if(strlen(str) == 0) -> it will crash if str==NULL (you need to check it before you use strlen)
  2. if(str[0] != 0) -> the same as above
  3. if(!*str) -> this is actually doing the same thing as 2)

To summarise: if you want to do it safely:

if(!str || !(*str)) { /* empty string or NULL */ }

6 Comments

You meant: if(!str || !*str) { ... } ?
I meant exactly what I wrote: what you wrote does exactly the same as I wrote (the paranthesis does not make any difference).
An empty string is not the same as a null pointer. In fact, I would change your || to && to ensure that you only execute the body of the if IF you indeed have a string to begin with.
I did not add anything since I wrote it, sorry, and yes: NULL is not an empty string, but it is too often treated as such in C
Wow, I'm surprised by the arguments here. sirgeorge is completely correct.
|
1

If you already know that str is non-null:

if (!*str) {
    // empty string
}

Otherwise:

if (!str || !*str) {
    // empty string
}

Forget about strlen().

Comments

0

All of your examples are fine. It should be clear from the fact that all three are in your codebase that there is no "commonly agreed upon" method.

3 Comments

You might be right. But that feels like concluding there's no need for a coding style by looking at a codebase that's a mixture of CamelCase and lowercase_with_undercores.
There probably isn't. There's no consensus on camel-case vs. underscores either. Just pick one.
Still, if you see millions of different ways of doing the same thing you would question
0

I would definitely go with strlen(str) == 0, just for clarity.

The function should be implemented something like this:

int strlen(char *s) {
  int len = 0;
  while(*(s++)) len++;
  return len;
}

so there will be no big overhead.

Edit: assuming you are sure that the string is actually zero terminated.

3 Comments

No big overhead? What if the string is kilobytes or even megabytes long? You end up doing 1000s or millions of increments, pointer de-references and comparisons vs. a single comparison.
That's the one I would avoid. Imagine you call it with a 2GB string accidentally. In a tight loop.
Yep, I didn't really think about that, I just assumed that the string was zero most of the time. If you are looking at a lot of data, (str[0] == 0) is definitely faster and safer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.