-

Functions HI-TECH C SCANF

提供: HI-TECH C for CP/M Fan WIKI(JP)
2018年1月5日 (金) 07:04時点におけるKumokosi (トーク | 投稿記録)による版 (ページの作成:「<strong>SCANF,VSCANF</strong> ==概要== #include <stdio.h> int scanf(char * fmt, ...) int vscanf(char *, va_list ap); ==詳細== <strong>Scanf</strong>()はstdi...」)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

SCANF,VSCANF

概要

#include  <stdio.h>

int scanf(char * fmt, ...)
int vscanf(char *, va_list ap);


詳細

Scanf()はstdinストリームからの整形された入力を"de-editing")します??一般的にストリームや文字列に対して使用可能な類似した関数があります。vscanf()は類似した関数ですが、連続した引数ではなく、引数リストへのポインタを取ります。このポインタはva_start()で初期化されるべきです。入力変換はfmt文字列で処理されます。一般的に整形された文字列は入力内の文字にマッチしなければいけません。しかし、整形文字列内のスペース文字は、入力内のゼロもしくはスペース、タブやnewlineの"ホワイトスペース文字"にマッチします。


 A conversion specification takes the

form of the character  %, optionally followed by an assignment suppression character ('*'), optionally followed by a numerical maximum field width, followed by a conversion specification character. Each conversion specification, unless it incorporates the assignment suppression character, will assign a value to the variable pointed at by the next argument. Thus if there are two conversion specifications in the fmt string, there should be two additional pointer arguments. The conversion characters are as follows:

o x d
Skip white space, then convert a number in base 8, 16 or 10 radix respectively. If a field width was supplied, take at most that many characters from the input. A leading minus sign will be recognized.
f
Skip white space, then convert a floating number in either conventional or scientific notation. The field width applies as above.
s
Skip white space, then copy a maximal length sequence of non-white-space characters. The pointer argument must be a pointer to char. The field width will limit the number of characters copied. The resultant string will be null-terminated.
c
Copy the next character from the input. The pointer argument is assumed to be a pointer to char. If a field width is specified, then copy that many characters. This differs from the s format in that white space does not terminate the character sequence.

The conversion characters o, x, u, d and f may be preceded by an l to indicate that the corresponding pointer argument is a pointer to long or double as appropriate. A preceding h will indicate that the pointer argument is a pointer to short rather than int.

Scanf()は変換が成功した数を返します。 変換がされる前にend-of-fileがあった場合はEOFが返されます。例は以下の通りです。

scanf("%d %s", &a, &s)
    with input "12s"
will assign 12 to a, and "s" to s.
scanf("%4cd %lf", &c, &f)
    with input " abcd -3.5"
will assign " abc" to c, and -3.5 to f.


参照

fscanf, sscanf, printf, va_arg

編集者からの注

は元のテキストでは抜けていたので、このWIKIでは修正しました。