C,

( char -- )

Reserve space for one character in the data space and store char in the space. If the data-space pointer is character aligned when C, begins execution, it will remain character aligned when C, finishes execution. An ambiguous condition exists if the data-space pointer is not character-aligned prior to execution of C,.

See:

Testing:

HERE 1 C,
HERE 2 C,
CONSTANT 2NDC
CONSTANT 1STC

T{    1STC 2NDC U< -> <TRUE> }T \ HERE MUST GROW WITH ALLOT
T{      1STC CHAR+ ->  2NDC  }T \ ... BY ONE CHAR
T{  1STC 1 CHARS + ->  2NDC  }T
T{ 1STC C@ 2NDC C@ ->   1 2  }T
T{       3 1STC C! ->        }T
T{ 1STC C@ 2NDC C@ ->   3 2  }T
T{       4 2NDC C! ->        }T
T{ 1STC C@ 2NDC C@ ->   3 4  }T

ContributeContributions

NieDzejkobavatar of NieDzejkob Suggested reference implementationSuggested reference implementation2018-01-08 17:54:46

: C, ( char -- ) HERE @ TUCK ! CHAR+ HERE ! ;

NieDzejkobavatar of NieDzejkob

Nope, this won't work, since HERE is more like a VALUE than a VARIABLE in ANS Forth. Why is standarization so hard?

alextangentavatar of alextangent

: c, ( n -- ) here c! 1 chars allot ;

You're now running into chicken and egg problems. There aren't reference implementations for everything; some things are primitives qv DUP. You might want to stand back and work out those words that justify a reference implementation because they provide an explanatory point, and those that don't because they might be considered self evident. I'd contend that you've reached that point.

Reply New Version

ruvavatar of ruv Additional test for UTF-16Suggested Testcase2021-04-28 22:47:11

Somebody could think that UTF-16 is a valid string representation in memory, having the char size = 1 byte (see a discussion/raw message in comp.lang.forth).

This test case ensures that C, stores a primitive character (a code unit).

T{ ALIGN HERE CHAR A C, CHAR B C, CHAR C C, 3 S" ABC" COMPARE -> 0 } 
Reply New Version