{"id":5020,"date":"2021-10-23T00:01:19","date_gmt":"2021-10-23T07:01:19","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5020"},"modified":"2021-10-16T11:26:13","modified_gmt":"2021-10-16T18:26:13","slug":"more-than-one-string-in-a-string","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5020","title":{"rendered":"More Than One String in a String"},"content":{"rendered":"<p>I&#8217;ve seen some oddball constructions in C. You may have as well, especially if you enjoy reading obfuscated C. Yet, the weirdness I just witnessed came from an online C course I was browsing. I&#8217;d never seen it before.<br \/>\n<!--more--><br \/>\nWhen you declare a string, you assign it to an array like this:<\/p>\n<p><code>char string[] = \"Hello!\\n\";<\/code><\/p>\n<p>Array <code>string[]<\/code> contains the string literal, the characters enclosed in double quotes, complete with a terminating null character added automatically by the compiler. This statement represents one way to declare a string. It&#8217;s common.<\/p>\n<p>But what about the construction in this code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_10_23-Lesson.c\" rel=\"noopener\" target=\"_blank\">2021_10_23-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char long_string[] = \r\n        \"Hello!\\n\"\r\n        \"This is a long string\\n\"\r\n        \"How will it format?\\n\";\r\n\r\n    printf(\"%s\",long_string);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Array <code>long_string[]<\/code> contains a single string, though the string itself is defined as three separate string literals, each appearing on a line by itself, each enclosed in double quotes. This type of declaration is valid in C. I&#8217;ve never seen it before.<\/p>\n<p>Check the listing again: You see that are no commas between the strings. The array is a <em>char<\/em> array, not an array of strings (pointers). It&#8217;s just that the long string is split between several lines. The compiler assembles each string, one after the other, with only a single null character at the end.<\/p>\n<p>Here is the output:<\/p>\n<p><code>Hello!<br \/>\nThis is a long string<br \/>\nHow will it format?<\/code><\/p>\n<p>What happens if you scrunch the entire expression onto one line?<\/p>\n<pre class=\"screen\">\r\nchar long_string[] = \"Hello!\\n\" \"This is a long string\\n\" \"How will it format?\\n\";<\/pre>\n<p>The thing still compiles. It runs. The output is the same. After all, what&#8217;s missing between the above statement and the way it appears in the full code earlier is just whitespace. So does this mean a single string can be composed of multiple string chunks?<\/p>\n<p>Yes.<\/p>\n<p>According to the C standard:<\/p>\n<blockquote><p>Adjacent string literal tokens are concatenated.<\/p><\/blockquote>\n<p>This rule doesn&#8217;t mean you can concatenate strings as is done in other languages, such as:<\/p>\n<p><code>string1 = string_a + string_b<\/code><\/p>\n<p>The rule does, however, mean that you can use multiple string literals to declare a single string. I don&#8217;t know what the advantage is for doing so, other than it&#8217;s possible to write a multi-line string without messing up anything. For example:<\/p>\n<pre class=\"screen\">\r\nsonnet18[] = \r\n    \"Shall I compare thee to a summer\u2019s day?\\n\"\r\n    \"Thou art more lovely and more temperate:\\n\"\r\n    \"Rough winds do shake the darling buds of May,\\n\"\r\n    \"And summer\u2019s lease hath all too short a date;\\n\";<\/pre>\n<p>The type of formatting shown above might be easier to type than trying to cram everything into a single, long string literal. Just remember <em>not<\/em> to separate the string literals with commas, and that the entire construction terminates with the required semicolon. It&#8217;s weird, yes, but it works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s possible to have a string that contains multiple strings. Weird, right? Keep reading. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5020\">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":[20,19],"class_list":["post-5020","post","type-post","status-publish","format-standard","hentry","category-main","tag-concatenation","tag-string-literal"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5020","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=5020"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5020\/revisions"}],"predecessor-version":[{"id":5028,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5020\/revisions\/5028"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}