# Variables, Arrays, Strings

Variables store numbers. For defining variables and giving them initial values, use a declaration like this:

int name;	// uninitialized variable of type 'int'
int name = 123; // initialized variable

This declaration creates a variable of type int with the given name. The name can contain up to 30 characters, and must begin with A..Z, a..z, or an underscore _. Variables must never have names that are already used for functions and for other previously-defined variables.

Also constant expressions can be assigned to variables. Example:

int size = 100/2; // assign the value 50

!!  Be careful when assigning constant expressions to variables. If the constant expression only contains integers (numbers without decimals), the result is also an integer. For instance, the constant (99/2) is 49, but (99.0/2.0) is 49.5. Likewise, the constant 1.0/2 is 0.5, but 1/2 is 0!

### Variable types

Computers always perform their calculations with finite accuracy, so all normal variable types are limited in precision and range. Lite-C supports the following variable types for different purposes:

 Type Size (bytes) Range Step Digits Used for double, var 8 -1.8·10308 to 1.8·10308 2.2·10-308 ~14 prices float 4 -3.4·1038 to 3.4·1038 1.2·10-38 ~6 compact prices fixed 4 -1048577.999 to 1048576.999 0.001 ~9 special purposes int, long 4 -2147483648 to 2147483647 1 ~10 counting, flags short 2 0 to 65536 1 ~4 saving space char, byte 1 0 to 256 1 ~2 text characters bool 4 true, false - - decisions char*, string characters+1 "..." - - text var*, vars elements*8 -1.8·10308 to 1.8·10308 2.2·10-308 ~14 arrays, series mat rows*cols*8+12 -1.8·10308 to 1.8·10308 2.2·10-308 ~14 vectors, matrices DATE 8 12-30-1899 00:00 to doom ~0.1 ms - time, date

Directly entered numbers in the script - such as character constants ('A'), integer numeric constants (12345) or hexadecimal constants (0xabcd) are treated as int. Constants containing a decimal point (123.456) are treated as var. Script constants have a precision of only 6 digits after the decimal, even when you type more digits.

### Typecast operators

Variables are normally automatically converted to another type. So you can assign var to int variables or vice versa without having to care about type conversion. However, sometimes you'll need to convert expressions or variables when the conversion is not obvious - for instance when calling an overloaded function that accepts several variable types. In this case you can enforce a type conversion with a typecast operator. A typecast (or cast) operator is the target variable type in parentheses - f.i. (int) or (var) - placed before the expression or variable to be converted. Example:
var dx = 123.456;
float fy = 0.12345; printf("The integer value of dx is %i, fy is %f",(int)dx,(var)fy); // %i expects the "int" variable type, %f expects "var"

### Global, local, and static variables

Variables can be defined outside functions or inside functions. When defined outside, they are called global variables and are accessible by all functions. They keep their values until the script is compiled the next time. Variable definitions inside functions produce local variables. Local variables only exist within the scope of the function. They are unknown outside and lose their values when the function returns. If a global variable with the same name as a local variable exists, the local variable has priority inside it's function.

A local variable can be declared as static. It is then treated as a global variable even if it is declared within a function, and keeps its content when the function terminates or when another instance of the function is called. This means that it is initialized only the first time the function is called. Example:

function foo()
{
static bool initialized = false;
if (!initialized) { initialize(); } // initialize only once
initialized = true;
...
}
!!  Because static and global variables keep their values until the script is compiled, make sure to initialize them at script start - if(is(INITRUN)) - to their initial values if required.

### Arrays

If you group several variables together, you have an array:
var name[n]; // uninitialized array
var name[n] = { 1, 2, ... n }// initialized global array
This creates a variable that contains n numbers, and optionally gives them default values as in the second line. Note the winged brackets { } that must surround the set of default values. Such a multi-number variable is called an array. The number n is called the length or the dimension of the array. Example:
int my_array[5] = { 0, 10, 20, 30, 40 };

