Home » C++ programming language » C++ 11

Use 'auto' to explicit type declarations in C++ 11

C++ 11 introduced a new feature explicit type declarations using auto, here we are going to learn, how we can declare a variable without specifying its data types?

'auto' keyword can be used to explicit declaration of the variables.

Variable must be initialized while declaration

Let's consider the declaration

auto variable_name = value;

Consider the following program - here we are declaring three variables a, b and c with initial assignment by 10 (as integer value), 10.23f (as float assignment) and 'x' (as character assignment).

The program will print the occupied size by the variables along with the stored values.

#include <iostream>
using namespace std;

int main()
	auto a=10;
	auto b=10.23f;
	auto c='x';
	cout<<"Occupied size by variables:"<<endl;
	cout<<"size of a: "<<sizeof(a)<<endl;
	cout<<"size of b: "<<sizeof(b)<<endl;
	cout<<"size of c: "<<sizeof(c)<<endl;
	cout<<"Values of variables: "<<endl;
	cout<<"a= "<<a<<endl;
	cout<<"b= "<<b<<endl;
	cout<<"c= "<<c<<endl;
	return 0;
    Occupied size by variables: 
    size of a: 4
    size of b: 4
    size of c: 1
    Values of variables:
    a= 10 
    b= 10.23
    c= x

This program will run successfully within the compiler which supports C++ 11 features, I run this program online on TutorialsPoint Coding Ground

(C++ 11 Compiler: https://www.tutorialspoint.com/compile_cpp11_online.php).

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.