Literals/String: Difference between revisions

mNo edit summary
(→‎{{header|Forth}}: Add Fortran)
Line 517:
<lang forth>'c emit
s\" hello\nthere!"</lang>
 
=={{header|Fortran}}==
In Fortran, spaces are nowhere significant, not even within words, so that <code>GOTO = G OTO = GO TO</code> and there are no reserved words either so GOTO might be the name of a variable as well and both can be used in the same source. Statements were originally specified with a special format: columns one to five were for statement labels (integers only, not zero), column six specified a continuation line if not blank or zero, and columns 73-80 were for sequence numbers in case you dropped the deck of cards. Thus, source was found in columns 7-72, and layout of the source could be varied without effect on the results.
 
Fortran IV introduced a text literal, specified within apostrophes, with two apostrophes in a row indicating an apostrophe within the text. Later, either an apostrophe or a double quote could be used to start a text string (and the same one must be used to end it) so that if one or the other were desired within a text literal, the other would be used as its delimiters. If both were desired, then there would be no escape from doubling for one. Because spaces are significant within text literals, a long text literal continued on the next line would have the contents of column seven of the continuation line immediately following the contents of column 72 of the continued line. F90 formalised an opportunity for free-format source files; many compilers had also allowed usage beyond column 72.
 
Within the text literal, any character whatever may be supplied as text grist, according to the encodement recognised by the card reader as this was a fixed-format file - cards have an actual physical size. This applied also to source text held in disc files, as they were either fixed-size records or, for variable-length records, records had a length specification and the record content was not involved. Variable-length records were good for omitting the storage of the trailing spaces on each line, except that the sequence numbers were at the end of the line! In this case they might be omitted (unlike a card deck, a disc file's records are not going to be dropped) or there may be special provision for them to be at the start of each line, with the source text's column one staring in column nine of the record. But, for the likes of paper tape, the question "How long is a record?" has no natural answer, and record endings were marked by a special symbol. Such a symbol (or symbol sequence) could not appear within a record, such as within a text literal and be taken as a part of the text. This style has been followed by the ASCII world, with variously CR, CRLF, LFCR and CR sequences being used to mark end-of-record. Such characters cannot be placed within a text literal, but the CHAR(n) function makes them available in character expressions. Some compilers however corrupt the simplicity of text "literals" by allowing escape sequences to do so, usually in the style popularised by C, thus \n, and consequently, \\ should a single \ be desired.
<lang Fortran>
TEXT = 'That''s right!' !Only apostrophes as delimiters. Doubling required.
TEXT = "That's right!" !Chose quotes, so that apostrophes may be used freely.
TEXT = "He said ""That's right!""" !Give in, and use quotes for a "quoted string" source style.
TEXT = 'He said "That''s right!"' !Though one may dabble in inconsistency.
</lang>
Producing a source file containing a block of text that will be presented as output in the same layout (new lines and all) is only possible if the added layout stuff (generating the required new line starts) in the source is disregarded...
 
=={{header|friendly interactive shell}}==
1,220

edits