Home >>Cpp Programs >Auto keyword in Cpp

Auto keyword in Cpp

C++ Auto

C++ auto is a type of keyword that exists and the function of it is to specify the type of the variable that is being declared should get deducted from its initialize. These are the keywords that are known as the Type Inference in C++. This basically refers to the automatic deduction of the data type of an expression that is in a programming language. Before the C++ 11 came in to existence, each of the data type needed to be explicitly declared at the compiling time that too being limiting the values of an expression at the runtime but that was the situation of previous times, after the new version of C++ has been released there are many keywords that has been included in the library that permits a programmer that they can leave the type deduction to the compiler itself.

The programmer now has to spend less time in writing the things that are already known to the compiler with the help of type inference capabilities. The time for compilation generally increases slightly because all the types are deduced only in the compiler phase despite of that there is no affect on the run time of the program. The auto keyword in C++ is associated with the decltype keyword and it is the joint companion of it. Let’s discuss both of them along with an example.

auto keyword

The auto keyword in C++ generally specifies that the type of the variable that is basically being declared will be deducted automatically from its initializer. Whereas, when it comes to functions and their return type is auto then the things will be different as that will be evaluated by the return type expression at the runtime.

Here is an example of the auto keyword that is depicted below and that will explain you about the application aspect of it:

#include <bits/stdc++.h> 
using namespace std; 
int main() 
{ 
	auto a = 10; 
	auto b = 4.45; 
	auto ptr = &a; 
	cout << typeid(a).name() << endl 
		<< typeid(b).name() << endl 
		<< typeid(ptr).name() << endl; 
	return 0; 
}
Output :
i
d
p

In the above example, there is the use of typeid just for getting the type of the variables. typeid is an operator in C++ that is used where the dynamic type of an object is needed to be known.

Another example of auto keyword

#include <bits/stdc++.h> 
using namespace std; 
int main() 
{ 
	//First need to  Create a strings set 
	set<string> str; 
	str.insert({ "This", "is", "auto", "keyword" }); 

	for (auto it = str.begin(); it != str.end(); it++) 
		cout << *it << " "; 

	return 0; 
} 
Output :This is auto keyword

decltype Keyword

decltype Keyword is C++ is generally used to inspect the declared type of an entity or the type of an expression that are used in the C++ programming language. Auto keyword in C++ basically lets the programmer to declare a variable with the particular type on the other hand the decltype keyword in C++ basically lets the programmer to extract the type direct from the variable hence; it can be said that the decltype is sort of an operator that generally evaluates the type of the expression that has been passed.

Here is an example of the same that will let you understand the application aspect of it:

#include <bits/stdc++.h> 
using namespace std; 
int Myfun1() { return 100; } 
char Myfun2() { return 'e'; } 
int main() 
{ 
	decltype(Myfun1()) a; 
	decltype(Myfun2()) b; 

	cout << typeid(a).name() << endl; 
	cout << typeid(b).name() << endl; 
	return 0; 
}
Output :
i
c

Cpp Programs Fibonacci Series in C++ Armstrong Number in C++ Factorial program in C++ Check Palindrome in C++ Prime Number Program In C++ Reverse Number Program in C++ Sum Of Digits Program in C++ C++ Find C++ protected keyword CPP Program for different ways to print array elements CPP Program to determine the colour of chess square CPP Program to Reverse Number CPP Program to Calculate Power of a Number CPP Program to print all Even and Odd numbers from 1 to N Program to find whether a no is power of two in CPP C++ program to find largest list of prime numbers Auto keyword in Cpp C++ program to print the left Rotation of the array Convert a given binary Tree to Doubly Linked List Delete keys in a Linked list using C++ program How do you delete a linked list in C++ Implement stack with linked list in c++ C++ Program to find first occurrence of a Number Using Recursion in an array C++ program to find Last occurrence of a Number using Recursion in an array C++ Program to find Union of two singly Linked Lists Remove Duplicates from linked list in c++ C++ Program to find Nth node in Linked List Merge sort singly linked list c++ C++ Program to Convert Roman Number to Integer Number C++ Program to find LCM of two numbers C++ Password Generator C++ Program to multiply two numbers without using multiplication operator C++ Program to print all possible subset of a set Sum of all the elements in an array divisible by a given number Print First uppercase letter in a string c++ C++ Program to Find the Number Occurring Odd Number of Times C++ Program for Print address of Variable Using Pointer C++ Program to Find ASCII Value of a Character C++ Classes and Objects Program Create a class method in C++ C++ Create Empty Class Define a class method outside the class definition in C++ How to create multiple objects of a class in C++
No Sidebar ads