FreeBasic Integer Keyword
Last modified October 18, 2026
The FreeBasic Integer keyword represents a fundamental data type
for storing whole numbers. Integers are essential for counting, indexing,
and mathematical operations in programming.
Basic Definition
In FreeBasic, Integer is a signed integer data type whose size
depends on the target platform: 32-bit on 32-bit targets and 64-bit on 64-bit
targets (the same size as a pointer). On a 32-bit target it can store values
from -2,147,483,648 to 2,147,483,647. For a fixed 32-bit type, use
Long. Integers are stored in two's complement format.
Integer variables are used for calculations that do not require
decimal precision. They are faster to process than floating-point numbers and
consume less memory than larger numeric types.
Declaring Integer Variables
This example shows how to declare and initialize Integer variables.
Dim age As Integer Dim count As Integer = 10 Dim temperature As Integer = -5 Print "age: "; age Print "count: "; count Print "temperature: "; temperature
Here we declare three Integer variables. The first is uninitialized and contains an undefined value. The others are initialized with positive and negative values. Integer variables default to 0 if declared at module level.
Integer Arithmetic Operations
Integers support standard arithmetic operations like addition and division.
Dim a As Integer = 15 Dim b As Integer = 4 Print "a + b = "; a + b Print "a - b = "; a - b Print "a * b = "; a * b Print "a / b = "; a / b Print "a \ b = "; a \ b Print "a Mod b = "; a Mod b
This example demonstrates basic arithmetic with Integers. The /
operator performs floating-point division, so 15 / 4 gives 3.75.
The \ operator performs integer division and truncates the result
to 3. The Mod operator returns the remainder of division.
Integer Overflow
Integer variables can overflow if assigned values outside their range. The range depends on the target: on 64-bit the maximum is 9,223,372,036,854,775,807.
Dim bigNum As Integer = 2147483647 Print "Maximum Integer: "; bigNum bigNum = bigNum + 1 Print "After overflow: "; bigNum
This example uses 2147483647, the maximum value of a 32-bit integer. On a
64-bit target the value 2147483647 is not the limit, so adding 1 gives
2147483648 with no overflow. On a 32-bit target the addition wraps around to
-2147483648. Use Integer<32> or Long for a
fixed 32-bit type, and check ranges when working with Integers.
Integer Type Conversion
FreeBasic automatically converts between Integer and other numeric
types.
Dim intVal As Integer = 42
Dim dblVal As Double = 3.14159
Print "Double to Integer: "; CInt(dblVal)
Print "Integer to Double: "; CDbl(intVal)
Print "String to Integer: "; CInt("1234")
This shows explicit conversion between types using CInt and
CDbl. When converting from floating-point, the value is
rounded. String conversion parses numeric strings.
Integer Arrays
Arrays of Integers are useful for storing sequences of whole numbers.
Dim numbers(1 To 5) As Integer
For i As Integer = 1 To 5
numbers(i) = i * 10
Next
For i As Integer = 1 To 5
Print numbers(i)
Next
This creates an array of 5 Integers and fills it with multiples of 10. Arrays are zero-based by default, but we specified 1-based indexing here. Integer arrays are memory-efficient for numeric sequences.
Integer with Bitwise Operations
Integers support bit-level manipulation using bitwise operators.
Dim a As Integer = &b1100 ' 12 in decimal Dim b As Integer = &b1010 ' 10 in decimal Print "a AND b: "; a And b Print "a OR b: "; a Or b Print "NOT a: "; Not a Print "a XOR b: "; a Xor b Print "a shifted left: "; a Shl 1 Print "b shifted right: "; b Shr 1
This demonstrates bitwise operations on Integers. The results are shown in decimal. Bitwise operations are useful for low-level programming and optimization tasks.
Integer in Function Parameters
Functions can accept and return Integer values.
Function Square(n As Integer) As Integer
Return n * n
End Function
Function Sum(a As Integer, b As Integer) As Integer
Return a + b
End Function
Print "Square of 5: "; Square(5)
Print "Sum of 3 and 7: "; Sum(3, 7)
These functions demonstrate passing and returning Integer values. Integer parameters are passed by value by default. The functions perform basic mathematical operations and return Integer results.
Best Practices
- Range Checking: Validate values to prevent overflow.
- Initialization: Always initialize Integer variables.
- Type Selection: Use appropriate-sized integers for needs.
- Constants: Use named constants for magic numbers.
- Comments: Document assumptions about integer ranges.
This tutorial covered the FreeBasic Integer keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.