{"id":211,"date":"2013-12-21T00:01:31","date_gmt":"2013-12-21T08:01:31","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=211"},"modified":"2013-12-28T08:43:16","modified_gmt":"2013-12-28T16:43:16","slug":"that-terminating-0","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=211","title":{"rendered":"That Terminating \\0"},"content":{"rendered":"<p>Text stored on a computer consists of various displayable characters and perhaps some control codes, such as a tab (<code>\\t<\/code>) or the newline (<code>\\n<\/code>). The string has a starting point, but determining where and how the string ends differs depending on what is storing or reading the string.<br \/>\n<!--more--><br \/>\nAs an example, one of the old DOS functions used the <code>$<\/code> character to mark the end of a string of text. (The <code>$<\/code> was borrowed from the even older CP\/M operating system.) So if you wanted to display a string of text, you&#8217;d specify text like this:<\/p>\n<p><code>This is a string of text.$<\/code><\/p>\n<p>That meant it was somewhat difficult to display any text that used the <code>$<\/code> character. Therefore, most DOS programmers avoided using that specific function.<\/p>\n<p>Some early programming languages used the newline character to end a string. To display a newline, you had to output the single <code>\\n<\/code> character or, specifically, character code 13 or code 10.<\/p>\n<p>In the C language, the null character (<code>\\0<\/code>) terminates a string. That&#8217;s fine because character code zero is neither a displayable character nor a control code.<\/p>\n<p>A string in C has a starting point, which is an address or memory location. Then each character in the string occupies the next byte in memory, on and on up to the <code>\\0<\/code> character. <em>Ta-da<\/em>! That&#8217;s the end of the string. Simple. Elegant.<\/p>\n<blockquote><p>Keep in mind that the <code>\\0<\/code> is the null character, which simply means ASCII code zero. It&#8217;s not the same as the <code>NULL<\/code> variable, which is a pointer.<\/p><\/blockquote>\n<p>You usually don&#8217;t need to worry about adding the <code>\\0<\/code> at the end of a string; the compiler does that for you. The C library text functions anticipate and deal with the <code>\\0<\/code>. For example:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char *string = \"How long can a string be?\\n\";\r\n    char *ptr;\r\n\r\n    ptr = string;\r\n    while(*ptr)\r\n    {\r\n        putchar(*ptr);\r\n        ptr++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Don&#8217;t shield your eyes from the scary pointer variables! In Line 8, <code>ptr<\/code> is set to the start of the text, the <code>string<\/code> pointer. Then the <em>while<\/em> loop at Line 9 evaluates the character referenced by <code>*ptr<\/code>. As long as that character isn&#8217;t zero, or <code>\\0<\/code>, the loop spins: The value of <code>*ptr<\/code> is displayed by <em>putchar()<\/em> (Line 11) and then the address in <code>ptr<\/code> is incremented (Line 12).<\/p>\n<p>The program&#8217;s output displays the string, one character at a time:<\/p>\n<pre><code>How long can a string be?<\/code><\/pre>\n<p>To demonstrate how the <code>\\0<\/code> works, edit Line 5 to read:<\/p>\n<p><code>char *string = \"How long can a string<span style=\"color:red\">\\0<\/span> be?\\n\";<\/code><\/p>\n<p>(I added the red color to show where to stick the <code>\\0<\/code>.)<\/p>\n<p>Save, compile, and run to see only the first part of the string displayed &#8212; an no newline added:<\/p>\n<pre><code>How long can a string<\/code><\/pre>\n<p>The <code>\\0<\/code> effectively truncates the string.<\/p>\n<p>While the C language functions, as well as the <em>while<\/em> loop in the sample code, work with the <code>\\0<\/code>, you need to remember to add the <code>\\0<\/code> when you write your own string manipulation routines. The <code>\\0<\/code> is required to terminate the string. If you forget it, the result is what I call <em>program puke<\/em> all over the screen.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=472\">Next week&#8217;s Lesson<\/a> further explores the string-creation concept.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A string of text in C is a bunch of letters or codes marching one after another until the <code>\\0<\/code> comes along and ends the party. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=211\">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-211","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\/211","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=211"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/211\/revisions"}],"predecessor-version":[{"id":498,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/211\/revisions\/498"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}