[try Beta version]
Not logged in

 
anyone explain why I need single quote?

May 25, 2011 at 1:36am
Can anyone explain a bit of the meaning and the cause of the compile error and why I must use single quote here?
1
2
3
4
5
6
7
#include <iostream>
using namespace std;
int main()
{
        cout << \26 << endl;
        return 0;
}



xtest.cpp: In function `int main()':
xtest.cpp:28: error: stray '\' in program
May 25, 2011 at 1:42am
Because, I assume, this is a character.
May 25, 2011 at 1:45am
Do you know how the program interprets it without single quote?
May 25, 2011 at 1:52am
If you put this.

1
2

cout << "HEY!"; 


Then the compiler views it as a "const char*" variable.

Is that what you meant?
May 25, 2011 at 1:54am
I think C and C++ give the backslash (\) a single use: To escape a character, and it is inside const chars or const strings. If found outside of a const char or a const string it is automatically an error.
May 25, 2011 at 1:56am
In natural languages, such as engish, we have syntax rules which define what is allowable in a sentence. For example, saying "the happy cow" is ok, but saying "the cow happy" is not ok (maybe in spanish, but not in english).

Similarly, programming languages have syntax rules. One of these rules is that character constants must be surrounded in single quotes. The rationale behind these seemingly arbitrary rules usually have to do with the ability to more easily parse the code files.

Consider

cout << a;

vs

cout << 'a';

How could you tell the difference without the quotes?

Also, feel free to look up the difference between single and double quotes in c++.
May 26, 2011 at 12:54am
Thanks for the reply, it helps.
I was curious how the compiler got confused by this \26 and spit "stray '\' in program" at that time.
May 26, 2011 at 3:58pm
An example of something similar in english would be if you wrote:

"The and color green is cool."

Although the word "and" is a real word, it does not belong between "The" and "color" in that sentence. So our brains stop and think "What?" With some thinking you can decide that either the author of the sentence thinks "and" is an adjective, or it was accidentally placed there, making it a "stray" 'and'. Omitting it reveals a proper sentence structure.

The "stray '\' in program" shows up because the compiler's parser (the part of the compiler that is in charge of syntax correctness), has run into a symbol that, although it's a legal symbol in code, has popped up in a context which it believes to be incorrect. Although the backslash is useful in other contexts (*inside* of quotes, for example), the compiler cannot figure out what to do with it in this particular situation.

Compilers do not attempt to fix your code as sometimes it can be impossible. For example, in this situation, it is difficult to tell if the correct code is cout << "\26" or cout << 26. Instead of fixing it for you, it just tells you that you messed up.
Last edited on May 26, 2011 at 4:02pm
Topic archived. No new replies allowed.