Variable size/Set: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(6 intermediate revisions by 4 users not shown)
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}}==
Line 269 ⟶ 326:
<syntaxhighlight lang="d">struct Empty { }
writefln(Empty.sizeof) ; // print 1</syntaxhighlight>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
{In Delphi you can have variables of a range of sizes}
var B: Byte; {8-bit, unsigned}
var C: char; {ASCII character}
var SI: shortint; {8-bit, signed}
var SM: Smallint; {16-bit signed}
var LI: Longint; {32-bit signed}
var W: word; {16-bit unsigned}
var LW: Longword; {32-bit unsigned}
var II: Int64; {64-bit signed}
var SR: Real48; {6-byte real}
var SN: single; {4-byte real}
var DB: double; {8-byte real}
var EX: Extended; {10-byte real}
var CM: Comp; {8-byte fixed point}
var CR: Currency; {8-byte fixed point}
 
{You can also custom define the size range of variable}
 
type TNumRange = -128..127;
var NM: TNumRange;
 
type TUpperCase = 'A'..'Z';
var UP: TUpperCase;
 
</syntaxhighlight>
{{out}}
<pre>
 
</pre>
 
 
=={{header|Erlang}}==
Line 290 ⟶ 384:
 
There is also "double" floating point values (8 bytes). Variables of this type use the suffix #.
 
=={{header|Forth}}==
<syntaxhighlight lang="forth">
create cvar 0 c, \ char size variable
 
variable var \ cell size variable
 
2variable 2var \ 2 cells size variable
 
fvariable fvar \ float size variable
 
</syntaxhighlight>
 
=={{header|Fortran}}==
Line 464 ⟶ 570:
 
Here, v is specified to have a minimum size. In this case, the minimum size of the content is zero, though the size of the representation is somewhat larger.
 
=={{header|Java}}==
Java'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.
 
If more than eight bytes of precision is required they can use either BigInteger or BigDecimal.
<syntaxhighlight lang="java">
public final class VariableSizeSet {
 
public static void main(String[] args) {
System.out.println("The ranges of Java's primitive data types are:");
System.out.println();
System.out.println("A Byte variable has a range of " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
System.out.println("A Short variable has a range of " + Short.MIN_VALUE + " to " + Short.MAX_VALUE);
System.out.println("An Int variable has a range of " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
System.out.println("A Long variable has a range of " + Long.MIN_VALUE +" to " + Long.MAX_VALUE);
System.out.println("A Float variable has a range of " + Float.MIN_VALUE + " to " + Float.MAX_VALUE);
System.out.println("A Double variable has a range of " + Double.MIN_VALUE + " to " + Double.MAX_VALUE);
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
The ranges of Java's primitive data types are:
 
A Byte variable has a range of -128 to 127
A Short variable has a range of -32768 to 32767
An Int variable has a range of -2147483648 to 2147483647
A Long variable has a range of -9223372036854775808 to 9223372036854775807
A Float variable has a range of 1.4E-45 to 3.4028235E38
A Double variable has a range of 4.9E-324 to 1.7976931348623157E308
</pre>
 
=={{header|Julia}}==
Line 903 ⟶ 1,043:
1.00000000000000000000000000000000000000000000000000000E0</syntaxhighlight>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn main() {
b := true
i := 5 // default type is i32
Line 942 ⟶ 1,082:
 
The programmer cannot specify a minimum size for a Map but can specify a minimum size for a List - in effect the amount of heap storage required to store its elements. This can still be increased dynamically by adding futher elements to the List. Here's an example.
<syntaxhighlight lang="ecmascriptwren">// create a list with 10 elements all initialized to zero
var l = List.filled(10, 0)
// give them different values and print them
9,476

edits