OneBite.Dev - Coding blog in a bite size

check if a string is empty in C

Code snippet on how to check if a string is empty in C

  if (strcmp(string,"") == 0)
  {
     printf("The string is empty");
  }

In this code, strcmp() is used to compare the string given to the empty string (""). If the two strings are equal, the strcmp() function will return a value of 0. Therefore, if the value of strcmp(string,"") is 0, that means the string is empty. If the string is empty, it will print out “The string is empty”. If the string is not empty, strcmp() will return a value that is not equal to 0, so nothing will be printed.

c