anyone explain why I need single quote?

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
Because, I assume, this is a character.
Do you know how the program interprets it without single quote?
If you put this.

1
2

cout << "HEY!"; 


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

Is that what you meant?
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.
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++.
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.
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
Topic archived. No new replies allowed.