C++ Programming Series: Special Characters

In the previous post, we talked about some really important but useless kind of things. I mean that why you really need to understand precedence and sizeof()! Han!?

Well, this post is also not interesting but I believe that it is worth knowing! For example, can you please tell me that how to show a double quote on the console? Ok, so here is the code which may do that:

cout << "You said,"I can show a double quote on the console!" and this is the code which may do that!" << endl;

What happens? Got an error! This is because you divided the sentence into strings… And strings are represented by the group of characters between two double quotes.

Similarly, you may want to have a single quote and then, a tab instead of a group of spaces for proper indentation. For the very purpose, there are some special characters. The list is mentioned here.

The solution of the double quote problem gets a fix below:

cout << "You said,\"I can show a double quote on the console!\" and this is the code which may do that!" << endl;

Instead of endl, you could have \n:

cout << "The special characters are very useful!\n";

No matter how many digits a number has in a list of numbers, it will be formatted fine with /t:

cout << " 1.\t73\n 2.\t9\n 3.\t992\n 4.\t14\n"; //Just run that to find out its meaning! 

That’s it! Try to use all kinds of special characters and make them run like you want!

Believe me, it’s really good to know these characters…


C++ Programming Series: Hello World and Variables

If you already read the previous post and installed Microsoft Visual Studio Express Edition, you can just open it and somewhere, you will see New Solution. Just set a new one and add an empty cpp or source file to it. In this cpp file, copy and paste that code below to it and then, run it. If it runs fine, it will output, “Hello World”, on the console screen.

Here’s the code for printing, “Hello World”:

#include <iostream>
using namespace std;
int main()
 cout << "Hello World" << endl;
 return 0;

The word cout may be taken as a short form of ‘console output’ and endl as ‘end the line’ or put an enter to it. << is an operator called as ‘put to’ operator. Lines of code present between two curly brackets may be taken as a block. There are two statements in the block. Each statement is required to be finished by a semi-colon. main() is the place where codes will run (or execute). For now, this is enough, you will understand the whole of it later on. Don’t worry about this! It looks hard but is very simple.

You can also print numbers and they can be typed in without double commas as well. Just replace “Hello World” with some integer.

cout << 221133 << endl;

Integers are any numbers ranging from infinite negative to infinite positive numbers. But what if we want to store an integer? I mean we may need to call it many times and manipulate its value as well which is hard with a constant like the above one.

To do that, we use variables. To store an integer say 221133 in a variable called as x and then call it later on, we can do something like this:

int x = 221133;
cout << x << endl;

Note that word int. It is the short form of a word called integer. A variable must be written with its type. int or integer is one of them. The cout then, treats x as 221133 which is of type int. Format of any variable is given as:

[type] [name] = [value];

You can declare a variable first and then, give a value to it (initialize it). If you don’t give it a value and print it, you may get an error or most probably, a random value. Therefore, always initialize the variables before using them.

int x;
x = 221133;
cout << x << endl;

The most important thing about the variables is that they have a specific scope. From scope, we mean the area or space where the variable can be used. The scope of a variable is the remaining block after its declaration. If the variable x, is declared in the block below main(), its scope is that block i.e it cannot be used above the declaration or outside the block’s ending flower bracket.

Don’t get panic with all the things here! There is way much more stuff. Try to figure out your learning rate and proceed according to it! Just try to type codes and make them run…