Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Difference
2.1.
Double Quoted Array
2.2.
Single Quoted Array
3.
Frequently Asked Questions
3.1.
What is C?
3.2.
What is an array in the C programming language?
3.3.
What is the syntax to create an array in C?
3.4.
What are header files?
3.5.
What is the stdio.h header file?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Difference Between Single Quoted and Double Quoted Char Array in C

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

C is a general-purpose programming language. C is commonly used in operating systems, device driver codes, and protocol stacks. The C programming language is not to be confused with the C++ programming language, which is an extension of the C programming language.

The array is nothing but a collection of elements of a similar data type. Each element stored in an array can be accessed easily using only the index of the element. In the C programming language, the character array is used to store a collection of characters at one memory location. The characters can be quoted using double quotes and single quotes. This blog will look at the difference between the single quoted and double quoted arrays.

Also Read, Sum of Digits in C, C Static Function

Difference

Double Quoted Array

In C programming language, if the characters are quoted in double quotes and the array size is not specified. In that case, the compiler automatically allots an additional blank space to the array and, in turn, increases the size of the array by one.

Let us look at an example of this case,

#include<stdio.h>
int main()
{
char arr[] = "CodingNinja";
printf("\n\nActual number of characters: 11\n");
printf("Number of elements in the Array: %lu\n\n\n", sizeof(arr));
return 1;
}

 

In the above example, the elements are quoted in double quotes, and the size of the array is not specified. 

 

The above problem can be solved by providing the size of the array during the declaration of the array. For example,

#include<stdio.h>
int main()
{
char arr[11] = "CodingNinja";
printf("\n\nActual number of characters: 11\n");
printf("Number of elements in the Array: %lu\n\n\n", sizeof(arr));
return 1;
}

 

(Note: This solution will throw an error in the C++ programming language.)

You can also read about the dynamic arrays in c, Tribonacci Series and  Short int in C Programming

Single Quoted Array

In C programming language, if the characters are quoted in single quotes and the array size is not specified. In that case, the compiler automatically doesn’t add any additional spaces to the array. For example,

#include<stdio.h>
int main()
{
char arr[] = {'N','i','n','j','a'};
printf("\n\nActual number of characters: 5\n");
printf("Number of elements in the Array: %lu\n\n\n", sizeof(arr));
return 1;
}

 

In the above example, each element is separately quoted in single quotes and separated by a comma(,).

Try it on an online compiler for better understanding.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

What is C?

C is a general-purpose programming language. C is commonly used in operating systems, device driver code, and protocol stacks.

What is an array in the C programming language?

The array is nothing but a collection of elements of a similar data type. Each element stored in an array can be accessed easily using only the index of the element.

What is the syntax to create an array in C?

Arrays can be created using the following syntax in the C and C++ programming languages,

dataType arrayName = {'Element1', 'Element2', ....}

What are header files?

Header files are a collection of pre-defined functions that are used to make the developer's programming experience more efficient and faster. Developers can import various header files using the include keyword in the C and C++ programming languages.

What is the stdio.h header file?

The stdio.h header file includes all the essential information regarding the input and the output related functions in the C and C++ programming languages.

Conclusion

This blog covered all the necessary points associated with the differences between a single quoted array and a double quoted array. We further looked at codes of single and double quoted arrays. To get a stronghold of the array concept, try solving the Sum Of Two Arrays problem.

Recommended problems -

 

Don’t stop here. Check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Previous article
Types of Array in C
Next article
Difference between Arrays and Pointers
Live masterclass