This creates an array of 5 numbers that can be accessed in expressions through my_array[0]...my_array[4]. In the expression the number in the [ ] brackets - the index - tells which one of the 5 numbers of the array is meant. Note that there is no my_array[5], as the index starts with 0. The advantage of using an array, compared to defining single variables, is that any numeric expression also can be given as index. Example:

int i;
for (i=0; i<5; i++)
my_array[i] = i; // sets the array to 0,1,2,3,... etc.

Care must be taken that the index never exceeds its maximum value, 4 in this example. Otherwise the script will crash.

!!  Initializing in the definition works only for simple arrays; multidimensional arrays, arrays that contain something else than numbers, or several arrays defined together in a single logical line must be initialized by explicitly setting their elements. Initializing local arrays makes them static (see below), meaning that they keep their previous values when the function is called the next time. For initializing them every time the function is called, explicitly set them to their initial values, like this:

function foo()
{
var my_vector[3];
my_vector[0] = 10;
my_vector[1] = 20;
my_vector[2] = 30;
...
static var my_static[3] = { 10, 20, 30 }; // initializing local arrays makes them static
...
}

!!  All local variables are stored on a special memory area called the stack. In lite-C the stack has a limited size that is sufficient for many variables, but not for huge arrays. Exceeding the stack size causes a Error 111 message at runtime, so when you need large arrays of ten thousands of variables, declare them outside the function. When you want to determine the array size dynamically, use the malloc / free method. When you need a data structure whose size can grow or change at runtime, use no array, but a dataset.

In C, arrays are in fact pointers to the start of a memory area containing the array elements. So, my_vector[3] is the same as *(my_vector+3).

Arrays can contain not only variables, but also complex data types such as structs. For sorting or searching arrays, the standard C functions qsort and bsearch can be used. They are documented in any C book and available in the stdio.h include file.

Multidimensional arrays can be defined by using several indices:

var Map[10][20];
... Map[j][i] = 10; // j = 0..9, i = 0..19
Computer memory has only one dimension, so the above defined array is in fact still a pointer to a one-dimensional memory area of the size 200 (10*20). Pointer offsets or the & operator can be used to address array elements or pointers to array elements. In the above example, Map[j][i] is the same as *(Map+j*20+i), and &(Map[j][i]) is the same as (Map+j*20+i).

### typedef

You can define your own variable types with typedef statements. Example;
typedef long DWORD;
typedef char byte;

...

DWORD dwFlags;
byte mypixel;
typedef can be used to redefine variables and structs; it can not be used for arrays.

### Strings

Strings are arrays of char variables that contain alphanumerical characters - letters, numbers or symbols - which can be used for messages or names. They are defined this way:

string Name = "characters";

This defines a string with the given name and initializes it to the content characters between the quotation marks. Special characters within a string must be preceded by a '\': \n = Line feed; \\ = Backslash; \" = Quotation mark. The string type is a char pointer (char*) that points to the first character. The last character is followed by a 0 byte to indicate the end of the string.

Any static char array can serve as a string with a fixed maximum length and variable content:

char Name[40];
...
strcpy(Name,"My String!");

After filling this array with characters, it can be used as a string. Care must be taken not to exceed 39 characters, as the last one must be the end mark 0. String functions can be used for setting and manipulating string content.

The content of a string can be compared with a string constant using the '==' operator:

string MyAsset = "AAPL";
...
if(MyAsset == "AAPL")
...

!!  Comparing strings with string constants is not the same as comparing two strings (f.i. if(MyAsset1 == MyAsset2) ...). The latter expression is only true when the two string pointers are really identical. For comparing the contents of two non-constant strings, use standard C library functions such as strstr or strcmp.

!!  Some string functions return temporary strings that are allocated on the CPU stack. Temporary strings can not be used for permanently storing content. They lose their content as soon as another function call returns a temporary string.

### Series

Series are a special array of var variables, normally used for price data, indicators, or data curves. If not given otherwise, the length of a series is determined by the global variable LookBack. Series data is stored in reverse order, i.e. the latest data is at the begin of the array.