Cicada has five primitive variable types: bool, char, int, double and string. The first four of these types occupy a fixed amount storage which may depend on the machine (for example, the int type generally is the same standard integer as C’s int type). However, string variables can store strings (character arrays) of any length.
In order to define a variable, use the define ‘::’ operator. Several variables of the same type can be defined on the same line. As an example, here we define three floating point variables and one string.
height :: time :: g :: double
units :: string
All primitive variables get initialized to some starting value when first created. That value is: 0 for numeric variables (ints and doubles); the null character for char variables; false for Boolean variables; and an empty (0-character) string for string variables.
To change the value stored by a variable, use the assignment operator, which can be written either ‘=’ as in C or ‘<-’. Here we’ll use the C convention.
g = -9.8
units = "m/s^2"
In this case we just assigned a constant value to each variable. Numeric constants are obviously just numbers. String constants are inside double quotes, whereas character constants just use single quotes. The only two Boolean constants are true and false. Of course, all primitive types can also be assigned using more complex expressions: for example isBigger = a > b sets the Boolean variable isBigger to either true or false depending on the values of numeric variables a and b.
For the sake of brevity, we can define and set variables in a single step using the := operator. So our last two examples could have been condensed a bit:
height :: time :: double
g := -9.8
units := "m/s^2"
Cicada has to somewhat guess about numeric types when we do this: is ‘g’ an int or a double? Because of the decimal point Cicada guesses double. Just know that if we had rounded to g := 10, then ‘g’ would have been defined as an integer variable.
By default, Cicada’s bool is stored internally as a byte, the int is a basic integer and the double type is a double-precision floating point. (See Table 1.) However, these can be reassigned to different C types: for example some applications might prefer Cicada’s double type to be C’s long double data type. To change a data type, open up the lnklst.h source file and change the appropriate #typedef (see the ‘C type name’ column in Table 1).
Last update: May 8, 2024