How to check if C string is empty

后端 未结 12 1175
一个人的身影
一个人的身影 2021-01-30 19:41

I\'m writing a very small program in C that needs to check if a certain string is empty. For the sake of this question, I\'ve simplified my code:

#include 

        
12条回答
  •  猫巷女王i
    2021-01-30 20:46

    I've written down this macro

    #define IS_EMPTY_STR(X) ( (1 / (sizeof(X[0]) == 1))/*type check*/ && !(X[0])/*content check*/)
    

    so it would be

    while (! IS_EMPTY_STR(url));
    

    The benefit in this macro it that it's type-safe. You'll get a compilation error if put in something other than a pointer to char.

提交回复
热议问题