Variables, Pointers, Constants, Scopes

Top  Previous  Next

Tibbo BASIC and Tibbo C support several simple variable types. These are signed and unsigned 8-, 16-, and 32-bit variables.

In addition, there is also a string type, which is unique to Tibbo BASIC and C.

Tibbo C supports static variables, while Tibbo BASIC does not.

 

Arrays and structures are supported in BASIC and C but the storage format in RAM differs between the two languages and there is no compatibility: you cannot pass an array or a structure from a BASIC procedure to a C function, or vice versa.

 

Tibbo C also supports unions. No surprises here.

 

Defining and declaring variables

All variables must be explicitly declared. Variables must be defined or declared before they are used.

Each variable has a fixed type and there are no "invariants" (variables that can change their type as needed).

 

Tibbo BASIC:

Tibbo C:

dim x as byte=5 'define first...
x=x+1 '...then use
 
'OR
 
declare xx as byte=5 'declare first...
xx=xx+1 '...then use
dim xx as byte 'define elsewhere

unsigned char x=5; //define first...
x=x+1; //...then use
 
//OR
 
extern unsigned char xx; //declare first
xx=xx+1; //...then use
unsigned char xx; //define elsewhere

 

Multi-variable definitions and declarations are OK, too.

The syntax of Tibbo BASIC doesn't allow you to initialize each variable in the multi-variable dim statement to the desired value.

C allows that:

 

Tibbo BASIC:

Tibbo C:

dim x,y as byte=5 'x will be 0 and y will be 5.
x=2 'you will have to initialize x separately
 
'AND ELSEWHERE...
 
declare x,y as byte

unsigned char x=2,y=5; //There is no problem doing this in C
 
 
//AND ELSEWHERE
 
extern unsigned char x,y;

 

In Tibbo BASIC and in Tibbo C there is no need to put all variable definitions on top of the procedure (function). You can mix "procedural" lines and new variable declarations and definitions:

As the Scopes topic explains, Tibbo BASIC and C create sub-scopes within control structures, such as if-then-else. You can define "sub-local" variables there as well.

 

Tibbo BASIC:

Tibbo C:

sub test
  dim i as word
  i=i+10
  dim s as string 'this variable is defined after some procedural code
end sub

void test(){
  unsigned int i;
  i+=10;
  string s; //'this variable is defined after some procedural code
}

 

Pointers (Tibbo C only)

Tibbo C supports pointers (and Tibbo BASIC doesn't).

Pointers are implemented in the standard manner, which is both the strength and the weakness of ANSI C.

Pointer arithmetic strengths are plentiful and well-known. I won't delve into compliments here.

The weakness is that C pointers are not safe. It's easy to wreck havoc with a poorly written pointer-wielding application.

Compared to Tibbo BASIC, which is a completely safe programming environment that won't allow bad code to corrupt application memory or TiOS, Tibbo C is an "almost safe" environment.

The "culprits" breaking the "safety record" of Tibbo C are pointers, arrays, structures, and unions.

 

One non-standard feature of Tibbo C that sets it apart from ANSI C is the string variable type.

The String Type and Pointers topic contains some interesting info on strings, as well as pointers to this unique variable type.