{"id":2661,"date":"2024-03-05T12:19:56","date_gmt":"2024-03-05T12:19:56","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2661"},"modified":"2024-05-10T11:09:32","modified_gmt":"2024-05-10T11:09:32","slug":"single-quoted-and-double-quoted-declaration-of-char-array","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/single-quoted-and-double-quoted-declaration-of-char-array\/","title":{"rendered":"What Is The Difference Between Single Quoted And Double Quoted Declaration Of Char Array?"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#example-1-double-quoted-declaration-of-char-array\">Example 1- Double Quoted Declaration of Char Array<\/a><\/li><li ><a href=\"#example-2-double-quoted-declaration-of-char-array\">Example 2-Double Quoted Declaration of Char Array<\/a><\/li><li ><a href=\"#example-3-single-quoted-declaration-of-char-array\">Example 3- Single Quoted Declaration of char Array<\/a><\/li><li ><a href=\"#faq-what-is-the-difference-between-single-quoted-and-double-quoted-declaration-of-char-array\">FAQ- What Is The Difference Between Single Quoted And Double Quoted Declaration Of Char Array?<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-1-double-quoted-declaration-of-char-array\">Example 1- Double Quoted Declaration of Char Array<\/h2>\n\n\n\n<p>In C and C++, when a character array is initialized with a double-quoted string and the array size is not specified, the compiler automatically allocates one extra space for the string terminator <code>'\\0'<\/code>. This is essential for correctly representing and manipulating strings in C and C++. This program will print 6 as output.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n#include&lt;stdio.h&gt; \nint main() \n{ \n  \/\/ size of arr&#91;] is 6 as it is '\\0' terminated  \n  char arr&#91;] = \"Hello\"; \n  \n  printf(\"%lu\", sizeof(arr)); \n  \n  return 0; \n} <\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>6<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-2-double-quoted-declaration-of-char-array\">Example 2-Double Quoted Declaration of Char Array<\/h2>\n\n\n\n<p>In C, specifying an array size of 5 in the above program is valid, and the program will work without any warning or error. The compiler will allocate exactly 5 characters for the array to hold &#8216;H&#8217;, &#8216;e&#8217;, &#8216;l&#8217;, &#8216;l&#8217;, &#8216;o&#8217; without the null terminator. The <code>sizeof<\/code> operator will correctly return 5 in this case.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\/\/ Works in C, but compilation error in C++ \n#include&lt;stdio.h&gt; \nint main() \n{ \n  \/\/ arr&#91;] is not terminated with '\\0' \n  \/\/ and its size is 5 \n  char arr&#91;5] = \"Hello\";  \n    \n  printf(\"%lu\", sizeof(arr)); \n    \n  return 0; \n} <\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>5<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-3-single-quoted-declaration-of-char-array\">Example 3- Single Quoted Declaration of char Array<\/h2>\n\n\n\n<p>When a character array is initialized with a comma-separated list of characters, and the array size is not specified, the compiler does not automatically create extra space for the string terminator <code>'\\0'<\/code>. Instead, the compiler allocates only enough space for the characters provided in the initialization list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;stdio.h&gt; \nint main() \n{ \n  \/\/ arr&#91;] is not terminated with '\\0'  \n  \/\/ and its size is 5  \n  char arr&#91;]= {'h', 'e', 'l', 'l', 'o'};  \n  \n  printf(\"%lu\", sizeof(arr)); \n  \n  return 0; \n} <\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>5<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-what-is-the-difference-between-single-quoted-and-double-quoted-declaration-of-char-array\">FAQ- What Is The Difference Between Single Quoted And Double Quoted Declaration Of Char Array?<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1697092482539\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is the difference between single-quoted and double-quoted declarations of char array in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C and C++, single quotes (<code>'<\/code>) represent single characters and double quotes (&#8220;) represent string literals. When you use a string literal &#8216;x&#8217;, it&#8217;s a two-character array containing the character <code>'x'<\/code> and a null terminator <code>'\\0'<\/code>.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697092727708\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is the difference between single and double quotes in programming?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C and C++, you can include single quotes in double-quoted strings and double quotes in single-quoted strings without escaping them. If you want to include the same type of quotation marks within a string, you need to escape them with a backslash.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697092736202\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is the difference between char * array and char array in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C and C++:<br \/><code>char[]<\/code> is a fixed-size character array with direct indexing.<br \/><code>char*<\/code> is a character pointer that references a memory location, providing flexibility in size and data access.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Example 1- Double Quoted Declaration of Char Array In C and C++, when a character array is initialized with a double-quoted string and the array size is not specified, the compiler automatically allocates one extra space for the string terminator &#8216;\\0&#8217;. This is essential for correctly representing and manipulating strings in C and C++. This &#8230; <a title=\"What Is The Difference Between Single Quoted And Double Quoted Declaration Of Char Array?\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/single-quoted-and-double-quoted-declaration-of-char-array\/\" aria-label=\"More on What Is The Difference Between Single Quoted And Double Quoted Declaration Of Char Array?\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":2663,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[454],"class_list":["post-2661","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-what-is-the-difference-between-single-quoted-and-double-quoted-declaration-of-char-array","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2661","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2661"}],"version-history":[{"count":8,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2661\/revisions"}],"predecessor-version":[{"id":10720,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2661\/revisions\/10720"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/2663"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}