• Aucun résultat trouvé

CHARACTER INPUT/OUTPUT

Dans le document 't!ser's vIA (Page 37-40)

35. int getcharO

May 1981

current stack pointer value. By default,

"dangerously close" is defined as 1000 bytes. To alter this default, see the next function. If you plan to use al/oeO and freeO in a program, but would also like some memory immune from alloca-tion to be available for scratch space, use sbrk() to request the desired memory instead of al/oeO.

Sbrk() calls may be made at any time (independent of any al/oeO and free() calls that may have been made.)

This should be used before any calls to sbrk or a/-lac, so that the storage allocation functions reject any allocation calls which would leave less than n bytes between the end of the allocated area and the current value of the stack pointer (remember that the stack grows down from high memory.) If rsvstk() is never used, then storage allocation is automatically prevented from approaching closer than 1000 bytes to the stack (just as if an rsvstk(1000) call had been made.)

. Returns next character' from standard input stream (CP 1M console input.)

Re·boots CP 1M on control-C.

Carriage return echos CR·LF to the console output and returns the newline ('\n') character.

A value of ·1 is returned for control·Z; note that the return value from getchar must be treated as an integer (as opposed to a character) if -1 is to be recognized. If you declare gl?tchar to be a character or assign its return value to a character variable, then the value 255 should be checked for instead (to detect the EOF character, control-Z.)

-38·

(

(

(

\.

80S C User's Guide

36. char ungetch(c)

37. int kbhitO

38. putchar(c)

39. putch(c)

The Standard Library

Causes the character c to be returned by the next call to getchar. Only one character may be

"ungotten" between consecutive getchar calls;

normally, zero is returned. If there was already a character pushed back since the last getchar() call, then the value of that character is returne~.

Returns true (non-zero) if input is present at the standard input (keyboard character hit); else reo turns false (zero.) In no case is the input actually sampled; to do so requires a subsequent getchar() call.

Note that kbhit will also return true if the ungetch function was used to push back a character to the console since the last getchar() call.

Writes the character. c to the standard output (CP 1M console output.)

The newline ('\n') character is transformed into a CR-LF combination.

If a control-C is detected on console input during a putchar call, program execution will halt and CPIM will be re-booted. If any other character is typed during a putchar call, then that character will be completely ignored.

If you don't want the console input interrogated . during console output, use the putch function,

described next:

Like putchar, except that the console input is NOT interrogated for control-C (or anything else) during output; any characters detected at the console in-put will be thrown away.

·39·

80S C User's Guide

40. puts(str) char ·str;

41. char • gets(str) char ·str;

42. printf(format,arg 1 ,arg2, ... ) char *format;

May 1981

Writes out the null-terminated string sIr to the standard output. No automatic newline is

append-ed.

Collects a line of input from the standard input into the buffer sIr.

Returns a pointer "to the beginning of sIr (the value gets was called with.)

The BOOS call to buffer up a line of input is used;

hence, the length of the provided buffer must be at least 3 bytes longer than the longest string you ever expect entered. Caution dictates making the buffer large, since an overflow here would most probably destroy neighboring data.

Formatted print function. Output goes to the stan-dard output. Conversion characters supported in the standard version:

d decimal integer format u unsigned integer format c single character

s string (null-terminated) o octal format

- x hex format

Each conversion is of the form:

% [-] [[0] w] [.n] <conv. char.>

where w specifies the width of the field, and n (if present) specifics the maximum number of charac-ters to be printed out of a string conversion. De-fault value for w is 1.

The field will be right-justified unless the dash is specifed following the percent sign, forcing left-justification. If the value for w is preceded by a zero, then zeros are used as padding on the left of the field instead of spaces. This feature has been iml)lemented for v1.43 of the package, and is very useful for printing hexadecimal values; the

-40-(

(

(

80S C User's Guide

43. int scanf{format,arg1,arg2, ... ) char ·format;

The Standard Library

feature had been neglected in previous versions.

An enhanced version of printf, incorporating the

e

and f format conversions for floating point values used in Bob Mathias's floating point package, is available for compilation in the file FLOAT.C.

Formatted input. This is analogous to printf, but operates in the opposite direction.

The %u conversion is not recognized; use %d for both signed and unsigned numerical input.

The field width specification is not supported, but the assignment suppression character (.) works OK.

The arguments to scant must be pointers!!!!!.

Note that input strings (denoted by a %s conver-sion specification in the format string) are terminat-ed only when the character following the %s in the format string is scanned.

Returns the number of items successfully assigned.

For a more detailed description of scant ~nd

printf, see Kernighan '& Ritchie, pages 145-150.

Dans le document 't!ser's vIA (Page 37-40)

Documents relatifs