-

Difference between revisions of "Functions HI-TECH C SCANF"

From HI-TECH C for CP/M Fan WIKI(EN)
Jump to: navigation, search
(Created page with "<strong>SCANF</strong> ==SYNOPSIS== #include <stdio.h> int scanf(char * fmt, ...) int vscanf(char *, va_list ap); ==DESCRIPTION== <strong>Scanf</strong>() performs fo...")
 
Line 12: Line 12:
 
the  stdin  stream. Similar functions are available for
 
the  stdin  stream. Similar functions are available for
 
streams in general,  and  for  strings.  The  function
 
streams in general,  and  for  strings.  The  function
<strong>vscanf() is similar, but takes a pointer to an argument
+
<strong>vscanf</strong>() is similar, but takes a pointer to an argument
 
list rather than a series of additional arguments. This
 
list rather than a series of additional arguments. This
 
pointer  should  have been initialized with <strong>va_start</strong>().
 
pointer  should  have been initialized with <strong>va_start</strong>().

Revision as of 23:22, 31 July 2017

SCANF

SYNOPSIS

#include  <stdio.h>

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


DESCRIPTION

Scanf() performs formatted input ("de-editing") from the stdin stream. Similar functions are available for streams in general, and for strings. The function vscanf() is similar, but takes a pointer to an argument list rather than a series of additional arguments. This pointer should have been initialized with va_start(). The input conversions are performed according to the fmt string; in general a character in the format string must match a character in the input; however a space character in the format string will match zero or more "white space" characters in the input, i.e. spaces, tabs or newlines. 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() returns the number of successful conversions; EOF is returned if end-of-file was seen before any conversions were performed. Some examples are:

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.


SEE ALSO

fscanf, sscanf, printf, va_arg