You use the auto keyword to let the compiler deduce the type. - C++ Data Type

C++ examples for Data Type:auto

Description

You use the auto keyword to let the compiler deduce the type.

Demo Code

#include <limits>
#include <iostream>

int main()/*  ww  w.ja  va  2  s.c o  m*/
{
    auto m = 10;                 // m is type int
    auto n = 200UL;              // n is type unsigned long
    auto pi = 3.14159;           // pi is type double

    std::cout << m;
    std::cout << n;
    std::cout << pi;
}

Result

You can use functional notation with auto for the initial value:

Demo Code

#include <limits>
#include <iostream>

int main()/*from w w  w.  j  av  a  2  s.  c  om*/
{
    auto pi(3.14159);           // pi is type double

    std::cout << pi;
}

Result


Related Tutorials