{"id":2641,"date":"2017-08-12T00:01:12","date_gmt":"2017-08-12T07:01:12","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2641"},"modified":"2017-08-05T09:51:30","modified_gmt":"2017-08-05T16:51:30","slug":"null-versus-empty-strings","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2641","title":{"rendered":"Null Versus Empty Strings"},"content":{"rendered":"<p>The C language offers some delicious predicaments you may not find in other languages. Many of these odd situations involve variables, which are strongly typed in other languages. As a case in point, consider the difference between an empty string and a null string.<br \/>\n<!--more--><br \/>\nIf you&#8217;ve only coded C, the question arises, &#8220;Aren&#8217;t an empty string and a null string the same thing?&#8221; They aren&#8217;t. In other programming languages, they&#8217;re definitely different.<\/p>\n<p>A null string has no values. It&#8217;s an empty <em>char<\/em> array, one that hasn&#8217;t been assigned any elements. The string exists in memory, so it&#8217;s not a <code>NULL<\/code> pointer. It&#8217;s just absent any elements.<\/p>\n<p>An empty string has a single element, the null character, <code>'\\0'<\/code>. That&#8217;s still a character, and the string has a length of zero, but it&#8217;s not the same as a null string, which has no characters at all.<\/p>\n<p>As an example:<\/p>\n<p><code>char name[32];<\/code><\/p>\n<p>The <code>name<\/code> array is null string. That doesn&#8217;t mean that it has a null character (<code>'\\0'<\/code>) at element zero. It means that <code>name<\/code> hasn&#8217;t been assigned a value. Had it been assigned a value, and the contents removed or replaced by a null character at element zero, then it becomes an empty string.<\/p>\n<p>So how can you tell the difference between an empty string and a null string?<\/p>\n<p>You must compare the two: Create an empty string or null string as a sample, then use the <em>strcmp()<\/em> function to compare them. A null string compares to another null string, and an empty string to an empty one.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main()\r\n{\r\n    char empty[5] = { '\\0' };\r\n    char null[5];\r\n\r\n    if( strcmp(empty,null)==0 )\r\n        puts(\"Strings are the same\");\r\n    else\r\n        puts(\"Strings are not the same\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Array <code>empty[]<\/code> at Line 6 is created as an empty string. It has a single element, a null character. The length of this string is zero, though it has one element.<\/p>\n<p>Array <code>null[]<\/code> at Line 7 is a null string. It&#8217;s declared, allocated 5 elements, but never assigned any values.<\/p>\n<p>The <em>strcmp()<\/em> function at Line 9 compares the two <em>char<\/em> arrays.<\/p>\n<p>Here&#8217;s the program&#8217;s output:<\/p>\n<pre><code>Strings are not the same<\/code><\/pre>\n<p>You might think that you could use the <em>strlen()<\/em> function to obtain the string lengths and compare them, but that logic doesn&#8217;t apply: <em>strlen()<\/em> returns zero for the <code>empty[]<\/code> array, even though that array has a single element. The string is empty.<\/p>\n<p>The <em>strlen()<\/em> returns an unpredictable value for the <code>null[]<\/code> array because it&#8217;s not initialized. Therefore the string length is wherever the next random null character is found in memory. By the way, this function wouldn&#8217;t work most other programming languages where variables must be initialized before you assault them with a function.<\/p>\n<p>Though C doesn&#8217;t really have official typing for empty and null strings, you can still tell the difference between them: compare. That&#8217;s the secret, should the need ever arise.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The concept of a null or empty string isn&#8217;t rooted in the C language, but you can use C to determine whether a string is empty or null. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2641\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2641","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2641","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2641"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2641\/revisions"}],"predecessor-version":[{"id":2653,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2641\/revisions\/2653"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}