Variable size/Set: Difference between revisions
New post without using external libraries, in addition to an existing post which uses the "Boost" library.
(New post.) |
(New post without using external libraries, in addition to an existing post which uses the "Boost" library.) |
||
Line 253:
boost::int_least32_t foo;</syntaxhighlight>
Alternatively,
C++'s primitive types are fixed in size. If a programmer wants a numeric type to be able to accommodate
a certain size of number, up to a maximum of eight bytes, then they declare a variable of the appropriate type.
<syntaxhighlight lang="c++">
#include <iostream>
#include <climits>
#include <cfloat>
int main() {
std::cout << "The ranges of C++'s primitive data types are:" << std::endl << std::endl;
std::cout << "a char ranges from : " << CHAR_MIN << " to " << CHAR_MAX << std::endl;
std::cout << "a short char ranges from : " << SCHAR_MIN << " to " << SCHAR_MAX << std::endl;
std::cout << "an unsigned char ranges from : " << 0 << " to " << UCHAR_MAX << std::endl << std::endl;
std::cout << "a short int ranges from : " << SHRT_MIN << " to " << SHRT_MAX << std::endl;
std::cout << "an unsigned short int ranges from : " << 0 << " to " << USHRT_MAX << std::endl << std::endl;
std::cout << "an int ranges from : " << INT_MIN << " to " << INT_MAX << std::endl;
std::cout << "an unsigned int ranges from : " << 0 << " to " << UINT_MAX << std::endl << std::endl;
std::cout << "a long int ranges from : " << LONG_MIN << " to " << LONG_MAX << std::endl;
std::cout << "an unsigned long int ranges from : " << 0 << " to " << ULONG_MAX << std::endl;
std::cout << "a long long int ranges from : " << LLONG_MIN << " to " << LLONG_MAX << std::endl;
std::cout << "an unsigned long long int ranges from : " << 0 << " to " << ULLONG_MAX <<std::endl << std::endl;
std::cout << "a float ranges from : " << -FLT_MAX << " to " << +FLT_MAX << std::endl << std::endl;
std::cout << "a double ranges from : " << -DBL_MAX << " to " << +DBL_MAX << std::endl;
}
</syntaxhighlight>
{{ out }}
<pre>
The ranges of C++'s primitive data types are:
a char ranges from : -128 to 127
a short char ranges from : -128 to 127
an unsigned char ranges from : 0 to 255
a short int ranges from : -32768 to 32767
an unsigned short int ranges from : 0 to 65535
an int ranges from : -2147483648 to 2147483647
an unsigned int ranges from : 0 to 4294967295
a long int ranges from : -2147483648 to 2147483647
an unsigned long int ranges from : 0 to 4294967295
a long long int ranges from : -9223372036854775808 to 9223372036854775807
an unsigned long long int ranges from : 0 to 18446744073709551615
a float ranges from : -3.40282e+38 to 3.40282e+38
a double ranges from : -1.79769e+308 to 1.79769e+308
</pre>
=={{header|D}}==
|