Auto Keyword in C++

Auto Keyword in C++ with Examples:

In this article, I am going to discuss Auto Keyword in C++ with Examples. Please read our previous article where we discussed STL in C++ with Examples. The Auto Keyword is Introduced in C++ 11. At the end of this article, you will understand the auto type of declaration in C++ with Examples.

Auto Keyword in C++

Usually, we use int, float, double, and char types of variables but C++11 allows the auto type of variable declaration. Suppose we don’t know the data type that we require and it depends on the results that we are obtaining. Then we can use auto declaration in C++. For example, if we have an expression like,

3 * 6.4 + ‘t’;

And we want to store the result of this expression in some variable i.e. x. So, what will be the data type of x? Instead of finding out the exact data type, we can write it as,

auto x = 3 * 6.4 + ‘t’;

This will perform the operations and store the result in x of the same data type as the result. So, we don’t have to decide the data type on our own. If we see the above expression, there are 3 types of data: int, float, and char. So finally, the result of the expression will be of double type. So, the type of variable that we require in this expression is type double. So, by writing auto or double we can store the result of that expression. Auto means automatically it becomes double in the above example.

If we execute the above expression then we will get 135.2 which is a double value. Let us see this practically. Following is the complete example code.

#include <iostream>
using namespace std;
int main()
{
    auto x = 3 * 6.4 + 't';
    cout << x;
    return 0;
}

Output: 135.2 

Note: So, the point that you need to remember is based on the expression result, the compiler will decide what will be the actual type at runtime and that will be replaced by auto. 

Let us take another example. If you execute the below code then the value of x will be 144.2.

#include <iostream>
using namespace std;
int main()
{
    double d = 25.3;
    int i = 43;
    auto x = 4 * d + i;
    cout << x;
    return 0;
}

Output: 144.2 

Suppose we have a function fun() as follows:
char fun(){
     return ‘a’;
}

And you call the above function inside the main method as follows:
auto x = fun();

So, here, the value of x will be ‘a’. The complete example code is given below.

#include <iostream>
using namespace std;
char fun()
{
    return 'a';
}

int main()
{
    auto x = fun ();
    cout << x;
    return 0;
}

Output: a

The auto keyword is a very powerful and useful feature of C++ 11. When a programmer uses the library functions or some functions of the built-in classes then he/she doesn’t have to know about the data type. We can simply use the auto declaration to do it automatically. It saves the programmer’s time.

Decltype in C++ 11:

There is one more feature of C++ 11 which is declaration type. For example,
float x = 32.2;

Suppose we have this x variable and we want to create another variable of the same data type as x. So, we can write,
decltype(x) z = 67.8;

Here data type of z will be the same as the data type of x. So, from the existing variables, we can capture their data types and we can use the same data type for new variables. The complete example code is given below.

#include <iostream>
using namespace std;
int main()
{
    float x = 32.2;
    decltype(x) z = 67.8;
    cout << x << endl;
    cout << z << endl;
    return 0;
}

In the next article, I am going to discuss the Final Keyword in C++ with Examples. Here, in this article, I try to explain Auto Keyword in C++ with Examples and I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this Auto Keyword in C++ with Examples article.

Leave a Reply

Your email address will not be published.