Literals

FreeBASIC

Literals
 
Non-variable compile-time string, numeric values and boolean values.

Literals are numbers, strings of characters or boolean truths specified directly in the source code. Literal values may be used by assigning them to a variable or constant, passing them to a procedure, or using them in an expression.

Numeric literals come in two forms - integer and floating-point.

Integer Literals


Decimal
Decimal digits ( 0 1 2 3 4 5 6 7 8 9 ).
Note: to get negative values, a "-" sign (Operator - (Negate)) can be placed before a numeric literal

Dim x As Integer = 123456
Dim b As Byte = -128


Hexadecimal
"&H;", followed by hexadecimal digits ( 0 1 2 3 4 5 6 7 8 9 A B C D E F ).

Dim x As Integer = &h1E240
Dim b As Byte = &H80

Octal
"&O;" ( O as in "Octal" ), followed by octal digits ( 0 1 2 3 4 5 6 7 )

Dim x As Integer = &O361100
Dim b As Byte = &O200

Binary
"&B;", followed by binary digits ( 0 1 )

Dim x As Integer = &B11110001001000000
Dim b As Byte = &B10000000


Integer size suffixes

If an integer literal suffix is not given, the number field size required to hold the literal is automatically calculated. Specifying a size suffix guarantees that the compiler will consider a number as a specific integer size.

Integer literals ending with:
  • "%", are considered as signed 32/64 (depending on platform) bit integers. (Integer)
  • "L", "&", are considered as signed 32 bit long integers. (Long)
  • "U", are considered as unsigned 32/64 (depending on platform) bit integers. (UInteger)
  • "UL", are considered as unsigned 32 bit integers. (Ulong)
  • "LL", are considered as signed 64 bit integers. (LongInt)
  • "ULL", are considered as unsigned 64 bit integers. (ULongInt)

The prefixes, suffixes, and hexadecimal letter digits are all case-insensitive.

Dim a As Long = 123L
Dim b As UInteger = &h1234u
Dim c As LongInt = 76543LL
Dim d As ULongInt = &b1010101ULL


Floating Point Literals

Floating point numbers are specified in decimal digits, may be positive or negative, have a fractional portion, and optionally an exponent. The format of a floating point literal is as follows:

number[.[fraction]][((D|E) [+|-] exponent)|(D|E)|][suffix]
or
.fraction[((D|E) [+|-] exponent)|(D|E)|][suffix]

By default, floating point numbers that do not have either an exponent or a suffix are considered as a double precision floating point value, except in the -lang qb dialect, where numbers of 7 digits or fewer are considered to be single precision.
Dim a As Double = 123.456
Dim b As Double = -123.0


The letter "D" or "E", placed after the number/fraction part, allows the number to be given an exponent. The exponent may be specified as either positive or negative with a plus ("+") or minus ("-") sign. Exponents that do not have a sign are positive.
An exponent is not required after the letter, so the letter can be used on its own just to specify the type. "D" specifies a double-precision floating-point number. "E" specifies a floating-point number using the default precision. When the letter is used on its own in combination with a suffix (see below) the type denoted by the suffix overrules the type specified by the letter.

Dim a As Double = -123.0d
Dim b As Double = -123e
Dim c As Double = 743.1e+13
Dim d As Double = 743.1D-13
Dim e As Double = 743.1E13
Dim f As Single = 743D! 


A suffix of "!" or "F" on a number specifies a single precision (32 bit total) floating point value. A suffix of "#" specifies a double precision float.
Note that the letter suffixes and exponent specifiers are all case-insensitive.

Dim a As Single = 3.1!
Dim b As Single = -123.456e-7f
Dim c As Double = 0#
Dim d As Double = 3.141592653589e3#


String Literals

String literals are a sequence of characters contained between two double quotes. The sequence of characters escaped or non-escaped.

Double quotes can be specified in the string literal by using two double quotes together.
Print "Hello World!"
Print "That's right!"
Print "See the ""word"" contained in double quotes."


String literals can contain escape sequences if the string literal is prefixed by the ! Operator (Escaped String Literal). See Escape Sequences for a list of accepted escape sequences.
Print !"Hello\nWorld!"


By default, string literals are non-escaped unless Option Escape was used in the source in which case all string literals following are by default escaped.

A string may be explicitly specified as non-escaped when prefixed by the $ Operator (Non-Escaped String Literal).
Print $"C:\temp"


Besides ASCII files with Unicode escape sequences (\u), FreeBASIC can parse UTF-8, UTF-16LE, UTF-16BE, UTF-32LE and UTF-32BE source files allowing unicode characters directly in the string literal.

Boolean Literals

The boolean type has two values, represented by literals True and False.

Dim a As Boolean = False
Dim b As Boolean = True



See also