C++

What Does an auto Keyword Do in C++

C++ is a popular programming language known for its efficiency and flexibility. One of its features is the use of keywords, which are reserved words that have special meanings within the language. This article covers one such keyword known as the auto keyword in C++.

What is a Keyword in C++?

A reserved word in C++ is a keyword that holds a specific significance within the language. These words cannot be used as variable names or identifiers because they are already used by the language for specific purposes. Some examples of keywords in C++ include int, for, if, and return.

What is the auto Keyword in C++?

In C++, the auto keyword is utilized to deduce the type of variable automatically based on its initializer. This can be useful when working with complex types or when the type of variable is not immediately obvious. The auto keyword enhances code readability and makes the code easy to maintain and debug.

Syntax of auto keyword

To use auto, simply write the auto keyword before the variable name and assign it an initial value.

For example:

auto var1 = 3.143;
auto var2 = 22;

 

In the above code, var1 will be deduced as a double type and var2 as an int type.

We can also define auto keywords in function return types, allowing the compiler to deduce the return type based on the function body.

For Example:

auto sum(int var1, int var2) {
  return (var1 + var2);
}
auto sub(double var1, double var2) {
  return (var1 - var2);
}

 

In the above example, sum() will return an int type, while sub() will return a double type, based on the data types of the input parameters, and return statements.

Example #1

This code demonstrates the use of vectors and iterators in C++ using auto keywords.

Code

#include <iostream>
#include <string>
#include <vector>

int main() {
  auto firstName = "SAM";
  auto lastName = "KASH";
  auto middleName = "AALY";
  std::vector < std::string > nameList;
  nameList.push_back(firstName);
  nameList.push_back(lastName);
  nameList.push_back(middleName);
  std::cout << "Here is the name list:" << std::endl;
  for (auto it = nameList.begin(); it != nameList.end(); ++it) {
    std::cout << * it << std::endl;
  }
  return 0;
}

 

It starts by declaring three strings, representing the first name, last name, and middle name. It then creates an empty vector called nameList. The first name, last name, and middle name are added to the vector using the push_back() function.

The code then prints a header message to the console. It uses a for loop with an iterator (it) to iterate over each element in the nameList vector. The *it syntax is used to dereference the iterator and access the value at that position. The value is then printed on the console:

Output

Example #2

In the given code, variables of different data types are used, and their addition is performed by declaring them as auto. The auto keyword allows the compiler to automatically deduce the resulting data type based on the operand types.

Code

#include <iostream>

using namespace std;
int main() {
  // declaring the variables as auto
  auto var1 = 50;
  auto var2 = 49.990;
  auto result = var1 + var2;
  cout << "Result of addition is " << result << endl;
  return 0;
}

 

In the provided code, the variables var1 and var2 are declared using the auto keyword. The value 50 assigned to var1 will be interpreted as an int by the compiler, while the value 49.990 assigned to var2 will be interpreted as a double. The result of adding these values together is stored in another variable declared as auto. The compiler will interpret this variable as a double, based on the types of operands, and print its value on the screen.

Output

Example #3

Here, in this code, we have used the function return type as auto. This code calculates the modulo of two numbers using a function called mod.

Code

#include<iostream>

using namespace std;
auto mod(auto num1, auto num2) {
  auto res = num1 % num2;
  return res;
}
int main() {
  auto num1 = 10;
  auto num2 = 28;
  auto result = mod(num1, num2);
  cout << "The result of the operation is " << result << endl;
  return 0;
}

 

The function takes two parameters, num1, and num2, which are declared as auto types. Inside the mod function, the modulo operation is performed and the output is stored in a variable called res. The res variable is then returned from the function.

The Main() function contains two variables with values 10 and 28, respectively. The mod function is called with these two variables as arguments, and the result is stored in the result variable. The result is printed on the screen.

Output

Conclusion

In C++, the auto keyword allows for automatic type deduction of a variable based on its initializer. This can be useful when working with complex types or when the type of variable is not immediately obvious. The auto keyword makes code more readable. Read about auto keywords in C++ in this article.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.