WIDTHOF

The WIDTHOF function takes an expression and returns its width. For an variable of type INT, the width is in bits; for a STRING variable the width is in characters; for an array the width is the number of elements.

Syntax

WIDTHOF( expression )

Parameters

expression
An expression of type INT or STRING or an array whose width should be returned.

Return value

An INT value whose meaning varies depending on the type of the expression:

INT
The width in bits of the value. If expression is a variable with an explicit width specified, then the width returned will be that specified width as a minimum, but could be larger if a wider value has been assigned to the variable. In all other cases, the width is the minimum number of bits required to represent the value. If the value is zero, then the width is one, not zero.
STRING
The number of bytes in the string.
Array
The number of elements in the array.

Examples

INT x WIDTH 32 := 1;
INT y := 42;
STRING s := "abc";

PRINT("WIDTHOF(0):  ", WIDTHOF(0), "\n");
PRINT("WIDTHOF(42): ", WIDTHOF(42), "\n");
PRINT("WIDTHOF(x):  ", WIDTHOF(x), "\n");
PRINT("WIDTHOF(s):  ", WIDTHOF(s), "\n");
PRINT("WIDTHOF(y):  ", WIDTHOF(y), "\n\n");

y := 0;

PRINT("WIDTHOF(y):  ", WIDTHOF(y), "\n");

The output from this code would be:

WIDTHOF(0):  1
WIDTHOF(42): 6
WIDTHOF(x):  32
WIDTHOF(s):  3
WIDTHOF(y):  6

WIDTHOF(y):  6

Note that the width of y is expanded to 6 to accommodate the value 42, but doesn't contract when it is assigned a smaller value.