Hi,
could anybody tell me about the differences between using single and double quotation?
thanks
Printable View
Hi,
could anybody tell me about the differences between using single and double quotation?
thanks
Single quotations are primarily used when denoting a single character:
Double quotations are used with strings and output statements:Code:char letter = 'e' ;
If there are any other distinctions, someone else can tell them; those are the only ones I can think of off the top of my head. :DCode:string MyString = "This is a string";
cout << "This is an instance where double quotations are used.";
That was quick Velocity.
thanks
More formally, single quotes delimit a character literal, double quotes delimit a string literal.
to expand upon what CornedBee said, you can use single quotes for a lot of interesting things.
for example:
is perfectly valid code, and should give you no warnings whereasCode:int myint = 'abcd';
is likely to give you warnings about converting const char* to intCode:int myint = "abcd";
Note however that the actual integer value of [code]int myint = 'abcd';[/cod] may be either of (or, technically something else entirely as it is up to the compiler to make it into an integer, and the compiler is allowed to do "anything it likes as long as 'abcd' and 'abce' are different), but that is unlikely):
That of course assumes that the integer is 32-bit (which is commonly the case, but older compilers may have 16-bit int, which would make the compiler grumble about "value to large for integer type" or some such).Code:int myint = 'a' + ('b' << 8) + ('c' << 16) + ('d' << 24);
// or
int myint = 'd' + ('c' << 8) + ('b' << 16) + ('a' << 24);
So whilst it is a clever way to store a small number of characters in a single integer value, it is not necessarily good if you want code that is portable to different types of processor architectures.
--
Mats
The result of multi-character character constants is implementation-defined and highly unportable. GCC therefore warns about it by default.Code:lit.cpp:2:13: warning: multi-character character constant
Errors, not warnings.Quote:
whereas
is likely to give you warnings about converting const char* to intCode:int myint = "abcd";
Further correction, that is an error in both C and C++... I really wish you would learn where these languages are different. I'll show you here:
Code:Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:non-strict warnings C90
"ComeauTest.c", line 3: error: a value of type "char *" cannot be used to initialize
an entity of type "int"
int foo = "bar";
^
1 error detected in the compilation of "ComeauTest.c".
When I write warning, I meant that the compiler spits out a warning instead of an error (which most compilers do), and not that it is not a programmer error in itself (that, it is).
But how are you supposed to know this when all C compilers spits of warnings instead of errors?!?!?!
The term for any given line in a compiler's output is diagnostic. The standard defines this term explicitly and uses it where the compiler needs to say something. There are two types of diagnostics I am aware of, warnings and errors. Their difference is that only errors will stop compilation.
Comeau's output is strong evidence that even in C90, compilation will stop if you initialize int with a char *. QED, it is an error.
C99 section 6.3.2.3 states that "any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type."
Now, "bar" is of type const char[4], so we have the usual conversion to const char*, thus it seems that initialising an int with "bar" is not necessarily an error in C, though it is almost certainly a mistake.
Microsoft Visual C++ 2008 does not raise an error with
and also does not raise error withCode:int t = 'abcd';
//Actually the value is 1633837924
Code:int t = "abcd";
//the value is 4211020
It's an error. The C standard is organized even worse than the C++ standard, but 6.5.4p3 says, "Conversions that involve pointers, other than where permitted by the constraints of 6.5.16.1, shall be specified by means of an explicit cast."
And said section permits, paraphrased:
1) Conversion between arithmetic types.
2) Conversion between compatible structures and unions.
3) Conversion between pointers to compatible types, where cv qualification is not lost.
4) Conversion between void* and other pointer types, where cv qualification is not lost.
5) Conversion from a null pointer constant to a pointer.
6) Conversion from a pointer to _Bool.
Hmm ... what mode (C or C++) and warning level? There should be at least a warning. (There's an awful lot of really, really bad legacy C code out there, which is why most compilers accept the code. But they should still warn.)
All warnings are default, and build is debug, and C++
I think one bottom line thing to say that neither C nor C++ nor most non-interpreted languages perceive a single quote the same as a double quote the way as an interpreted language does.
JavaScript
That is cool. But in C++ that is not cool at all. Luckily your compiler will notify you of the difference when you make a "boo boo."Code:document.alert('Holy smokes! You are being annoyed by a pop-up message!');