Best way to check if a character array is empty

后端 未结 7 2016
臣服心动
臣服心动 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:36

    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.

提交回复
热议问题