Timeline for Which language had the first scanf?
Current License: CC BY-SA 4.0
14 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Aug 18, 2021 at 23:23 | vote | accept | DrSheldon | ||
S Aug 18, 2021 at 20:46 | history | suggested | chicks | CC BY-SA 4.0 |
formatting heading/link and copy edit
|
Aug 18, 2021 at 19:19 | review | Suggested edits | |||
S Aug 18, 2021 at 20:46 | |||||
Aug 17, 2021 at 22:45 | comment | added | jamesqf | Re "...obvious advantage that identical format definitions can be used in multiple I/O statements...", you can do this in C scanf too. The format argument is a string, which doesn't have to be a literal string. It can be a string variable (that is, '\0' terminated array of char). You can even write the format to the string on the fly. | |
Aug 17, 2021 at 19:45 | comment | added | supercat | @another-dave: I should perhaps have added "without having to be given information about how many arguments there are". An implementation expect each call instruction to be followed by a zero-terminated list of argument addresses, but that would make it necessary to either include that extra zero word on all function calls, and have all functions' return logic scan for it (which would I guess be possible, but impractical), or else process calls to variadic functions differently from calls to other functions. | |
Aug 17, 2021 at 19:29 | comment | added | dave | @supercat: re: On a FORTRAN implementation which doesn't have a stack, it would be impossible for a function to accept a variable number of arguments - I don't see this at all. If your calling sequence is, say, a jump-to-subroutine followed by sequence of addresses of arguments (pass-by-ref), then I can imagine a number of ways to determine the number of actual-args, and adjust the return appropriately. | |
Aug 17, 2021 at 19:19 | comment | added | supercat | ...having to know or care how many words on the stack were in fact function arguments. | |
Aug 17, 2021 at 19:18 | comment | added | supercat |
@alephzero: On a FORTRAN implementation which doesn't have a stack, it would be impossible for a function to accept a variable number of arguments; the only way to have a single function call take care of an arbitrary input/output task would be to use the approach you describe for modern implementations. FORTRAN has, so far as I can tell, always allowed a reference to an arbitrary-sized array to be passed as a single argument, but that's very different from how scanf would have worked in typical early C implementations which could read arguments off the stack without the compiler...
|
|
Aug 17, 2021 at 17:38 | comment | added | alephzero | ... AFAIK modern Fortran run-time libraries use a function with just two arguments, i.e. two data structures representing that format string and the variables to be input or output. (I don't know the history of how this evolved over time, but note that modern Fortran includes arbitrary user-defined code activated by the FORMAT specification, e.g. to output user-defined data types. | |
Aug 17, 2021 at 17:32 | comment | added | alephzero |
@supercat I don't think the a compiler could have converted them into calls with a predefined fixed sequence of arguments, because (1) as another-dave pointed out, re-using the format items follows it own logic (depending on the format string itself) and (2) the number of input items can be read as part of the input data - e.g 10 FORMAT(3I5) and then READ(*,10) M, (N(J),J=1,M) is valid Fortran. The first input data line contains M and the first two elements of array N, subsequent lines each contain the next three elements of N, ignoring any excess input items on the last line.
|
|
Aug 17, 2021 at 16:11 | comment | added | supercat | @alephzero: Were the formatting operations in FORTRAN processed as calls to variadic functions, or would they have required compiler logic to translate what looked like variadic calls into sequences of calls to functions that would each expect a fixed set of arguments? | |
Aug 17, 2021 at 14:33 | comment | added | alephzero |
@another-dave Well, scanf just replaces the "built-in baroque logic" with user-written baroque logic to loop around the scanf statement. IMO the real genius of the Fortran design was identifying a subset of the general formatting problem which was "good enough" to do something useable almost all of the time.
|
|
Aug 17, 2021 at 14:10 | comment | added | dave | Maybe FORMAT should be disqualified on the requirement that there be an argument that "specifies the number ... of the items to read." :-) If I recall correctly, it's the de facto number of variable/array elements that appear in the READ that determine how many items are read. "Not using all the FORMAT" is ok, and coming to the end of the FORMAT is governed by baroque rules of where to resume scanning. I suspect Algol 68 has similar features. | |
Aug 17, 2021 at 10:45 | history | answered | alephzero | CC BY-SA 4.0 |