Hi,
could anybody tell me about the differences between using single and double quotation?
thanks
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.Code: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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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.whereas
is likely to give you warnings about converting const char* to intCode:int myint = "abcd";
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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