Which Data Type To Use?
There are a number of different data types available, so how do you choose the
right data type for any given application? The rule of thumb is to use the largest data
type you need to hold the expected range of values. This may seem like stating the
obvious, but many programs fail because the programmer did not fully understand the
range of data in their program. When you crate a program, you should map out not only
the logic of the program, but the data associated with each block of logic. When you map
out the data ahead of time, you are less likely to run into data-type errors.
For example, if you were working with ASCII codes, which range from 0 to 255, an
ubyte would be a good choice since the range of an ubyte is the same as the range of
ASCII codes, and you are only using 1 byte of memory. There is another consideration
though, the “natural” data size of the computer. On a 32-bit system, the natural data size
is 4 bytes, or an integer. This means that the computer is optimized to handle an integer,
and does so more efficiently, even though you are “wasting” 3 bytes of memory by using
an integer for an ASCII code.
In most cases an integer is a good general-purpose choice for integer data. The
range is quite large, it handles both negative and positive numbers and you benefit from
using the computer’s natural data type. For floating point data, a double is a good choice
since, like the integer, it has a good range of values and better precision than a single.
For large integer data you should use a uinteger for positive data or a longint for large
negative and positive numbers. These are only suggestions; what data type you end up
using will be dictated by the needs of your program.
These “rules of thumb” apply to single variable declarations where a few wasted bytes
are not critical. However, as you will see in the chapter on arrays, choosing the right
sized data type is critical in large arrays where a few wasted bytes can add up to a
large amount of wasted memory.
You may notice when looking at other people's source code, the compiler directive
Option Explicit has been used at the top of each program. A compiler directive is code
that instructs the compiler to do something. In the case of Option Explicit, it instructs the
compiler to make sure that any variable being used has been properly Dim’ed. Although
you can write a program without using Option Explicit and explicitly Dim’ing each
variable, you run the risk of introducing nasty, hard-to-find bugs as the following short
Listing 2.2: nooptionexplicit.bas
Online Change your PDF file Permission Settings
You can receive the locked PDF by simply clicking download and you are good to go!. Web Security. If we need a password from you, it will not be read or stored. change password on pdf; create pdf password
In line 1 myInteger is being declared to the compiler. The compiler will
use this declaration to allocate space for the variable. In line 3 the variable is being
initialized to a value, in this case 3. The Print statement in line 5 will print out the result
to the console screen. Sleep, listed in line 7, will wait for a keypress and the End keyword
in line 8 will end the program. The End statement isn't mandatory at the end of a
program but should be used especially if you want to return an exit code to the operating
After typing this program into FBIde and running the program, you should see the
Output 2.2: nooptionexplicit.bas
The output should be 5, but as you can see, the result isn’t what was expected or
wanted. Since Option Explicit wasn’t used in the program, the compiler created a new
variable when it encountered the misspelled variable name, myIntger, in the print
statement. If this was large and complicated program, you can see how difficult it would
be to track this error down and fix it.
Now, add Option Explicit at the top of the program and run it again. You should see
the following error message in the results pane of FBIde.
Output 2.3: Modified nooptionexplicit.bas
Here the compiler detected the misspelled variable name and informed us of the
problem. You should always use Option Explicit in your programs. A few seconds of extra
typing will save you hours of frustration.
A Note about Option Explicit
Recent versions of the FreeBASIC compiler do not allow OPTION commands
including OPTION EXPLICIT. From now on OPTION EXPLICIT is implied and your programs
will behave as if you used OPTION EXPLICIT. It is a good rule of thumb to always implicitly
declare your variables, so get in the habit now. To compile a source file that uses OPTION
EXPLICIT, either use “-lang deprecated” or remove the OPTION EXPLICIT line from the
A Look Ahead
When working with numeric data, there are times when it becomes necessary to
convert one data type to another. FreeBasic offers two conversion methods, implicit and
explicit, which you will see in the next chapter.
1) What data type would be the best to store the number 196?
2) What data type would be the best to store the number 2.134?
3) What data type is the best for general usage on 32-bit systems?
4) What is the difference between signed and unsigned data types?
5) What prefix would you use to designate a binary number?
6) What prefix would you use to designate a hexidecimal number?
7) What Alphabetic letters are allowed in a hexidecimal number?
8) What would the hexidecimal number 1AF be in decimal form?
Converting Numeric Data Types
When you work with numbers there will come a time when you will need to convert
a variable from one data type to another data type. In FreeBasic there are two types of
data conversion. Implicit, where the compiler will automatically convert the data types
during an assignment or calculation, and explicit using one of the predefined conversion
Implicit Data Conversion
Implicit data conversion occurs either through an assignment statement, or as the
result of a calculation. Where implicit data conversion can cause a problem is in the loss
of precision that can result when converting a large data type to a smaller data type.
While implicit data conversion eases the programming process somewhat, you want to
make sure that the results you are getting are what you expect. You should always check
the result of an implicit conversion to make sure the range of values being converted is in
the expected range.
The following short program illustrates the conversion that takes place during an
Listing 3.1: assignconv.bas
In lines 1 and 2 two variable are being declared, myDbl, a double-type
varibale and myInt, an integer-type variable. Line 3 is a comment which, as you can see,
starts with the ' (single quote) character. In line 4 the double-type variable is being
initialized to the floating-point value 5.56. In line 6 the double is being assigned to an
integer variable, invoking the implicit conversion. In line 7 the Print displays the result on
the console window. In line 8 the program waits for a key press with the Sleep statement
and in line 9 the End statement is used to end the program.
Running the program will result in the following output.
Output 3.1: assignconv.bas
In the program, the value of myDbl which is a double-type variable, was set to
5.56. When myInt was assigned this float value, the compiler converted the value to an
integer, and then rounded it up to 6. Maybe you were just looking for the whole number
portion of 5? In this case, your result would be incorrect, although you may not know until
later in the program. This type of subtle bug is another one of those problems that are
hard to track down and fix.
Caution Even if the compiler is producing the correct result, there is no guarantee
that future versions of the compiler will. It may be necessary to change the behavior of
the compiler to add features or fix a bug, and you may find yourself with a program that
suddenly quits working.
The next little program illustrates the implicit conversion that takes place during a
calculation. In this example, the two integer operands are converted to double-types
during the division calculation.
Listing 3.2: calcconv.bas
Line 1 and 2 in the program are the alternate Dim statement formats. You
can use this format to declare multiple variables of the same type. In line 4 and 5, the
variables are initialized. In line 6 the / character is the division operator. The result of the
division operation will be implicitly converted to a double-type in the assignment
statement. Line 7 prints the newly converted value to the screen and in lines 8 and 9, the
program will wait for a key press and then end.
Running the program produces the following output.
Output 3.2: calconv.bas
The result is as expected since the double-type has a greater precision than the
integer-type. However, consider this program and its output, which demonstrates the
implicit rounding during calculations.
Listing 3.3: calcconv2.bas
This program is similar to the program in Listing 4.2 except the
conversion process is from two double-type variables to an integer. Lines 1 and 2 declare
the working variables. Lines 4 and 5 set the values of the two double-type variables. In
line 6 the double-type division result is implicitly converted to an integer, resulting in
precision loss. Line 7 prints the result to the console window and lines 8 and 9 wait for a
key press and end the program.
The output of the program:
Output 3.3: calconv2.bas
In this example, the two double-type variables were converted to integers and
then the division operation was performed. Since 5.6 was rounded up to 6 during the
conversion, the result is 2 rather than 1. This may not be a problem, but you should be
aware that these types of conversions occur when working with mixed precision types.
Explicit Data Conversion
There is an alternative to implicit data conversion, explicit data conversion where
you use one of FreeBasic’s built-in conversion functions. Since these functions are
designed for conversion, they return consistent results and are unlikely to change even if
the implicit rules of the compiler change. Even though it is more work, it is always safer
to explicitly convert data to the needed data type before carrying out operations on that
Numeric Data Conversion Functions
Table 4.1 lists all the conversion functions. Keep in mind that these functions do
not check for overflow, so be sure that the value passed to these functions is in the
Documents you may be interested
Documents you may be interested