📄 stdio.h
字号:
/* Copyright (c) 2002, Joerg Wunsch
Portions of documentation Copyright (c) 1990, 1991, 1993
The Regents of the University of California.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
$Id: stdio.h,v 1.11.2.3 2003/11/24 18:09:28 troth Exp $
*/
#ifndef _STDIO_H_
#define _STDIO_H_ 1
#ifndef __ASSEMBLER__
#include <inttypes.h>
#include <stdarg.h>
#define __need_NULL
#define __need_size_t
#include <stddef.h>
/** \defgroup avr_stdio Standard IO facilities
\code #include <stdio.h> \endcode
\warning
This implementation of the standard IO facilities is new to
avr-libc. It is not yet expected to remain stable, so some
aspects of the API might change in a future release.
This file declares the standard IO facilities that are implemented
in \c avr-libc. Due to the nature of the underlying hardware,
only a limited subset of standard IO is implemented. There is no
actual file implementation available, so only device IO can be
performed. Since there's no operating system, the application
needs to provide enough details about their devices in order to
make them usable by the standard IO facilities.
Due to space constraints, some functionality has not been
implemented at all (like some of the \c printf conversions that
have been left out). Nevertheless, potential users of this
implementation should be warned: the \c printf and \c scanf families of functions, although
usually associated with presumably simple things like the
famous "Hello, world!" program, are actually fairly complex
which causes their inclusion to eat up a fair amount of code space.
Also, they are not fast due to the nature of interpreting the
format string at run-time. Whenever possible, resorting to the
(sometimes non-standard) predetermined conversion facilities that are
offered by avr-libc will usually cost much less in terms of speed
and code size.
In order to allow programmers a code size vs. functionality tradeoff,
the function vfprintf() which is the heart of the printf family can be
selected in different flavours using linker options. See the
documentation of vfprintf() for a detailed description. The same
applies to vfscanf() and the \c scanf family of functions.
The standard streams \c stdin, \c stdout, and \c stderr are
provided, but contrary to the C standard, since avr-libc has no
knowledge about applicable devices, these streams are not already
pre-initialized at application startup. Also, since there is no
notion of "file" whatsoever to avr-libc, there is no function
\c fopen() that could be used to associate a stream to some device.
(See \ref stdio_note1 "note 1".) Instead, the function \c fdevopen()
is provided to associate a stream to a device, where the device
needs to provide a function to send a character, to receive a
character, or both. There is no differentiation between "text" and
"binary" streams inside avr-libc. Character \c \\n is sent
literally down to the device's \c put() function. If the device
requires a carriage return (\c \\r) character to be sent before
the linefeed, its \c put() routine must implement this (see
\ref stdio_note2 "note 2").
For convenience, the first call to \c fdevopen() that opens a
stream for reading will cause the resulting stream to be aliased
to \c stdin. Likewise, the first call to \c fdevopen() that opens
a stream for writing will cause the resulting stream to be aliased
to both, \c stdout, and \c stderr. Thus, if the open was done
with both, read and write intent, all three standard streams will
be identical. Note that these aliases are indistinguishable from
each other, thus calling \c fclose() on such a stream will also
effectively close all of its aliases (\ref stdio_note3 "note 3").
All the \c printf and \c scanf family functions come in two flavours: the
standard name, where the format string is expected to be in
SRAM, as well as a version with the suffix "_P" where the format
string is expected to reside in the flash ROM. The macro
\c PSTR (explained in \ref avr_pgmspace) becomes very handy
for declaring these format strings.
\anchor stdio_note1 \par Note 1:
It might have been possible to implement a device abstraction that
is compatible with \c fopen() but since this would have required
to parse a string, and to take all the information needed either
out of this string, or out of an additional table that would need to be
provided by the application, this approach was not taken.
\anchor stdio_note2 \par Note 2:
This basically follows the Unix approach: if a device such as a
terminal needs special handling, it is in the domain of the
terminal device driver to provide this functionality. Thus, a
simple function suitable as \c put() for \c fdevopen() that talks
to a UART interface might look like this:
\code
int
uart_putchar(char c)
{
if (c == '\n')
uart_putchar('\r');
loop_until_bit_is_set(UCSRA, UDRE);
UDR = c;
return 0;
}
\endcode
\anchor stdio_note3 \par Note 3:
This implementation has been chosen because the cost of maintaining
an alias is considerably smaller than the cost of maintaining full
copies of each stream. Yet, providing an implementation that offers
the complete set of standard streams was deemed to be useful. Not
only that writing \c printf() instead of <tt>fprintf(mystream, ...)</tt>
saves typing work, but since avr-gcc needs to resort to pass all
arguments of variadic functions on the stack (as opposed to passing
them in registers for functions that take a fixed number of
parameters), the ability to pass one parameter less by implying
\c stdin will also save some execution time.
*/
struct __file;
/*@{*/
/**
\c FILE is the opaque structure that is passed around between the
various standard IO functions.
*/
#define FILE struct __file
/**
Stream that will be used as an input stream by the simplified
functions that don't take a \c stream argument.
The first stream opened with read intent using \c fdevopen()
will be assigned to \c stdin.
*/
#define stdin (__iob[0])
/**
Stream that will be used as an output stream by the simplified
functions that don't take a \c stream argument.
The first stream opened with write intent using \c fdevopen()
will be assigned to both, \c stdin, and \c stderr.
*/
#define stdout (__iob[1])
/**
Stream destined for error output. Unless specifically assigned,
identical to \c stdout.
If \c stderr should point to another stream, the result of
another \c fdevopen() must be explicitly assigned to it without
closing the previous \c stderr (since this would also close
\c stdout).
*/
#define stderr (__iob[2])
/**
\c EOF declares the value that is returned by various standard IO
functions in case of an error. Since the AVR platform (currently)
doesn't contain an abstraction for actual files, its origin as
"end of file" is somewhat meaningless here.
*/
#define EOF (-1)
#ifdef __cplusplus
extern "C" {
#endif
extern struct __file *__iob[];
extern FILE *fdevopen(int (*__put)(char), int (*__get)(void), int __opts);
/**
This function closes \c stream, and disallows and further
IO to and from it.
It currently always returns 0 (for success).
*/
extern int fclose(FILE *__stream);
/**
\c vfprintf is the central facility of the \c printf family of
functions. It outputs values to \c stream under control of a
format string passed in \c fmt. The actual values to print are
passed as a variable argument list \c ap.
\c vfprintf returns the number of characters written to \c stream,
or \c EOF in case of an error. Currently, this will only happen
if \c stream has not been opened with write intent.
The format string is composed of zero or more directives: ordinary
characters (not \c %), which are copied unchanged to the output
stream; and conversion specifications, each of which results in
fetching zero or more subsequent arguments. Each conversion
specification is introduced by the \c % character. The arguments must
properly correspond (after type promotion) with the conversion
specifier. After the \c %, the following appear in sequence:
- Zero or more of the following flags:
<ul>
<li> \c # The value should be converted to an "alternate form". For
c, d, i, s, and u conversions, this option has no effect.
For o conversions, the precision of the number is
increased to force the first character of the output
string to a zero (except if a zero value is printed with
an explicit precision of zero). For x and X conversions,
a non-zero result has the string `0x' (or `0X' for X
conversions) prepended to it.</li>
<li> \c 0 (zero) Zero padding. For all conversions, the converted
value is padded on the left with zeros rather than blanks.
If a precision is given with a numeric conversion (d, i,
o, u, i, x, and X), the 0 flag is ignored.</li>
<li> \c - A negative field width flag; the converted value is to be
left adjusted on the field boundary. The converted value
is padded on the right with blanks, rather than on the
left with blanks or zeros. A - overrides a 0 if both are
given.</li>
<li> ' ' (space) A blank should be left before a positive number
produced by a signed conversion (d, or i).</li>
<li> \c + A sign must always be placed before a number produced by a
signed conversion. A + overrides a space if both are
used.</li>
</ul>
- An optional decimal digit string specifying a minimum field width.
If the converted value has fewer characters than the field width, it
will be padded with spaces on the left (or right, if the left-adjust
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -