
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Single quotes vs. double quotes in C or C++\\n
When coding in C/C++, we go across single quotes (' ') and double quotes (" "). These symbols have different roles in the language. Single quotes stand for single characters, while double quotes define strings, which are groups of characters. Knowing this difference has an impact on how data gets stored compared, and handled in your code.
So, let us explore various approaches regarding single vs double quotes in C/C++.
- Character vs String Representation
- Memory and Size Difference
- Comparison Behavior
- Function Call Behavior
Character vs String Representation
This approach shows how single quotes(' ') represent a character, and double quotes(" ") represent a string.
Syntax
Following is the syntax for Character vs String:
// Character literal (single quotes) char c = 'A'; // String literal (double quotes) const char* str = "Anu";
Example
This program displays a character and a string to the console.
#include <iostream> using namespace std; int main() { char ch = 'A'; const char* str = "Anu"; cout << "Character: " << ch << endl; cout << "String: " << str << endl; return 0; }
Output
The above program produces the following result :
Character: A String: Anu
Memory and Size Difference
The memory and size difference highlights how character and string literals differ in memory usage.
A character is 1 byte, while a string is a pointer that usually takes more bytes.
Example
In this example, we display the size of a character as 1 byte and the size of a string pointer depending on the system architecture(4 bytes for 32-bit systems or 8 bytes for 64-bit systems).
#include <iostream> using namespace std; int main() { char ch = 'A'; const char* str = "A"; cout << "Size of character: " << sizeof(ch) << " byte" << endl; cout << "Size of string pointer: " << sizeof(str) << " bytes" << endl; return 0; }
Output
The above program obtained the following result :
Size of character: 1 byte Size of string pointer: 8 bytes
Comparison Behavior
Here, we demonstrate proper and improper comparisons using single vs double quotes.
Example
In this example, we demostrate correct comparison with a character literal and highlights that comparing a character with a string literal causes a compile-time error.
#include <iostream> using namespace std; int main() { char c = 'A'; if (c == 'A') { cout << "Correct comparison with character literal." << endl; } // This will cause a compile-time error if uncommented: // if (c == "A") { // cout << "Incorrect comparison with string literal." << endl; // } return 0; }
Output
The program above produced the following outcome:
Correct comparison with character literal.
Function Call Behavior
The function call behavior shows how characters and strings behave when passed to different function types.
Example
In this example, we define two functions to display a character and a string.
#include <iostream> using namespace std; void printChar(char c) { cout << "Character: " << c << endl; } void printString(const char* s) { cout << "String: " << s << endl; } int main() { printChar('B'); printString("Banu"); return 0; }
Output
The above program obtained the following result:
Character: B String: Banu