When should I use single quotes and double quotes in C or C++ programming?
In C and in C++ single quotes identify a single character, while double quotes create a string literal. 'a' is a single a character literal, while "a" is a string literal containing an 'a' and a null terminator (that is a 2 char array).
In C++ the type of a character literal is char, but note that in C, the type of a character literal is int, that is sizeof 'a' is 4 in an architecture where ints are 32bit (and CHAR_BIT is 8), while sizeof(char) is 1 everywhere.
Some compilers also implement an extension, that allows multi-character constants. The C99 standard says:
6.4.4.4p10: "The value of an integer character constant containing more
than one character (e.g., 'ab'), or
containing a character or escape
sequence that does not map to a
single-byte execution character, is
implementation-defined."
This could look like this, for instance:
const uint32_t png_ihdr = 'IHDR';
The resulting constant (in GCC, which implements this) has the value you get by taking each character and shifting it up, so that 'I' ends up in the most significant bits of the 32-bit value. Obviously, you shouldn't rely on this if you are writing platform independent code.
Single quotes are characters (char), double quotes are null-terminated strings (char *).
char c = 'x';
char *s = "Hello World";
'x' is an integer, representing the numerical value of the
letter x in the machine’s character set
"x" is an array of characters, two characters long,
consisting of ‘x’ followed by ‘\0’
I was poking around stuff like: int cc = 'cc'; It happens that it's basically a byte-wise copy to an integer. Hence the way to look at it is that 'cc' which is basically 2 c's are copied to lower 2 bytes of the integer cc. If you are looking for a trivia, then
printf("%d %d", 'c', 'cc'); would give:
99 25443
that's because 25443 = 99 + 256*99
So 'cc' is a multi-character constant and not a string.
Cheers
Single quotes are for a single character. Double quotes are for a string (array of characters). You can use single quotes to build up a string one character at a time, if you like.
char myChar = 'A';
char myString[] = "Hello Mum";
char myOtherString[] = { 'H','e','l','l','o','\0' };
single quote is for character;
double quote is for string.
In C, single-quotes such as 'a' indicate character constants whereas "a" is an array of characters, always terminated with the \0 character
Double quotes are for string literals, e.g.:
char str[] = "Hello world";
Single quotes are for single character literals, e.g.:
char c = 'x';
EDIT As David stated in another answer, the type of a character literal is int.
A single quote is used for character, while double quotes are used for strings.
For example...
printf("%c \n",'a');
printf("%s","Hello World");
Output
a
Hello World
If you used these in vice versa case and used a single quote for string and double quotes for a character, this will be the result:
printf("%c \n","a");
printf("%s",'Hello World');
output :
For the first line. You will get a garbage value or unexpected value or you may get an output like this:
�
While for the second statement, you will see nothing. One more thing, if you have more statements after this, they will also give you no result.
Note: PHP language gives you the flexibility to use single and double-quotes easily.
Use single quote with single char as:
char ch = 'a';
here 'a' is a char constant and is equal to the ASCII value of char a.
Use double quote with strings as:
char str[] = "foo";
here "foo" is a string literal.
Its okay to use "a" but its not okay to use 'foo'
Single quotes are denoting a char, double denote a string.
In Java, it is also the same.
While I'm sure this doesn't answer what the original asker asked, in case you end up here looking for single quote in literal integers like I have...
C++14 added the ability to add single quotes (') in the middle of number literals to add some visual grouping to the numbers.
constexpr int oneBillion = 1'000'000'000;
constexpr int binary = 0b1010'0101;
constexpr int hex = 0x12'34'5678;
constexpr double pi = 3.1415926535'8979323846'2643383279'5028841971'6939937510;
In C & C++ single quotes is known as a character ('a') whereas double quotes is know as a string ("Hello"). The difference is that a character can store anything but only one alphabet/number etc. A string can store anything.
But also remember that there is a difference between '1' and 1.
If you type
cout<<'1'<<endl<<1;
The output would be the same, but not in this case:
cout<<int('1')<<endl<<int(1);
This time the first line would be 48. As when you convert a character to an int it converts to its ascii and the ascii for '1' is 48.
Same, if you do:
string s="Hi";
s+=48; //This will add "1" to the string
s+="1"; This will also add "1" to the string
different way to declare a char / string
char char_simple = 'a'; // bytes 1 : -128 to 127 or 0 to 255
signed char char_signed = 'a'; // bytes 1: -128 to 127
unsigned char char_u = 'a'; // bytes 2: 0 to 255
// double quote is for string.
char string_simple[] = "myString";
char string_simple_2[] = {'m', 'S', 't', 'r', 'i', 'n', 'g'};
char string_fixed_size[8] = "myString";
char *string_pointer = "myString";
char string_poionter_2 = *"myString";
printf("char = %ld\n", sizeof(char_simple));
printf("char_signed = %ld\n", sizeof(char_signed));
printf("char_u = %ld\n", sizeof(char_u));
printf("string_simple[] = %ld\n", sizeof(string_simple));
printf("string_simple_2[] = %ld\n", sizeof(string_simple_2));
printf("string_fixed_size[8] = %ld\n", sizeof(string_fixed_size));
printf("*string_pointer = %ld\n", sizeof(string_pointer));
printf("string_poionter_2 = %ld\n", sizeof(string_poionter_2));
This question already has answers here:
Single quotes vs. double quotes in C or C++
(15 answers)
Closed 1 year ago.
I tried running a C program with the code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char grade;
float mark;
printf("Enter total mark percentage : ");
scanf("%f", &mark);
if(mark>=90){
grade="A";
}else{
grade="B";
}
printf("Your grade is %c", grade);
return EXIT_SUCCESS;
}
It produces a warning 'assignment from 'char' to 'char*' makes integer from pointer without a cast'.
I am able to solve it by changing " to ' in line 10 and 12
grade='A';
}else{
grade='B';
}
This solves the warning. But I want to use " to contain A & B.
I tried initializing mark, as int instead of char in line 5 but it also didn't work.
I just want to know what is the difference in using ' and " .
The symbol ' is used to specify an integer character constant. For example 'A' is an integer character constant that has the type int.
The symbol " is used to specify a string literal. For example "A" is a string literal that has the type char[2]. That is the string literal is a character array that is stored like
{ 'A', '\0' }
String literals contain sequences of characters terminated by the zero terminating character '\0' that also is stored in string literals.
Character arrays designators used in expressions are implicitly (with rare exceptions) converted to pointers to their first elements of the type char * or const char *.
This means that in this assignment statement
grade="A";
the string literal "A" that has the array type char[2] is converted to a pointer to its first element 'A'. As a result the compiler issues a message that you are trying to assign a pointer to an object of an integer type.
You can write for example
grade = "A"[0];
or
grade = *"A";
But this will only confuse readers of the code because it will be more simpler and clear to write
grade = 'A';
That is there is no need to use a whole array like "A" (remember that it internally represents an array of two elements { 'A', '\0' }) to use only its one element 'A'.
"..." is used for strings - ie. a NUL-terminated char-array (char[] or char*)
'.' is used for a single character - ie. a char.
For example:
"Hello" is a string (char-array) with the following 6 single characters:
'H', 'e', 'l', 'l', 'o', '\0' (The last is the NUL-character).
Thus
'A' is just 'A' (a single char containing the ASCII-value for A)
while
"A" is a char array, containing two chars - 'A' and '\0' (and thus a string)
" " are used for string and ' ' is used for the character. as A and B are
characters we will use single quotes for them.
this is why this format is correct.
grade='A';
}else{
grade='B';
}
Generally strlen() function in C language returns unsigned int but if the string has new line character then what will be the output ?
For example :
What will be the output of strlen("stack\n") in C language ?
strlen("stack\n") --> 6. Nothing special about '\n'.
"What will be the output of strlen("stack\n")?"
6. The newline character such as any character except '\0' (NUL) is counted as any character else.
"Generally strlen() function in C language returns unsigned int."
That is not correct. strlen() returns a size_t value which is quite a distinct type from unsigned int, although in most implementations size_t can be an alias for unsigned int. But to keep the difference is important.
Note: If the string is stored in an char array instead (it is not a string literal and with that immutable) and you want to remove the newline, you can use strcspn():
char a[7] = "stack\n"; // 7 elements, not 6. Required to store terminating NUL.
printf("%zu\n", strlen(a)); // This will print 6.
a[strcspn(a, "\n")] = 0; // Replace newline with NUL.
printf("%zu", strlen(a)); // This will print 5.
So I want to add a string to registry, since the registry strings are to written NULL terminated my string contains a null char in various places.
This is what my string looks like.
char names[550] = "1DFA-3327-*\01DFA-3527-*\001DFA-E527-*\00951-1500-
I convert this to whcar_t string like so.
wchar_t names_w[1000];
size_t charsConverted = 0;
mbstowcs_s(&charsConverted, names_w, names, SIZE);
RegSetValueEx(*pKeyHandle, valueName, 0, REG_MULTI_SZ, (LPBYTE)names_w, size);
The registry entry should be
1DFA-3327-*
1DFA-3527-*
1DFA-E527-*
0951-1500-*
0951-0004-*
0951-160D-*
But this is the registry entry now,
1DFA-3327-*
<a box here>DFA-3527-*
<a box here>DFA-E527-*
951-1500-*
951-0004-*
951-160D-*
So it eats up the 0 in 0951 also eats up the 1 in 1DFA
What I have tried:
1> I tried changing the string to
char names[550] = "1DFA-3327-*\0\01DFA-3527-*\0\001DFA-E527-*\0\00951-1500-
^ ^ Two nulls
2> I tried different conversion.
for(int i; i < SIZE; i++)
names_w[i] = (wchar_t)names[i];
The problem is in your string literal.
char names[550] = "1DFA-3327-*\01DFA-3527-*\001DFA-E527-*\00951-1500-...\0";
^^ ^^^ ^^
You can provide ASCII characters in octal notation (\ooo) or in hexadecimal notation (\x hh).
In your case you provide octal notation and this eats up the next up to three characters. You should change your string to
char names[550] = "1DFA-3327-*\0001DFA-3527-*\0001DFA-E527-*\0000951-1500-...\0";
or
char names[550] = "1DFA-3327-*\0" // put null byte at end of GUID
"1DFA-3527-*" "\0" // add null byte as extra literal
"1DFA-E527-*" "\0"
"0951-1500-...\0";
which also makes it easier to identify the GUIDs in the string.
If you want to use hexadecimal notation then take care that these might eat more than two bytes, so you also need to work with the literal concatenation. (See How to properly add hex escapes into a string-literal?)
char names[550] = "1DFA-3327-*\x00" // put null byte at end of GUID
"1DFA-3527-*" "\x00" // add null byte as extra literal
"1DFA-E527-*" "\x00"
"0951-1500-...\x00";
BTW, is there any reason not to directly store a wide char string?
wchar_t names[550] = L"1DFA-3327-*\x001DFA-3527-*\x001DFA-E527-*\x000951-1500-...\0";
I need to convert a string to a char array in C; how can I do this?
Or at least, how can I extract single chars from a string incrementally?
In C, a string is actually stored as an array of characters, so the 'string pointer' is pointing to the first character. For instance,
char myString[] = "This is some text";
You can access any character as a simple char by using myString as an array, thus:
char myChar = myString[6];
printf("%c\n", myChar); // Prints s
Hope this helps!
David
In C, there's no (real, distinct type of) strings. Every C "string" is an array of chars, zero terminated.
Therefore, to extract a character c at index i from string your_string, just use
char c = your_string[i];
Index is base 0 (first character is your_string[0], second is your_string[1]...).
In this simple way
char str [10] = "IAmCute";
printf ("%c",str[4]);