Empty string: Difference between revisions

m
Line 426:
=={{header|C}}==
In C the strings are <code>char</code> pointers. A string terminates with the null char (U+0000, <code>'\0'</code>), which is not considered part of the string. Thus an empty string is <code>"\0"</code>, while a null string is a null pointer which points to nothing.
<lang C>/* assign an empty#include <string */.h>
 
/* ... */
 
/* assign an empty string */
const char *str = "";
 
/* to test a null string */
if (str) { ... }
 
/* to test if string is empty */
if (str[0] == '\0') { ... }
 
/* or equivalently use strlen function */
if (strlen(str) == 0) { ... }
strlen will seg fault on NULL pointer, so check first */
if ( (str == NULL) || (strlen(str) == 0)) { ... }
 
/* or compare to a known empty string, same thing. "== 0" means strings are equal */
if (strcmp(str, "") == 0) { ... }