What's new

Backslashes and Special Characters [Tutorial]

  • Thread starter Bellatrix
  • Start date
  • Views 894
Bellatrix

Bellatrix

Enthusiast
Tutorial Creator
Messages
197
Reaction score
43
Points
125
Sin$
0
Hello all. Today I thought I would make a small tutorial on Backslashes, special characters and the importance of them.

So let's start with a basic set of code.

Code:
#include <iostream>

using namespace std;

int main()
{
cout << "Why hello thereeee. " << endl;
return 0;
}

Many of you already may know this small feature but here it is. Adding 2 slashes before any line of code will make the compiler read the code as a comment, or code that can only be seen in the source code. So if you were to run the program right now with two slashes before a set of code, it would not read the code, and it would be ignored. So let's put 2 slashes before any code.

Code:
#include <iostream>

using namespace std;
//two slashes before code.
int main()
{
cout << "Why hello thereeee. " << endl;
return 0;
}

Running the program you should see;

Why hello thereeee.

And the 2 slashes will not affect the code, and it will be ignored.

Another great affect of the backslash is that it can also create tabs. "What do you mean?" I'll show you.

So let's have the basic code once again, but this time let's add a backslash and "T" and run the compiler.

Code:
 #include <iostream>

using namespace std;

int main()
{
cout << "Why/thello thereeee. " << endl;
return 0;
}

Running the compiler you should see this:

Why hello thereeee.

See? Adding a /t before any set of text will add a tab to the text before it. Neat right? There's more.

What if you wanted to add just one backslash to the text between cout? You would add 2 backslashes.

Code:
#include <iostream>

using namespace std;

int main()
{
cout << "Why hello //thereeee. " << endl;
return 0;
}

running the code you should see this.

Why hello /thereeee.


It only excecuted ONE backslash. There is still one more neat tutorial. Let's say you wanted to make a double quote INSIDE the double quotes. How would we do that? Simple. Add a backslash before the double quotes.

Code:
#include <iostream>

using namespace std;

int main()
{
cout << "Why hello thereeee. /" << endl;
return 0;
}

Running the code you should see this.

Why hello thereeee. "

Cool right? Cool effects of the backslash :biggrin: Hope you enjoyed my tutorial, and please give feedback :wink:
 
Calamity

Calamity

Retired
Retired
Messages
6,704
Reaction score
1,159
Points
650
Sin$
0
I hope you know you are using a forward slash.

They are called escape characters as well, not special. Special characters are more of the 0-32 ASCII characters that aren't printable, but used for other reasons, such as end of transmission character for end of receiving data.
 
B

BaBaBaseline

Enthusiast
Messages
156
Reaction score
25
Points
85
Sin$
0
You should be using \ not / and one I noticed you forgot
Code:
#include <iostream>

using namespace std;

int main()
{
cout << "Why hello \n thereeee." << endl;
return 0;
}
Why hello
thereeee
It make a new text line.
And this isn't just for c++ several languages use these same escape characters.
 
Top Bottom
Login
Register