Constants and enumerations

Constants

A PureBasic constant starts with #. Its value is worked out when the program is compiled.

#Width = 640
#Height = #Width * 3 / 4
#Title$ = "Game"

OpenConsole()
PrintN(#Title$ + " " + Str(#Width) + "x" + Str(#Height))

Output:

Game 640x480

A C-style const defines a constant that can be used without #:

const int Lives = 3
const double Gravity = 9.81

OpenConsole()
PrintN(Str(Lives) + " " + StrD(Gravity))

Output:

3 9.81

Both spellings share one set of names: const int Max = 10 and #Max = 10 define the same constant. A constant defined with # is always written with the #. A bare Max is a variable, as in PureBasic.

Enumerations

Enumeration numbers constants 0, 1, 2, … A start value and a Step can follow. EnumerationBinary gives 1, 2, 4, 8, …, for flags.

Enumeration
  #Red
  #Green
  #Blue
EndEnumeration

Enumeration 10 Step 5
  #First
  #Second
EndEnumeration

EnumerationBinary
  #Read
  #Write
  #Exec
EndEnumeration

OpenConsole()
PrintN(Str(#Blue) + " " + Str(#Second) + " " + Str(#Read | #Exec))

Output:

2 15 5

C style:

enum Direction { North, East, South, West }

OpenConsole()
PrintN(Str(West))

Output:

3

Built-in constants

Constant Value
#True, true 1
#False, false 0
#Null, nullptr 0
#Empty$ ""
#CRLF$, #LF$, #CR$, #TAB$, #DQUOTE$ carriage return + line feed, line feed, carriage return, tab, "
#PB_Compiler_OS #PB_OS_Windows, #PB_OS_Linux or #PB_OS_MacOS
#PB_Compiler_Processor #PB_Processor_x64, …
#PB_Compiler_Version the PureBasic version x is compatible with (630)
#X_Compiler_Version the x compiler version
OpenConsole()
PrintN("a" + #TAB$ + "b" + #DQUOTE$)

Output:

a   b"