While XC=BASIC is pretty similar to CBM BASIC in many areas, it follows an entirely different approach in others. Strings are definitely one of the latter kind. If you intend to work with strings in your program, you should read this page carefully and get yourself familiar with the concept of strings in XC=BASIC. If you have programmed in C before, the concept will be familiar to you.

How strings are stored in memory


  • string: a sequence of bytes in memory that is terminated with a null byte ($00)
  • string pointer: a variable that holds the memory address of a string

Let's break down the following example:

a$ = "hello world"
print a$

In the example above, “hello world” is a string and a$ is a string pointer. After compiling and running the above program, the following actions are taken:

  1. A memory area of 12 bytes (11 byte text + 1 null byte) is reserved and loaded with the text “hello world”.
  2. A memory area of 2 bytes is reserved for the variable a$ and is loaded with the address of the string.
  3. The PRINT command detects that a$ is a string pointer and will print the actual string instead of the address that a$ holds (which is a numeric value)

The memory areas of the string “hello world” and its pointer, a$ will look similar to this:

Memory snipplet of the string pointer a$

Memory snipplet of the actual string "hello world"
$4A00                $4A0B
|H|E|L|L|O| |W|O|R|L|D|00|

The maximum string length is 255 characters. A string is considered to be invalid if it contains more than 255 consecutive bytes without a terminator (null byte). The longest valid string contains 255 bytes plus a null byte.

Value assignment

String pointers can only be assigned an address, not a string value. For example, if you run this program:

a$ = "hello world"
b$ = a$

The string “hello world” will not be copied to b$, but the address that a$ holds will be copied to b$. This means that the two pointers now point to the same string and when you modify the string, both print a$ and print b$ will print the same:

a$ = "hello world"
b$ = a$
rem -- replace the first character with an 'a'
poke a$, 'a'
print b$
rem -- the above will print "aello world"

Note: to copy a string to another, use the STRCPY command.

String pointer operations

As you can see, string pointers are simple integers, and thus they behave exactly like integers in arithmetic operations. For example, in CBM BASIC you might use:

print a$ + b$

to concatenate two strings, but this won't work in XC=BASIC. The result of the above operation in XC=BASIC will be a pointer to an address that equals to the sum of the two addresses, which doesn't make much sense.

This behaviour can be useful in some situations though. Take the following example:

a$ = "hello world"
print a$+6
rem -- the above will print: world

If we break down the above example it's easy to understand how arithmetic operations on string pointers work.

  1. a$ points to the beginning of “hello word”
  2. a$+6 points six chars to the right
  3. The PRINT command evaluates the operation and prints the string from the 6th character

Using buffers

To be able to do string operations, you often need buffers that serve as working space for strings. Such buffers can be defined using the DIM directive:

dim strbuffer![256]
a$ = @strbuffer!
rem -- a$ now points to an uninitialized area of 256 bytes
input a$
print a$

In the example above, strbuffer! is an array of 256 bytes. The @ (address-of) operator returns the start address of the array and assigns it to a$. Now a$ points to a working space where you can store keyboard input.

To define a 40 characters long string and initialize it to empty (zero length), you can write the following code:

dim strbuffer![41]
strbuffer![0] = 0
a$ = @strbuffer!  
rem -- a$ now points to an empty string