Best way to check if a character array is empty

后端 未结 7 2011
臣服心动
臣服心动 2020-12-13 02:15

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

char text[50];

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

or

if(text[         


        
相关标签:
7条回答
  • 2020-12-13 02:45

    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.

    0 讨论(0)
提交回复
热议问题