User Tools

Site Tools


datatypes

Data Types

SharpBASIC supports 23 predefined data types (on 64 bits architectures). The predefined data types can be categorized in numeric types, character or string types and general buffer types.

Data types do not have traditional names such as short, long or longint. This is because with each new architecture these names usually have a different word-size. To avoid confusion, SharpBASIC adds the bit-size to numeric data types, such as int8 and uint8 for 8-bits signed and unsigned.

numeric datatype

The following numeric data types are supported:

bool         - boolean (false (0) or true (-1)
int8         - 8 bits signed integer 
uint8        - 8 bits unsigned integer
int16        - 16 bits signed integer
uint16       - 16 bits unsigned integer
int32        - 32 bits signed integer
uint32       - 32 bits unsigned integer
int64        - 64 bits signed integer
uint64       - 64 bits unsigned integer
int          - signed integer, architecture default size
uint         - unsigned integer, architecture default size
itr          - signed iterator, equivalent to int
uitr         - unsigned iterator, equivalent to uint
real32       - 32 bits (4 bytes) floating point
real64       - 64 bits (8 bytes) floating point
real80       - 80 bits (10 bytes) extended floating point
ptr          - pointer, architecture default size
hnd          - handle, equivalent to int

Example of numeric data type declarations:

dim a: int;
dim b, c, d: uint;
dim x, y, z: real32 = 0;
dim p: ptr;

string datatype

The following string data types are supported:

str          - null-terminated string
str * n      - fixed length string, not null-terminated

Examples of string data type declarations:

const msg = "these are character data types";
dim text: str;
dim f: str * 255;

The general string type str is a null-terminated variable length string or a constant string. The fixed-length string str * n has a fixed length of n bytes and is not null-terminated. This means that for fixed-length strings the null-character is a valid character with no special meaning.

buffer datatype

The buffer datatype is a general memory buffer type, which can be declared as either byte (8 bits), word (16 bits), dword (32 bits) or qword (64 bits). Buffer types are declared with the dim statement and the length of the buffer must be specified similar to the fixed-length string. The difference between a buffer type and a fixed-length string is that a buffer type does not have a descriptor.

Example of buffer type declarations:

dim bdata: byte * 256;      ' 256 bytes buffer
dim wdata: word * 256;      ' 512 bytes buffer (256 * 2)
dim ddata: dword * 256;     ' 1024 bytes buffer (256 * 4)
dim qdata: qword * 256;     ' 2048 bytes buffer (256 * 8)

Buffer types cannot be part of expressions, but they can be assigned values with the function cbuf.

typof

The built-in function typof can be used to determine an identifier’s type. The function returns the following codes:

01           - bool
02           - int8
03           - uint8
04           - int16
05           - uint16
06           - int32
07           - uint32
08           - int64
09           - uint64
10           - int
11           - uint
12           - itr
13           - uitr
14           - real32
15           - real64
16           - real80
17           - [reserved]
18           - ptr
19           - hnd
20           - str
21           - byte (buffer type)
22           - word (buffer type)
23           - dword (buffer type)
24           - qword (buffer type)
25           - enum (enumeration)
26           - record (record)
27           - struc (structure)

Example:

dim s: str = "Hello world!";

main do
  ' string?
  if typof(s) == 20 do
    print(s);
  end
end

datatypes.txt · Last modified: 2023/06/30 12:20 by admin