Types and variables
Base types
Every type has two spellings. A PureBasic type tag goes after the name (count.l). A C type name goes before it (long count).
| C name | Tag | Size | Range / notes |
|---|---|---|---|
byte |
.b |
1 | −128 … 127 |
ubyte |
.a |
1 | 0 … 255 |
word |
.w |
2 | −32768 … 32767 |
uword |
.u |
2 | 0 … 65535 |
char |
.c |
2 | a UTF-16 code unit, 0 … 65535 |
long |
.l |
4 | 32-bit signed |
int |
.i |
8 | pointer-sized: 8 bytes on 64-bit targets, 4 on 32-bit |
quad |
.q |
8 | 64-bit signed |
float |
.f |
4 | single precision |
double |
.d |
8 | double precision |
bool |
1 | #True (1) or #False (0) |
|
string |
.s or $ |
8 | text, UTF-16, managed automatically. See Strings |
string8 🔜 |
.s8 |
8 | 8-bit text (UTF-8) |
long is 32 bits, as in PureBasic, not as in 64-bit Linux C.
Declaring variables
A variable can simply be used. Its first use declares it, with the type from its tag, or int without one:
OpenConsole()
count = 10 ; int
ratio.d = 0.5 ; double
name$ = "Ada" ; string
PrintN(name$ + " " + Str(count) + " " + StrD(ratio))
Output:
Ada 10 0.5
The tag is only needed the first time. After that, ratio alone refers to the double.
Explicit declarations, in either style:
OpenConsole()
Define total.q = 0, label$ = "sum"
Global limit.i = 100
int width = 640
double scale = 1.5
PrintN(label$ + ": " + Str(width * 2) + ", " + StrD(scale * 2))
Output:
sum: 1280, 3
Global, Protected, Static, Shared, Threaded and Define declare arrays, lists and maps too: Global Dim table(9), Global NewList names.s(). One line can mix them with variables, and a type after the keyword (Global.w) is the default for the variables on the whole line:
OpenConsole()
Global count = 2, NewList names.s(), Dim grid(3, 3), total.d = 0.5,
NewMap ages()
AddElement(names()) : names() = "Ada"
grid(3, 3) = count * 10
ages("Ada") = 36
PrintN(names() + " " + Str(grid(3, 3)) + " " + Str(ages("Ada")) + " " + StrD(total))
Output:
Ada 20 36 0.5
A variable can have the same name as a procedure or a library function (len = Len(a$), day = Day(d)): written without brackets, the name is the variable; with them, it's the call.
Scope
| Declared with | Where | Visible |
|---|---|---|
Global x |
anywhere | everywhere, including procedures |
int x (C style) at top level |
top level | everywhere, like Global |
Define x, or first use |
top level | main code only, not inside procedures |
Protected x, a parameter, or first use |
in a procedure | that procedure. Set to 0 on every call |
Static x |
in a procedure | that procedure. Keeps its value between calls |
Threaded x |
anywhere | everywhere, like Global, but each thread has its own |
Shared x |
in a procedure | gives the procedure access to a main-code variable |
Global calls = 0
level = 7 ; main code only
Procedure Visit()
Static visits
Protected temp
calls + 1
visits + 1
temp + 1 ; always 1: Protected starts at 0 on each call
ProcedureReturn visits * 10 + temp
EndProcedure
OpenConsole()
Visit()
PrintN(Str(Visit()) + " " + Str(calls) + " " + Str(level))
Output:
21 2 7
Visit can't see level, because main-code variables are private to main code. Inside a procedure, level would be a new local variable that starts at 0. The compiler warns about that (W6), because it is usually a mistake. Use Global or Shared to share a variable.
Requiring declarations
EnableExplicit (or #explicit) makes using an undeclared name an error from that point on. That catches typos, which would otherwise quietly create a new variable.
EnableExplicit
Define count
count = 1
cuont = 2 ; error: 'cuont' is not declared
Conversions
- An integer stored in a float variable is converted exactly (as far as the float can represent it).
- A float stored in an integer variable is rounded, not truncated (PureBasic rule).
n = 2.7stores 3. - If any value in an arithmetic expression is a float, the whole expression is calculated in floating point. So is an expression stored into a float variable:
f.d = 10 / 3stores 3.333…, butn = 10 / 3stores 3.
OpenConsole()
f.d = 10 / 3
n = 10 / 3
m = 2.7
PrintN(StrD(f) + " " + Str(n) + " " + Str(m))
Output:
3.3333333333 3 3