6.1.0895 CHAR char CORE

( "<spaces>name" -- char )

Skip leading space delimiters. Parse name delimited by a space. Put the value of its first character onto the stack.



Typical use: ... CHAR A CONSTANT "A" ...


T{ CHAR X     -> 58 }T
T{ CHAR HELLO -> 48 }T


BerndPaysanavatar of BerndPaysan Non-XCHAR implementationSuggested reference implementation2015-11-27 20:45:54

: CHAR ( "char" -- c ) BL WORD CHAR+ C@ ;

Reply New Version

mcondronavatar of mcondron Behavior when no text foundComment2019-05-19 13:57:46

When no text is found (i.e. CHAR followed by nothing but spaces until the end of the line) should this be an ambiguous condition, or should CHAR place 0 on the stack?

AntonErtlavatar of AntonErtl

I don't see this defined in the standard at all (not even as ambiguous condition). I guess we should fix this in the standard.

I think a system should react to this condition by reporting an error.

BerndPaysanavatar of BerndPaysan

There's not even a suitable standard throw code available.

Gforth's current behavior is to return whatever character is found after the end of the input buffer, which is a classical read outside boundary problem.

AntonErtlavatar of AntonErtl

I have just fixed this in Gforth, which now produces an error. I have used -16 "Attempt to use zero-length string as a name" as throw code, but it does not quite fit.

mcondronavatar of mcondron

Yes, seems like an error to me, too. Thanks.

JohanKotlinskiavatar of JohanKotlinski

My Forth does REFILL until it finds a char... does that make sense, or is it better to simply ABORT?

Reply New Version

PopovMPavatar of PopovMP Describe Compile time and Run time behaviorComment2022-11-19 09:25:07

I had troubles to make my .f files working in popular distributions until I found CHAR is not supported in Compile / Run time.


Compilation semantics for this word are undefined.


Run-time semantics for this word are undefined.

AntonErtlavatar of AntonErtl

CHAR has default compilation semantics, and that may be confusing. Forth-94 supports [CHAR] for compiling a literal character into a definition. In Forth-2012 there is also the option to use the syntax 'A', which pushes the ASCII code of A at interpretation/run-time.

Reply New Version