Next: , Previous: Memory Tutorial, Up: Tutorial


3.24 Characters and Strings

On the stack characters take up a cell, like numbers. In memory they have their own size (one 8-bit byte on most systems), and therefore require their own words for memory access:

     create v4
       104 c, 97 c, 108 c, 108 c, 111 c,
     v4 4 chars + c@ .
     v4 5 chars dump

The preferred representation of strings on the stack is addr u-count, where addr is the address of the first character and u-count is the number of characters in the string.

     v4 5 type

You get a string constant with

     s" hello, world" .s
     type

Make sure you have a space between s" and the string; s" is a normal Forth word and must be delimited with white space (try what happens when you remove the space).

However, this interpretive use of s" is quite restricted: the string exists only until the next call of s" (some Forth systems keep more than one of these strings, but usually they still have a limited lifetime).

     s" hello," s" world" .s
     type
     type

You can also use s" in a definition, and the resulting strings then live forever (well, for as long as the definition):

     : foo s" hello," s" world" ;
     foo .s
     type
     type
Assignment: Emit ( c -- ) types c as character (not a number). Implement type ( addr u -- ).

Reference: Memory Blocks.