Contenido del Curso
C++ Data Types
2. Numerical Data Types
4. Other Data Types and Concepts
C++ Data Types
Auto
Although C++ is a typed language, you can use auto
when initializing a variable to determine the type automatically. This feature was introduced in C++14 and does not work with older versions.
Here is an example:
main.cpp
Note that it is harder to catch errors when using auto
. Consider the example:
main.cpp
Specifying types would help us understand code better and find what's wrong:
main.cpp
Note
Any of
num1
,num2
ornum3
should befloat
to output the correct result.
Nevertheless, auto
is widely used in range-based for
loops since it is a versatile method of iterating over arrays and other containers. Just use the following syntax for iterating over arr
:
main.cpp
It also can be used when the type is long to type but is clear from the context and can't lead to incorrect results.
¿Todo estuvo claro?
Contenido del Curso
C++ Data Types
2. Numerical Data Types
4. Other Data Types and Concepts
C++ Data Types
Auto
Although C++ is a typed language, you can use auto
when initializing a variable to determine the type automatically. This feature was introduced in C++14 and does not work with older versions.
Here is an example:
main.cpp
Note that it is harder to catch errors when using auto
. Consider the example:
main.cpp
Specifying types would help us understand code better and find what's wrong:
main.cpp
Note
Any of
num1
,num2
ornum3
should befloat
to output the correct result.
Nevertheless, auto
is widely used in range-based for
loops since it is a versatile method of iterating over arrays and other containers. Just use the following syntax for iterating over arr
:
main.cpp
It also can be used when the type is long to type but is clear from the context and can't lead to incorrect results.
¿Todo estuvo claro?