FreeBasic Short Keyword
last modified June 16, 2025
The FreeBasic Short
keyword represents a 16-bit signed integer
data type. It can store whole numbers from -32,768 to 32,767. Short integers
are useful when memory conservation is important.
Basic Definition
In FreeBasic, Short
is a numeric data type that occupies 2 bytes
(16 bits) of memory. It is signed by default, meaning it can represent both
positive and negative values.
The Short type is ideal for situations where you need to store small integers and want to minimize memory usage. It's commonly used in arrays and structures where space optimization matters.
Declaring Short Variables
This example shows how to declare and initialize Short variables.
Dim smallNumber As Short Dim temperature As Short = -15 Dim itemCount As Short = 25000 Print "smallNumber: "; smallNumber Print "temperature: "; temperature Print "itemCount: "; itemCount
Here we declare three Short variables. The first is uninitialized and contains an undefined value. The others are set to -15 and 25000 respectively. Short variables can store values from -32,768 to 32,767.
Short Arithmetic Operations
Short variables can participate in all standard arithmetic operations.
Dim a As Short = 15000 Dim b As Short = 20000 Dim result As Integer result = a + b Print "Sum: "; result result = b - a Print "Difference: "; result result = a * 2 Print "Product: "; result result = b \ 10 Print "Integer division: "; result
This demonstrates arithmetic with Short values. Note that results may need to be stored in larger types (like Integer) to avoid overflow. The backslash operator performs integer division.
Short in Arrays
Short arrays are memory-efficient when storing many small integers.
Dim scores(1 To 5) As Short scores(1) = 85 scores(2) = 92 scores(3) = 78 scores(4) = 90 scores(5) = 88 Dim total As Integer = 0 For i As Short = 1 To 5 total += scores(i) Next Print "Average score: "; total \ 5
This example creates an array of Short values to store test scores. Using Short for the array and loop counter saves memory compared to using Integer. The total is stored in an Integer to prevent overflow.
Short with Type Suffix
FreeBasic allows using the % suffix to declare Short variables.
Dim smallNum% = 12345 Dim bigNum As Integer = 40000 Print "smallNum% type: "; TypeName(smallNum%) Print "bigNum type: "; TypeName(bigNum) ' This would cause overflow ' smallNum% = bigNum
The % suffix is shorthand for declaring Short variables. The TypeName function shows the variable type. Note that assigning a larger value than Short can hold would cause overflow.
Short Range Checking
It's important to ensure values stay within the Short range.
Dim maxShort As Short = 32767 Dim minShort As Short = -32768 Print "Max Short: "; maxShort Print "Min Short: "; minShort ' These would cause overflow ' maxShort += 1 ' minShort -= 1 Print "Safe operations:" maxShort = maxShort - 1 minShort = minShort + 1 Print "New max: "; maxShort Print "New min: "; minShort
This demonstrates the limits of the Short type. Attempting to exceed these limits would cause overflow. The commented lines show dangerous operations that should be avoided.
Short in Function Parameters
Short can be used for function parameters and return values.
Function SumShorts(a As Short, b As Short) As Short ' Important: Check for potential overflow If a > 0 And b > 0 And (a + b) < 0 Then Print "Warning: Possible overflow" End If Return a + b End Function Dim x As Short = 20000 Dim y As Short = 12000 Print "Sum: "; SumShorts(x, y)
This function takes two Short parameters and returns their sum as a Short. The overflow check demonstrates good practice when working with limited-range types. The function works correctly within the Short range.
Short Conversion
FreeBasic performs implicit and explicit conversions with Short.
Dim s As Short = 100 Dim i As Integer = 50000 Dim f As Single = 123.456 ' Implicit conversion from Short to Integer i = s Print "Integer from Short: "; i ' Explicit conversion from Integer to Short s = CShort(i \ 50) Print "Short from Integer: "; s ' Explicit conversion from Single to Short s = CShort(f) Print "Short from Single: "; s
This shows conversions between Short and other types. CShort explicitly converts to Short, truncating floating-point values. Implicit conversion to larger types is safe, but converting from larger types risks overflow.
Best Practices
- Memory Use: Use Short when memory savings are important.
- Range Awareness: Always be mindful of the -32,768 to 32,767 range.
- Overflow Checks: Implement checks when performing arithmetic.
- Arrays: Prefer Short for large arrays of small integers.
- Conversion: Use explicit conversion (CShort) when narrowing.
This tutorial covered the FreeBasic Short
keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.