Menu

How to print " in a string

2023-04-21
2023-04-21
  • Jan Kromhout

    Jan Kromhout - 2023-04-21

    I need to print out over the serial. => t0.txt="9999"
    In C I can use print("t0.txt=\"9999\""
    Forth is not accepting the \, how should I program this in forth?

    Cheers,

    Jan

     
    👍
    1
  • Mikael Nordman

    Mikael Nordman - 2023-04-21

    FF is a bit simple to keep the size down. with [CHAR] you can get the ASCII code for any character and print it with EMIT.
    [CHAR] is for compilation mode, while CHAR works in interpretation mode.

    : quote. ( n -- ) [char] " emit . [char] " emit ;
    

    But the DOT word prints an extra space after the number which may not be what you wanted

    : "" [char] " emit ;
    : ... <# dup abs false #s rot sign #> type ;
    : "". ( n -- ) "" ... "" ;
    

    Here is an example quoting a string

    : "".s ( a n -- ) "" type "" ;
    : hello ( -- a n ) s" hello" ;
    hello "".s 
    "hello"
    
     
    👍
    1

    Last edit: Mikael Nordman 2023-04-21

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.