scanf(3C)
_________________________________________________________________
scanf function
Perform a formatted read from standard input.
_________________________________________________________________
Calling Sequence
#include <stdio.h>
scanf(format, arg [,arg ...]);
where each arg is normally a pointer to a C variable or
array name (if you used @, arg would be an integer
expression).
format is a string containing three kinds of
objects:
* Whitespace characters such as space, New Line,
tab, and carriage return, which cause scanf to read
input up to the next non-whitespace characters.
* Ordinary printable ASCII characters.
* Conversion specifications that start with %.
Description
The scanf function takes the format string and matches the input
to it. It attempts to replace the format specifications with the
successive arguments, and reads the results into the arguments.
Note that ordinary characters must match exactly:
After the % character come three optional fields, which you
choose from the six options below. Note that, although these
fields are optional, you must enter them in the following
relative order.
A comma (,) specifying that commas are to be treated like spaces.
scanned, but
An asterisk (*) specifying that the number or string is to be
normal assignment omitted.
place of the
A digit string specifying the maximum field width. If @ occurs in
digit string, scanf will take the field width from the next
DG/UX 4.00 Page 1
Licensed material--property of copyright holder(s)
scanf(3C)
argument. (This is a Data General feature.)
item is a long
A capital or small l (L or l) indicating that the corresponding
int (or double, if using e, f, or g conversions) instead of
the normal int or float.
short int
A capital or small h (H or h) indicating that the next item is a
instead of a normal int.
the character
A conversion character, specifying how to convert the input. If
is not a valid conversion character, the input is expected
to match that character exactly. (This allows you to match
the percent % character itself.) In support of other
versions of C, an uppercase conversion character is
equivalent to an l or L prefix. Characters marked with an
asterisk (*) automatically skip blanks, tabs, and New Lines.
The following are conversion characters:
d Read a decimal integer.
i Read an integer and perform a signed, based conversion. If
the input begins with 0x or 0X, the number is converted as a
hexadecimal number; if the input begins with a leading 0,
the number is converted as an octal number. Otherwise, the
number is converted as a decimal number.
o Read an octal integer (with or without a leading 0).
x Read a hexadecimal integer (with or without leading 0x).
u Read an unsigned int.
c Read a character or characters, depending on the specified
field width.
s Read a string terminated by whitespace.
e Read a float (same as f and g, below).
f Read a float (same as e and g).
g Read a float (same as e and f, above).
n Assume that the next argument is an integer pointer in which
DG/UX 4.00 Page 2
Licensed material--property of copyright holder(s)
scanf(3C)
the number of characters read so far will be stored. Note
that the number stored does not count characters that scanf
pushes back with ungetc. This does not read anything.
r Do not read from input: argument is a nested format. (This
is a Data General feature.)
[...]
Read a string, matching only the characters in brackets.
Brackets denote a string not delimited by spaces. You can
include a "-" sign within the brackets to mean any letters
from the letter preceding the "-" to the letter succeeding
the "-"; for example, [a-z] reads a string consisting of
lowercase alphabetic characters.
[^..]
Read a string, matching all characters except those in
brackets and end of file; the first character must be a
caret (^). Brackets denote a string not delimited by
spaces. Some C environments may not support this feature.
See the "Input/Output Functions and Macros" section of
Chapter 1 for buffering information on I/O functions and
macros.
Returns
The return value from scanf is the number of items successfully
matched and assigned, or -1 if an error occurred before the first
match.
Related Functions
See also the fscanf, sscanf, and printf functions.
Examples
1. /* Program test for the scanf() function */
#include <stdio.h>
#define MAX 136
int i = 1, c;
char string[MAX];
main(argc, argv)
int argc;
char *argv[];
DG/UX 4.00 Page 3
Licensed material--property of copyright holder(s)
scanf(3C)
{
printf("Number of strings?\n");
scanf("%d\n", &c);
printf("Enter them:\n");
while (i <= c) {
scanf("%s", &string);
printf("%s%c", string, (i == c) ? '\n' : ' ');
i++;
}
}
If you call the program test, respond to the first query with 5,
and to the second with
There are five strings here.
you generate the output
There are five strings here.
The scanf function skips spaces and tabs, and reads in the five
character strings. The printf function places a space between
each pair of strings it writes out, and a New Line after the last
string.
2. /* Program test for the scanf() function */
#include <stdio.h>
#define MAX 136
int i = 0, c;
char dlmstart[32] = "%[^", dlm[24], dlmend[4] = "]",
*strcat(), string[MAX];
main(argc, argv)
int argc;
char *argv[];
{
printf("How many strings?\n");
scanf("%d", &c);
printf("Pick characters for delimiters (max 24):\n");
scanf("%s", &dlm);
strcat(dlmstart, dlm);
strcat(dlmstart, dlmend);
printf("Enter %d strings using %s as delimiters:\n",
c, dlm);
while (i < c) {
scanf("%r", dlmstart, &string);
printf("%s\n", string);
scanf("%*1s", &string);
DG/UX 4.00 Page 4
Licensed material--property of copyright holder(s)
scanf(3C)
i++;
}
}
If you call the program test, respond to the first query with 5,
to the second with
!?,.
and to the third with
Greetings, user! Some data, please? Thank you.
you generate the output
How many strings?
Pick characters for delimiters (max 24):
Enter 5 strings using !?., as delimiters:
Greetings
user
Some data
please
Thank you
The program stores the characters you enter in the variable dlm,
and uses the strcat function to create a %[^...] format, which
serves as a delimiter table. The program replaces the %r format
specification with the %[^...] format stored in the argument
dlmstart. It reads in each string, writes the string out, and
then reads past the current delimiter with the %*1s format.
3. /* Program test for the scanf() function */
#include <stdio.h>
int dec, oct, hex;
main() {
printf("Enter decimal, octal, and ");
printf("hexadecimal numbers:\n");
scanf("%d%o%x", &dec, &oct, &hex);
printf("\n\tDecml\tOctal\tHexdec\n\n");
printf("Decml\t%d\t%o\t%x\n", dec, dec, dec);
printf("Octal\t%d\t%o\t%x\n", oct, oct, oct);
printf("Hexdec\t%d\t%o\t%x\n", hex, hex, hex);
}
If you call the program test, and respond to the query with
DG/UX 4.00 Page 5
Licensed material--property of copyright holder(s)
scanf(3C)
1000 1000 1000
you generate the output
Decml Octal Hexdec
Decml 1000 1750 3e8
Octal 512 1000 200
Hexdec 4096 10000 1000
4. /* Program test for the scanf() function */
#include <stdio.h>
int i = 0, c;
double d;
main() {
printf("How many floating-point entries?\n");
scanf("%d", &c);
printf("Please enter them:\n");
while (i < c) {
scanf("%f", &d);
printf("%f\t= %g\n", d, d);
i++;
}
printf("Input Value\tShortest Representation\n");
}
If you call the program test and respond to the first query with
4 and to the second with
you generate the output
0.000005 = 5.e-06
0.005000 = 5.e-03
50000.000000 = 50000
5000000.000000 = 5.e+06
Input Value Shortest Representation
DG/UX 4.00 Page 6
Licensed material--property of copyright holder(s)