www.delorie.com/djgpp/doc/libc/libc_670.html   search  
libc.a reference

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

scanf

Syntax

 
#include <stdio.h>

int scanf(const char *format, ...);

Description

This function scans formatted text from stdin and stores it in the variables pointed to by the arguments. See section scanf.

The format string contains regular characters which much match the input exactly as well as a conversion specifiers, which begin with a percent symbol. Any whitespace in the format string matches zero or more of any whitespace characters in the input. Thus, a single space may match a newline and two tabs in the input. All conversions except `c' and `[' also skip leading whitespace automatically. Each conversion specifier contains the following fields:

Integer formats make use of strtol or strtoul to perform the actual conversions. Floating-point conversions use strtod and _strtold.

Return Value

The number of items successfully matched and assigned. If input ends, or if there is any input failure before the first item is converted and assigned, EOF is returned. Note that literal characters (including whitespace) in the format string which matched input characters count as "converted items", so input failure after such characters were read and matched will not cause EOF to be returned.

Portability

ANSI/ISO C C89; C99 (see note 1) (see note 2)
POSIX 1003.2-1992; 1003.1-2001

Notes:

  1. The hh, j, t and z conversion specifiers first appeared in the ANSI C99 standard.

  2. The conversion specifiers `F', `D', `I', `O', and U are DJGPP extensions; they are provided for compatibility with Borland C and other compilers. The conversion specifiers for the long long data type are GCC extensions. The meaning of `[a-c]' as a range of characters is a very popular extension to ANSI (which merely says a dash "may have a special meaning" in that context).

Example

 
int x, y;
char buf[100];
scanf("%d %d %s", &x, &y, buf);

/* read to end-of-line */
scanf("%d %[^\n]\n", &x, buf);
/* read letters only */
scanf("%[a-zA-Z]", buf);


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2004   by DJ Delorie     Updated Apr 2004