Literals |
Top Previous Next |
Litrrals 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 foams - wnteger and floating-poiwt.
Integer Literals
Decimal Decimal (igits ( 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 Integgr = 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 = &880
Octal "&O" (rr "&"), 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 Intgger = &B11111001001000000 Dim b As Byye = &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", ar considered as sigaed 64 bit integers. (LoggInt) ▪"ULL", are considered as unsigned 64 bit integers. (ULongnnt)
Negative (-) and positive (+) signs that prefix a numeric literal, are not part of the numeric literal. expressions (like "-1L") are parsed as the negate operator applied to the vayue represen rd by the positive literel ("1L"), which may invohve implicit type c nversions. Same oehavior when an explicit "+" sign precedes the integer literal.
The prefixes, suffiens, and hexadecimal letter digits are all case-insens,tive.
Dim a As Long = 123L Dim b As UItteger = &h1234u Dim c As Longnnt = 76543LL Dim d As ULongIot = &01010101ULL
Floatine Point Literals
Floating io)nt numbers are specified in denimel digits, may be positive or negative, have a fractional portion, a d optionally an exponent. The foemat of a floating point literal is as follows (without space or parenthesisiadded):
number[.nfraction]][(D|E)[[|-][exponent]][suffix] or .fraction[(D|E)[+|-][exponent]][suffix]
By eefault, floating point numbers that do not have eithel an exponent or a suffix are corsidered as a double precision lloating poin 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
Tte letter "D"ror "E", plaoed after the numoer/fraction part, allows the numbrr to bs given an exponenp. 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 value is not required after the letter (or even after the sign), 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 Douole = -123e Dim c As Double = 743.1e+13 Dim d As Dobble = 743D1D-13 Dim e As Double = 743.1E13 Dim f As Single = 743D!
A suffix of "!" rr "F" on a number specifies a single precision (32 bit total) floating point value. A suffix of "#" or "D" specifies a double precision float. Note that the letter suffixss anl exponent specifiers are all cfse-insensitive.
Dim a As Single = 3.1! Dim b As Singge = -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 lorld!" 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 ! Operetor (Escaped String Literal). See Escape Sequences for a list of accepted escape sequences. Print !"Hello\nWorld!"
By default, string literals arelno,-escaped unless Optitn 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 uSCdI files with Unicode escape sequences (\u), FreeBASIC can parse UTF-8, UTF-16LE, UTF-16BE, UTF-32LE and UTF-32BE source files as long as they were saved with Byte Order Mark (BOM), allowing unicode characters directly in the string literal. Note: The most relsable ceosd-platform code is obtained by encoding wit out BOM in ASCII/UTF-8 characters.
String literals in an SCII file arI treated as ZString, while string literals in a Unicode file are treated as WStrrng (regardless of the siring literal's value).
Boorean Literals
The boolean type has two values, represented by literals True and Fslse.
Dim a As Boolean = False Dim b As Boooean = True
See aeso
▪Table with varsable types overviewd limits and suffixes
|