{"id":26960,"date":"2022-12-21T12:50:57","date_gmt":"2022-12-21T07:20:57","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/"},"modified":"2022-12-21T12:50:57","modified_gmt":"2022-12-21T07:20:57","slug":"solved-what-is-the-output-of-the-following-code-in-c-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/","title":{"rendered":"[Solved] What is the output of the following code in C? [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-51910997\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"51910997\" data-parentid=\"51909824\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>&#8220;How can a pointer to a character data type can hold a string data type?&#8221;  Well, it&#8217;s partly true that in C, type &#8216;pointer to char&#8217; <em>is<\/em> the string type.  Any function that operates on strings (including <code>printf<\/code>) will be found to accept these strings via parameters of type <code>char *<\/code>.<\/p>\n<p>&#8220;How can <code>printf()<\/code> print a string when no apparent quotation marks are used?&#8221;  There&#8217;s no rule that says you need quotation marks to have a string!  That thing with quotation marks is a <em>string constant<\/em> or <em>string literal<\/em>, and it&#8217;s one way to get a string into your program, but it&#8217;s not at all the only way.  There are lots of ways to construct (and manipulate, and modify) strings that don&#8217;t involve any quotation marks at all.<\/p>\n<p>Let&#8217;s draw some pictures representing your code:<\/p>\n<pre><code>char *p;\n<\/code><\/pre>\n<p><code>p<\/code> is a pointer to <code>char<\/code>, but as you correctly note, it doesn&#8217;t point anywhere yet.  We can represent it graphically like this:<\/p>\n<pre><code>   +-----------+\np: |    ???    |\n   +-----------+\n<\/code><\/pre>\n<p>Next you set <code>p<\/code> to point somewhere:<\/p>\n<pre><code>p = \"%d\";\n<\/code><\/pre>\n<p>This allocates the string <code>\"%d\"<\/code> somewhere (it doesn&#8217;t matter where), and sets <code>p<\/code> to point to it:<\/p>\n<pre><code>   +---+---+---+\n   | % | d |\\0 |\n   +---+---+---+\n     ^\n     |\n      \\\n       \\\n        \\\n         |\n   +-----|-----+\np: |     *     |\n   +-----------+\n<\/code><\/pre>\n<p>Next, you start incrementing <code>p<\/code>:<\/p>\n<pre><code>p++;\n<\/code><\/pre>\n<p>As you said, this makes <code>p<\/code> point one past where it used to, to the second character of the string:<\/p>\n<pre><code>   +---+---+---+\n   | % | d |\\0 |\n   +---+---+---+\n         ^\n         |\n         |\n         |\n         |\n         |\n   +-----|-----+\np: |     *     |\n   +-----------+\n<\/code><\/pre>\n<p>Next,<\/p>\n<pre><code>p++;\n<\/code><\/pre>\n<p>Now we have:<\/p>\n<pre><code>   +---+---+---+\n   | % | d |\\0 |\n   +---+---+---+\n             ^\n             |\n            \/\n           \/\n          \/\n         |\n   +-----|-----+\np: |     *     |\n   +-----------+\n<\/code><\/pre>\n<p>Next you called <code>printf<\/code>, but somewhat strangely:<\/p>\n<pre><code>printf(p-2,23);\n<\/code><\/pre>\n<p>The key to that is the expression <code>p-2<\/code>.  If <code>p<\/code> points to the third character in the string, then <code>p-2<\/code> points to the first character in the string:<\/p>\n<pre><code>         +---+---+---+\n         | % | d |\\0 |\n         +---+---+---+\n           ^       ^\n      +----|----+  |\n p-2: |    *    | \/\n      +---------+\/\n                \/\n               |\n         +-----|-----+\n      p: |     *     |\n         +-----------+\n<\/code><\/pre>\n<p>And that pointer, <code>p-2<\/code>, is more or less the same pointer that <code>printf<\/code> would have received if you&#8217;re more conventionally called <code>printf(\"%d\", 23)<\/code>.<\/p>\n<p>Now, if you thought <code>printf<\/code> received a string, it may surprise you to hear that <code>printf<\/code> is happy to receive a <code>char *<\/code> instead \u2014 and that in fact it <em>always<\/em> receives a <code>char *<\/code>.  If this is surprising, ask yourself, what did you thing <code>printf<\/code> <em>did<\/em> receive, if not a pointer to <code>char<\/code>?<\/p>\n<p>Strictly speaking, a string in C is an array of characters (terminated with the <code>'\\0'<\/code> character).  But there&#8217;s this super-important secret fact about C, which if you haven&#8217;t encountered yet you will real soon (because it&#8217;s really not a secret at all):<\/p>\n<blockquote>\n<p>You can&#8217;t do much with arrays in C.  Whenever you mention an array in an expression in C, whenever it looks like you&#8217;re trying to do something with the value of the array, what you get is a pointer to the array&#8217;s first element.<\/p>\n<\/blockquote>\n<p>That pointer is pretty much the &#8220;value&#8221; of the array.  Due to the way pointer arithmetic works, you can use pointers to access arrays pretty much transparently (almost as if the pointer <em>was<\/em> the array, but of course it&#8217;s not).  And this all applies perfectly well to arrays of (and pointers to) characters, as well.<\/p>\n<p>So since a string in C is an array of characters, when you write<\/p>\n<pre><code>\"%d\"\n<\/code><\/pre>\n<p>that&#8217;s an array of three characters.  But when you use it in an expression, what you get is a pointer to the array&#8217;s first element.  For example, if you write<\/p>\n<pre><code>printf(\"%d\", 23);\n<\/code><\/pre>\n<p>you&#8217;ve got an array of characters, and you&#8217;re mentioning it in an expression, so what you get is a pointer to the array&#8217;s first element, and that&#8217;s what gets passed to <code>printf<\/code>.<\/p>\n<p>If we said<\/p>\n<pre><code>char *p = \"%d\";\nprintf(p, 23);\n<\/code><\/pre>\n<p>we&#8217;ve done the same thing, just a bit more explicitly: again, we&#8217;ve mentioned the array <code>\"%d\"<\/code> in an expression, so what we get as its value is a pointer to its first element, so that&#8217;s the pointer that&#8217;s used to initialize the pointer variable <code>p<\/code>, and that&#8217;s the pointer that gets passed as the first argument to <code>printf<\/code>, so <code>printf<\/code> is happy.<\/p>\n<p>Up above, I said &#8220;it&#8217;s partly true that in C, type &#8216;pointer to char&#8217; is the string type&#8221;.  Later I said that &#8220;a string in C is an array of characters&#8221;.  So which is it?  An array or a pointer?  Strictly speaking, a string is an array of characters.  But like all arrays, we can&#8217;t do much with an array of characters, and when we try, what we get is a pointer to the first element.  So most of the time, strings in C are accessed and manipulated and modified via pointers to characters.  All functions that operate on strings (including <code>printf<\/code>) actually receive pointers to char, pointing at the strings they&#8217;ll manipulate.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved What is the output of the following code in C? [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] &#8220;How can a pointer to a character data type can hold a string data type?&#8221; Well, it&#8217;s partly true that in C, type &#8216;pointer to char&#8217; is the string type. Any function that operates on strings (including printf) will be found to accept these strings via parameters of type char *. &#8220;How can printf() &#8230; <a title=\"[Solved] What is the output of the following code in C? [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/\" aria-label=\"More on [Solved] What is the output of the following code in C? [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,558,712,362],"class_list":["post-26960","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-character","tag-pointers","tag-string"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] What is the output of the following code in C? [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] What is the output of the following code in C? [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] &#8220;How can a pointer to a character data type can hold a string data type?&#8221; Well, it&#8217;s partly true that in C, type &#8216;pointer to char&#8217; is the string type. Any function that operates on strings (including printf) will be found to accept these strings via parameters of type char *. &#8220;How can printf() ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-21T07:20:57+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] What is the output of the following code in C? [closed]\",\"datePublished\":\"2022-12-21T07:20:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/\"},\"wordCount\":739,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"character\",\"pointers\",\"string\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/\",\"name\":\"[Solved] What is the output of the following code in C? [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-21T07:20:57+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] What is the output of the following code in C? [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] What is the output of the following code in C? [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] What is the output of the following code in C? [closed] - JassWeb","og_description":"[ad_1] &#8220;How can a pointer to a character data type can hold a string data type?&#8221; Well, it&#8217;s partly true that in C, type &#8216;pointer to char&#8217; is the string type. Any function that operates on strings (including printf) will be found to accept these strings via parameters of type char *. &#8220;How can printf() ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/","og_site_name":"JassWeb","article_published_time":"2022-12-21T07:20:57+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] What is the output of the following code in C? [closed]","datePublished":"2022-12-21T07:20:57+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/"},"wordCount":739,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","character","pointers","string"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/","name":"[Solved] What is the output of the following code in C? [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-21T07:20:57+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-what-is-the-output-of-the-following-code-in-c-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] What is the output of the following code in C? [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/26960","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=26960"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/26960\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=26960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=26960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=26960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}