Best way to check if a character array is empty

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

    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.

提交回复
热议问题