What Is The Difference Between Single Quoted And Double Quoted Declaration Of Char Array?

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 '\0'. This is essential for correctly representing and manipulating strings in C and C++. This program will print 6 as output.


#include<stdio.h> 
int main() 
{ 
  // size of arr[] is 6 as it is '\0' terminated  
  char arr[] = "Hello"; 
  
  printf("%lu", sizeof(arr)); 
  
  return 0; 
} 

Output

6

Example 2-Double Quoted Declaration of Char Array

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 ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ without the null terminator. The sizeof operator will correctly return 5 in this case.



// Works in C, but compilation error in C++ 
#include<stdio.h> 
int main() 
{ 
  // arr[] is not terminated with '\0' 
  // and its size is 5 
  char arr[5] = "Hello";  
    
  printf("%lu", sizeof(arr)); 
    
  return 0; 
} 

Output

5

Example 3- Single Quoted Declaration of char Array

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 '\0'. Instead, the compiler allocates only enough space for the characters provided in the initialization list.

#include<stdio.h> 
int main() 
{ 
  // arr[] is not terminated with '\0'  
  // and its size is 5  
  char arr[]= {'h', 'e', 'l', 'l', 'o'};  
  
  printf("%lu", sizeof(arr)); 
  
  return 0; 
} 

Output

5

FAQ- What Is The Difference Between Single Quoted And Double Quoted Declaration Of Char Array?

Q1. What is the difference between single-quoted and double-quoted declarations of char array in C?

Ans. In C and C++, single quotes (') represent single characters and double quotes (“) represent string literals. When you use a string literal ‘x’, it’s a two-character array containing the character 'x' and a null terminator '\0'.

Q2. What is the difference between single and double quotes in programming?

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.

Q3. What is the difference between char * array and char array in C?

Ans. In C and C++:
char[] is a fixed-size character array with direct indexing.
char* is a character pointer that references a memory location, providing flexibility in size and data access.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment