In C, there is #define with which you can define constant values, which is replaced by the preprocessor. This can benefit you when you want to use a value which is not meant to change at all... C++ has a feature to replace this usage of #define, it is called const, and the basic syntax is like the following:

const int ci = 5;

In the above single line of code, I defined an int, but what is that const keyword? it changes the normal int definition into a constant int definition. Which means the value of ci cannot be edited at all.
If you try to change the value of ci along the program, you will get a compile error. Isn't that nice? you cannot change the value of const variables by mistake So it's like a safety check.

Const can be used in many situations and places. Look at the following uses...

Const Pointer
The address of a constant pointer cannot be changed, but the value it points to, can be changed.

int * const p = &i;

Read it from right to left: p is a constant pointer to an int, which points to the address of i. Totally simple

Pointer to Const
What if we want to define a pointer to a constant int? Have Great attention:

const int * p = &i;

Again, read from right, p is a pointer to an int which is const. Simple, right? but notice, there is another syntax for this, which is correct too:

int const * p = &i;

By reading from right to left we get: p is a pointer to a const int
It's the same as the one before functionally, so we can use both equally, use the one you are more comfortable with
The value p points to cannot be changed, but the address p points to Can be changed.

Const Pointer to Const
This is a mix of the previous two versions mentioned, have a look:

int const * const p = &i;

p is a constant pointer to a constant int, neither the value nor the address of p can be changed.

Pay extra attention: Const variables and pointers need assignment and definition at the Same time, otherwise you get a compilation error.

Const As Return
There are times that, you have to return a const reference/value in a function, sometimes they play an important role to avoid bugs:

char* func() {
return "hey";
}

this function returns a pointer to a char array, actually returning a constant memory location, what happens if we write such code:

char * p = func();
p[0] = 'a';

This tries to change the value of a character in the array, but that memory location was constant and not meant for changing, which causes the program to crash with a nasty "Segmentation Fault"
To solve this mistake and make it safe, use const like this:

const char* func() {
return "hey";
}

Now, You cannot mistakenly change the value like before, as the pointer returned is a pointer to constant char So you have to store this returned address in a pointer to const char, which disables any changes made to the value

Const As Argument
Constant objects come very handy as function arguments, it's like a promise to compiler that you Will Not change the value of that argument inside the scope of the function (or other cases, whichever you define).
The syntax is so simple and similar to what we had before:

void print(const int a) {
cout<<a;
}

Here we didn't want to change a, just a simple print. Now that we defined the argument with const, we can have calls such as print(4) which we couldn't have without the const.

OK, were is the perfect use of this feature?... When passing by Reference! When you pass an object by reference to a function, you are allowed to directly change it's value, if we want to restrict the function and avoid any changes be made(and benefit passing by reference), we have to make this it const:

void print(const LargeType& a) {
//do some stuff
}

Now, you are using the benefit of passing by reference and avoiding any changes made to the variable at the same time.
I am going to show you a class example with lots of const usage at the end... now we have 1 more section...

Const In Classes
By defining a member function as constant, you are banning that member function from making any changes to member variables. The syntax is as follows:

Note the place of this const, this member function is banned from changing any member variables (here "val"). There are times when you Have to make a member function constant. Look at the following example:

This code gives a compilation error. The reason is that, f1 is a constant foo. So the compiler has to make sure all members of the object remain unchanged, just change the class definition like this to correct the error:

I might add that const is a compile-time only feature and does not slow your program's run speed.
Also, note that it is mostly to prevent mistakes. You will be able to easily circumvent it if you try. For example:

In C, there is #define with which you can define constant values, which is replaced by the preprocessor. This can benefit you when you want to use a value which is not meant to change at all... C++ has a feature to replace this usage of #define, it is called const

Firstly, very good tutorial man, thanks! And I guess this concept is also present in C language. So we've #define and const in C

Firstly, very good tutorial man, thanks! And I guess this concept is also present in C language. So we've #define and const in C

Well strictly you have #define in C++ as well sop both have both.

However consts in C are not as good as consts in C++, they are... how to put it... less constant. A constant in C++ is a true constant and can be used in most places a literal constant can be but a constant in C is just a variable that can not be changed, it can not be used anywhere that requires a literal constant.

As an example the following will compile in C++ but will not compile in C (C89 at least)