AVR-LibC  2.3.0
Standard C library for AVR-GCC
 

AVR-LibC Manual

AVR-LibC Sources

Main Page

User Manual

Lib­rary Refe­rence

FAQ

Exam­ple Pro­jects

Index

Loading...
Searching...
No Matches
stdio.h
Go to the documentation of this file.
1/* Copyright (c) 2002, 2005, 2007 Joerg Wunsch
2 All rights reserved.
3
4 Portions of documentation Copyright (c) 1990, 1991, 1993
5 The Regents of the University of California.
6
7 All rights reserved.
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
11
12 * Redistributions of source code must retain the above copyright
13 notice, this list of conditions and the following disclaimer.
14
15 * Redistributions in binary form must reproduce the above copyright
16 notice, this list of conditions and the following disclaimer in
17 the documentation and/or other materials provided with the
18 distribution.
19
20 * Neither the name of the copyright holders nor the names of
21 contributors may be used to endorse or promote products derived
22 from this software without specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 POSSIBILITY OF SUCH DAMAGE. */
35
36#ifndef _STDIO_H_
37#define _STDIO_H_ 1
38
39#ifndef __ASSEMBLER__
40
41#include <inttypes.h>
42#include <stdarg.h>
43
44#ifndef __DOXYGEN__
45#define __need_NULL
46#define __need_size_t
47#include <stddef.h>
48#endif /* !__DOXYGEN__ */
49
50/** \file */
51/** \defgroup avr_stdio <stdio.h>: Standard IO facilities
52 \code #include <stdio.h> \endcode
53
54 <h3>Introduction to the Standard IO facilities</h3>
55
56 This file declares the standard IO facilities that are implemented
57 in AVR-LibC. Due to the nature of the underlying hardware,
58 only a limited subset of standard IO is implemented. There is no
59 actual file implementation available, so only device IO can be
60 performed. Since there's no operating system, the application
61 needs to provide enough details about their devices in order to
62 make them usable by the standard IO facilities.
63
64 Due to space constraints, some functionality has not been
65 implemented at all (like some of the \c printf conversions that
66 have been left out). Nevertheless, potential users of this
67 implementation should be warned: the \c printf and \c scanf families of functions, although
68 usually associated with presumably simple things like the
69 famous "Hello, world!" program, are actually fairly complex
70 which causes their inclusion to eat up a fair amount of code space.
71 Also, they are not fast due to the nature of interpreting the
72 format string at run-time. Whenever possible, resorting to the
73 (sometimes non-standard) predetermined conversion facilities that are
74 offered by AVR-LibC will usually cost much less in terms of speed
75 and code size.
76
77 <h3>Tunable options for code size vs. feature set</h3>
78
79 In order to allow programmers a code size vs. functionality tradeoff,
80 the function vfprintf() which is the heart of the printf family can be
81 selected in different flavours using linker options. See the
82 documentation of vfprintf() for a detailed description. The same
83 applies to vfscanf() and the \c scanf family of functions.
84
85 <h3>Outline of the chosen API</h3>
86
87 The standard streams \c stdin, \c stdout, and \c stderr are
88 provided, but contrary to the C standard, since AVR-LibC has no
89 knowledge about applicable devices, these streams are not already
90 pre-initialized at application startup. Also, since there is no
91 notion of "file" whatsoever to AVR-LibC, there is no function
92 \c fopen() that could be used to associate a stream to some device.
93 (See \ref stdio_note1 "note 1".) Instead, the function \c fdevopen()
94 is provided to associate a stream to a device, where the device
95 needs to provide a function to send a character, to receive a
96 character, or both. There is no differentiation between "text" and
97 "binary" streams inside AVR-LibC. Character \c \\n is sent
98 literally down to the device's \c put() function. If the device
99 requires a carriage return (\c \\r) character to be sent before
100 the linefeed, its \c put() routine must implement this (see
101 \ref stdio_note2 "note 2").
102
103 As an alternative method to fdevopen(), the macro
104 fdev_setup_stream() might be used to setup a user-supplied FILE
105 structure.
106
107 It should be noted that the automatic conversion of a newline
108 character into a carriage return - newline sequence breaks binary
109 transfers. If binary transfers are desired, no automatic
110 conversion should be performed, but instead any string that aims
111 to issue a CR-LF sequence must use <tt>"\r\n"</tt> explicitly.
112
113 For convenience, the first call to \c fdevopen() that opens a
114 stream for reading will cause the resulting stream to be aliased
115 to \c stdin. Likewise, the first call to \c fdevopen() that opens
116 a stream for writing will cause the resulting stream to be aliased
117 to both, \c stdout, and \c stderr. Thus, if the open was done
118 with both, read and write intent, all three standard streams will
119 be identical. Note that these aliases are indistinguishable from
120 each other, thus calling \c fclose() on such a stream will also
121 effectively close all of its aliases (\ref stdio_note3 "note 3").
122
123 It is possible to tie additional user data to a stream, using
124 fdev_set_udata(). The backend put and get functions can then
125 extract this user data using fdev_get_udata(), and act
126 appropriately. For example, a single put function could be used
127 to talk to two different UARTs that way, or the put and get
128 functions could keep internal state between calls there.
129
130 <h3>Format strings in flash ROM</h3>
131
132 All the \c printf and \c scanf family functions come in three flavours:
133 the standard name, where the format string is expected to be in
134 SRAM, as well as two versions with the suffix "_P" and "_F" where the
135 format string is expected to reside in the flash ROM. The macros
136 #PSTR (explained in \ref avr_pgmspace) and #FSTR (explained in
137 \ref avr_flash "<avr/flash.h>") become very handy for declaring
138 these format strings.
139
140 \anchor stdio_without_malloc
141 <h3>Running stdio without malloc()</h3>
142
143 By default, fdevopen() requires \ref a_malloc "malloc()". As this is often
144 not desired in the limited environment of a microcontroller, an
145 alternative option is provided to run completely without malloc().
146
147 The macro fdev_setup_stream() is provided to prepare a
148 user-supplied FILE buffer for operation with stdio.
149
150 <h4>Example</h4>
151
152 \code
153 #include <stdio.h>
154
155 static FILE mystdout = FDEV_SETUP_STREAM (uart_putchar, NULL,
156 _FDEV_SETUP_WRITE);
157 static int
158 uart_putchar (char c, FILE *stream)
159 {
160 if (c == '\n')
161 uart_putchar ('\r', stream);
162 loop_until_bit_is_set (UCSRA, UDRE);
163 UDR = c;
164 return 0;
165 }
166
167 int main (void)
168 {
169 init_uart();
170 stdout = &mystdout;
171 printf ("Hello, world!\n");
172 return 0;
173 }
174 \endcode
175
176 This example uses the initializer form FDEV_SETUP_STREAM() rather
177 than the function-like fdev_setup_stream(), so all data
178 initialization happens during C start-up.
179
180 If streams initialized that way are no longer needed, they can be
181 destroyed by first calling the macro fdev_close(), and then
182 destroying the object itself. No call to fclose() should be
183 issued for these streams. While calling fclose() itself is
184 harmless, it will cause an undefined reference to \ref a_free "free()"
185 and thus cause the linker to pull in the \ref a_malloc "malloc"
186 module into the application.
187
188<dl>
189<dt>\anchor stdio_note1 Note 1:</dt>
190<dd>
191 It might have been possible to implement a device abstraction that
192 is compatible with \c fopen() but since this would have required
193 to parse a string, and to take all the information needed either
194 out of this string, or out of an additional table that would need to be
195 provided by the application, this approach was not taken.
196</dd>
197<dt>\anchor stdio_note2 Note 2:</dt>
198<dd>
199 This basically follows the Unix approach: if a device such as a
200 terminal needs special handling, it is in the domain of the
201 terminal device driver to provide this functionality. Thus, a
202 simple function suitable as \c put() for \c fdevopen() that talks
203 to a UART interface might look like this:
204
205 \code
206 int uart_putchar (char c, FILE *stream)
207 {
208 if (c == '\n')
209 uart_putchar ('\r', stream);
210 loop_until_bit_is_set (UCSRA, UDRE);
211 UDR = c;
212 return 0;
213 }
214 \endcode
215</dd>
216<dt>\anchor stdio_note3 Note 3:</dt>
217<dd>
218 This implementation has been chosen because the cost of maintaining
219 an alias is considerably smaller than the cost of maintaining full
220 copies of each stream. Yet, providing an implementation that offers
221 the complete set of standard streams was deemed to be useful. Not
222 only that writing \c printf() instead of <tt>fprintf(mystream, ...)</tt>
223 saves typing work, but since avr-gcc needs to resort to pass all
224 arguments of variadic functions on the stack (as opposed to passing
225 them in registers for functions that take a fixed number of
226 parameters), the ability to pass one parameter less by implying
227 \c stdin or stdout will also save some execution time.
228</dd>
229</dl>
230*/
231
232#if !defined(__DOXYGEN__)
233
234/*
235 * This is an internal structure of the library that is subject to be
236 * changed without warnings at any time. Please do *never* reference
237 * elements of it beyond by using the official interfaces provided.
238 */
239struct __file {
240 char *buf; /* buffer pointer */
241 unsigned char unget; /* ungetc() buffer */
242 uint8_t flags; /* flags, see below */
243#define __SRD 0x0001 /* OK to read */
244#define __SWR 0x0002 /* OK to write */
245#define __SSTR 0x0004 /* this is an sprintf/snprintf string */
246#define __SPGM 0x0008 /* fmt string is in progmem */
247#define __SERR 0x0010 /* found error */
248#define __SEOF 0x0020 /* found EOF */
249#define __SUNGET 0x040 /* ungetc() happened */
250#define __SMALLOC 0x80 /* handle is malloc()ed */
251#if 0
252/* possible future extensions, will require uint16_t flags */
253#define __SRW 0x0100 /* open for reading & writing */
254#define __SLBF 0x0200 /* line buffered */
255#define __SNBF 0x0400 /* unbuffered */
256#define __SMBF 0x0800 /* buf is from malloc */
257#endif
258 int size; /* size of buffer */
259 int len; /* characters read or written so far */
260 int (*put)(char, struct __file *); /* function to write one char to device */
261 int (*get)(struct __file *); /* function to read one char from device */
262 void *udata; /* User defined and accessible data. */
263};
264
265#endif /* not __DOXYGEN__ */
266
267/**@{*/
268/**
269 \c FILE is the opaque structure that is passed around between the
270 various standard IO functions.
271*/
272typedef struct __file FILE;
273
274/**
275 Stream that will be used as an input stream by the simplified
276 functions that don't take a \c stream argument.
277
278 The first stream opened with read intent using \c fdevopen()
279 will be assigned to \c stdin.
280*/
281#define stdin (__iob[0])
282
283/**
284 Stream that will be used as an output stream by the simplified
285 functions that don't take a \c stream argument.
286
287 The first stream opened with write intent using \c fdevopen()
288 will be assigned to both, \c stdin, and \c stderr.
289*/
290#define stdout (__iob[1])
291
292/**
293 Stream destined for error output. Unless specifically assigned,
294 identical to \c stdout.
295
296 If \c stderr should point to another stream, the result of
297 another \c fdevopen() must be explicitly assigned to it without
298 closing the previous \c stderr (since this would also close
299 \c stdout).
300*/
301#define stderr (__iob[2])
302
303/**
304 \c EOF declares the value that is returned by various standard IO
305 functions in case of an error. Since the AVR platform (currently)
306 doesn't contain an abstraction for actual files, its origin as
307 "end of file" is somewhat meaningless here.
308*/
309#define EOF (-1)
310
311/** This macro inserts a pointer to user defined data into a FILE
312 stream object.
313
314 The user data can be useful for tracking state in the put and get
315 functions supplied to the fdevopen() function. */
316#define fdev_set_udata(stream, u) do { (stream)->udata = u; } while(0)
317
318/** This macro retrieves a pointer to user defined data from a FILE
319 stream object. */
320#define fdev_get_udata(stream) ((stream)->udata)
321
322#if defined(__DOXYGEN__)
323/**
324 \brief Setup a user-supplied buffer as an stdio stream
325
326 This macro takes a user-supplied buffer \c stream, and sets it up
327 as a stream that is valid for stdio operations, similar to one that
328 has been obtained dynamically from fdevopen(). The buffer to setup
329 must be of type #FILE.
330
331 The arguments \c put and \c get are identical to those that need to
332 be passed to fdevopen().
333
334 The \c rwflag argument can take one of the values #_FDEV_SETUP_READ,
335 #_FDEV_SETUP_WRITE, or #_FDEV_SETUP_RW, for read, write, or read/write
336 intent, respectively.
337
338 \note No assignments to the standard streams will be performed by
339 fdev_setup_stream(). If standard streams are to be used, these
340 need to be assigned by the user. See also under
341 \ref stdio_without_malloc "Running stdio without malloc()".
342 */
343#define fdev_setup_stream(stream, put, get, rwflag)
344#else /* !DOXYGEN */
345#define fdev_setup_stream(stream, p, g, f) \
346 do { \
347 (stream)->put = p; \
348 (stream)->get = g; \
349 (stream)->flags = f; \
350 (stream)->udata = 0; \
351 } while(0)
352#endif /* DOXYGEN */
353
354#define _FDEV_SETUP_READ __SRD /**< fdev_setup_stream() with read intent */
355#define _FDEV_SETUP_WRITE __SWR /**< fdev_setup_stream() with write intent */
356#define _FDEV_SETUP_RW (__SRD|__SWR) /**< fdev_setup_stream() with read/write intent */
357
358/**
359 Return code for an error condition during device read.
360
361 To be used in the get function of fdevopen(). */
362#define _FDEV_ERR (-1)
363
364/**
365 Return code for an end-of-file condition during device read.
366
367 To be used in the get function of fdevopen(). */
368#define _FDEV_EOF (-2)
369
370#if defined(__DOXYGEN__)
371/**
372 \brief Initializer for a user-supplied stdio stream
373
374 This macro acts similar to fdev_setup_stream(), but it is to be
375 used as the initializer of a variable of type FILE.
376
377 The remaining arguments are to be used as explained in
378 fdev_setup_stream().
379 */
380#define FDEV_SETUP_STREAM(put, get, rwflag)
381#else /* !DOXYGEN */
382/* In order to work with C++, we have to mention the fields in the order
383 as they appear in struct __file. Also, designated initializers are
384 only supported since C++20. */
385#define FDEV_SETUP_STREAM(PU, GE, FL) \
386 { \
387 (char*) 0 /* buf */, \
388 0u /* unget */, \
389 FL /* flags */, \
390 0 /* size */, \
391 0 /* len */, \
392 PU /* put */, \
393 GE /* get */, \
394 (void*) 0 /* udata */ \
395 }
396#endif /* DOXYGEN */
397
398#ifdef __cplusplus
399extern "C" {
400#endif
401
402#if !defined(__DOXYGEN__)
403/*
404 * Doxygen documentation can be found in fdevopen.c.
405 */
406
407extern struct __file *__iob[];
408
409#if defined(__STDIO_FDEVOPEN_COMPAT_12)
410/*
411 * Declare prototype for the discontinued version of fdevopen() that
412 * has been in use up to AVR-LibC 1.2.x. The new implementation has
413 * some backwards compatibility with the old version.
414 */
415extern FILE *fdevopen(int (*__put)(char), int (*__get)(void),
416 int __opts __attribute__((__unused__)));
417#else /* !defined(__STDIO_FDEVOPEN_COMPAT_12) */
418/* New prototype for AVR-LibC 1.4 and above. */
419extern FILE *fdevopen(int (*__put)(char, FILE*), int (*__get)(FILE*));
420#endif /* defined(__STDIO_FDEVOPEN_COMPAT_12) */
421
422#endif /* not __DOXYGEN__ */
423
424/**
425 This function closes \c stream, and disallows and further
426 IO to and from it.
427
428 When using fdevopen() to setup the stream, a call to fclose() is
429 needed in order to free the internal resources allocated.
430
431 If the stream has been set up using fdev_setup_stream() or
432 FDEV_SETUP_STREAM(), use fdev_close() instead.
433
434 It currently always returns 0 (for success).
435*/
436extern int fclose(FILE *__stream);
437
438/**
439 This macro frees up any library resources that might be associated
440 with \c stream. It should be called if \c stream is no longer
441 needed, right before the application is going to destroy the
442 \c stream object itself.
443
444 (Currently, this macro evaluates to nothing, but this might change
445 in future versions of the library.)
446*/
447#define fdev_close() ((void)0)
448
449
450/**
451 \c vfprintf is the central facility of the \c printf family of
452 functions. It outputs values to \c stream under control of a
453 format string passed in \c fmt. The actual values to print are
454 passed as a variable argument list \c ap.
455
456 \c vfprintf returns the number of characters written to \c stream,
457 or \c EOF in case of an error. Currently, this will only happen
458 if \c stream has not been opened with write intent.
459
460 The format string is composed of zero or more directives: ordinary
461 characters (not \c %), which are copied unchanged to the output
462 stream; and conversion specifications, each of which results in
463 fetching zero or more subsequent arguments. Each conversion
464 specification is introduced by the \c % character. The arguments must
465 properly correspond (after type promotion) with the conversion
466 specifier. After the \c %, the following appear in sequence:
467
468 - Zero or more of the following flags:
469 <ul>
470 <li> \c # The value should be converted to an "alternate form". For
471 c, d, i, s, and u conversions, this option has no effect.
472 For o conversions, the precision of the number is
473 increased to force the first character of the output
474 string to a zero (except if a zero value is printed with
475 an explicit precision of zero). For x and X conversions,
476 a non-zero result has the string `0x' (or `0X' for X
477 conversions) prepended to it.</li>
478 <li> \c 0 (zero) Zero padding. For all conversions, the converted
479 value is padded on the left with zeros rather than blanks.
480 If a precision is given with a numeric conversion (d, i,
481 o, u, i, x, and X), the 0 flag is ignored.</li>
482 <li> \c - A negative field width flag; the converted value is to be
483 left adjusted on the field boundary. The converted value
484 is padded on the right with blanks, rather than on the
485 left with blanks or zeros. A - overrides a 0 if both are
486 given.</li>
487 <li> ' ' (space) A blank should be left before a positive number
488 produced by a signed conversion (d, or i).</li>
489 <li> \c + A sign must always be placed before a number produced by a
490 signed conversion. A + overrides a space if both are
491 used.</li>
492 </ul>
493
494 - An optional decimal digit string specifying a minimum field width.
495 If the converted value has fewer characters than the field width, it
496 will be padded with spaces on the left (or right, if the left-adjustment
497 flag has been given) to fill out the field width.
498 - An optional precision, in the form of a period . followed by an
499 optional digit string. If the digit string is omitted, the
500 precision is taken as zero. This gives the minimum number of
501 digits to appear for d, i, o, u, x, and X conversions, or the
502 maximum number of characters to be printed from a string for \c s
503 conversions.
504 - An optional \c l or \c h length modifier, that specifies that the
505 argument for the d, i, o, u, x, or X conversion is a \c "long int"
506 rather than \c int. The \c h is ignored, as \c "short int" is
507 equivalent to \c int.
508 - A character that specifies the type of conversion to be applied.
509
510 The conversion specifiers and their meanings are:
511
512 - \c diouxX The int (or appropriate variant) argument is converted
513 to signed decimal (d and i), unsigned octal (o), unsigned
514 decimal (u), or unsigned hexadecimal (x and X) notation.
515 The letters "abcdef" are used for x conversions; the
516 letters "ABCDEF" are used for X conversions. The
517 precision, if any, gives the minimum number of digits that
518 must appear; if the converted value requires fewer digits,
519 it is padded on the left with zeros.
520 - \c p The <tt>void*</tt> argument is taken as an unsigned integer,
521 and converted similarly as a <tt>%\#x</tt> command would do.
522 - \c c The \c int argument is converted to an \c "unsigned char", and the
523 resulting character is written.
524 - \c s The <tt>char*</tt> argument is expected to be a pointer to an array
525 of character type (pointer to a string). Characters from
526 the array are written up to (but not including) a
527 terminating NUL character; if a precision is specified, no
528 more than the number specified are written. If a precision
529 is given, no null character need be present; if the
530 precision is not specified, or is greater than the size of
531 the array, the array must contain a terminating NUL
532 character.
533 - \c % A \c % is written. No argument is converted. The complete
534 conversion specification is "%%".
535 - \c eE The double argument is rounded and converted in the format
536 \c "[-]d.ddde±dd" where there is one digit before the
537 decimal-point character and the number of digits after it
538 is equal to the precision; if the precision is missing, it
539 is taken as 6; if the precision is zero, no decimal-point
540 character appears. An \e E conversion uses the letter \c 'E'
541 (rather than \c 'e') to introduce the exponent. The exponent
542 always contains two digits; if the value is zero,
543 the exponent is 00.
544 - \c fF The double argument is rounded and converted to decimal notation
545 in the format \c "[-]ddd.ddd", where the number of digits after the
546 decimal-point character is equal to the precision specification.
547 If the precision is missing, it is taken as 6; if the precision
548 is explicitly zero, no decimal-point character appears. If a
549 decimal point appears, at least one digit appears before it.
550 - \c gG The double argument is converted in style \c f or \c e (or
551 \c F or \c E for \c G conversions). The precision
552 specifies the number of significant digits. If the
553 precision is missing, 6 digits are given; if the precision
554 is zero, it is treated as 1. Style \c e is used if the
555 exponent from its conversion is less than -4 or greater
556 than or equal to the precision. Trailing zeros are removed
557 from the fractional part of the result; a decimal point
558 appears only if it is followed by at least one digit.
559 - \c S Similar to the \c s format, except the pointer is expected to
560 point to a program-memory (ROM) string instead of a RAM string.
561
562 In no case does a non-existent or small field width cause truncation of a
563 numeric field; if the result of a conversion is wider than the field
564 width, the field is expanded to contain the conversion result.
565
566 Since the full implementation of all the mentioned features becomes
567 fairly large, three different flavours of vfprintf() can be
568 selected using linker options:
569 - The default vfprintf() implements all the mentioned functionality
570 except floating point conversions.
571 - A minimized version of vfprintf() that only implements
572 the very basic integer and string conversion facilities, but only
573 the \c # additional option can be specified using conversion
574 flags (these flags are parsed correctly from the format
575 specification, but then are simply ignored).
576 - A version with floating point-support, but with the following twists:
577 - The argument will be converted to IEEE single for output.
578 - When \c long \c double is a 64-bit type and \c double is a 32-bit
579 type, then the former will only be printed as a single '?'.
580 Rationale is to avoid 64-bit floating point arithmetic in printf
581 when the used selected <tt>-mdouble=32</tt>. In order to print
582 IEEE double, it can be converted to IEEE single by hand.
583
584 The respective version can
585 be requested using the following \ref gcc_minusW "link options":
586
587 <dl>
588 <dt>Classic approach</dt>
589 <dd>
590 - The minimal version can be requested with the following options:
591 \code
592 -Wl,-u,vfprintf -lprintf_min
593 \endcode
594
595 - If the full functionality including the floating point conversions
596 is required, the following options should be used:
597 \code
598 -Wl,-u,vfprintf -lprintf_flt
599 \endcode
600
601 This approach will always link the selected printf code,
602 even when the application doesn't use printf.
603 </dd>
604 <dt>Since AVR-LibC v2.3</dt>
605 <dd>
606 - The minimal version can be requested with the following options:
607 \code
608 -Wl,--defsym,vfprintf=vfprintf_min
609 \endcode
610 - The full functionality including the floating point conversions
611 can be requested with:
612 \code
613 -Wl,--defsym,vfprintf=vfprintf_flt
614 \endcode
615
616 The difference to the "classic" approach is that when no printf
617 is used in the application and <tt>-Wl,\--gc-sections</tt> is added to
618 the linker options, then the printf code will not be pulled in. See
619 <a href="https://github.com/avrdudes/avr-libc/issues/654">issue \#654</a>
620 for an example.
621 </dd>
622 </dl>
623
624 <b>Limitations:</b>
625 - The specified width and precision can be at most 255.
626
627 <b>Notes:</b>
628 - For floating-point conversions, if you link the default or minimized
629 version of vfprintf(), the symbol <tt>?</tt> will be output.
630 The same applies for the float version with <tt>-mdouble=32</tt>
631 and when an IEEE double arguments is passed.
632 For default version the width field and the "pad to left" (symbol
633 \em minus) option will work in this case.
634 - The \c hh length modifier is ignored (\c char argument is
635 promouted to \c int). More exactly, this realization does not check
636 the number of \c h symbols.
637 - But the \c ll length modifier will to abort the output, as this
638 realization does not operate \c long \c long arguments.
639 - The variable width or precision field (an asterisk \c * symbol)
640 is not realized and will to abort the output.
641 */
642extern int vfprintf(FILE *__stream, const char *__fmt, va_list __ap);
643
644/**
645 Variant of \c vfprintf() that uses a \c fmt string that resides
646 in program memory. See also #vfprintf_F.
647*/
648extern int vfprintf_P(FILE *__stream, const char *__fmt, va_list __ap);
649
650/**
651 The function \c fputc sends the character \c c (though given as type
652 \c int) to \c stream. It returns the character, or \c EOF in case
653 an error occurred.
654*/
655extern int fputc(int __c, FILE *__stream);
656
657#if !defined(__DOXYGEN__)
658
659/* putc() function implementation, required by standard */
660extern int putc(int __c, FILE *__stream);
661
662/* putchar() function implementation, required by standard */
663extern int putchar(int __c);
664
665#endif /* not __DOXYGEN__ */
666
667/**
668 The macro \c putc used to be a "fast" macro implementation with a
669 functionality identical to fputc(). For space constraints, in
670 AVR-LibC, it is just an alias for \c fputc.
671*/
672#define putc(__c, __stream) fputc(__c, __stream)
673
674/**
675 The macro \c putchar sends character \c c to \c stdout.
676*/
677#define putchar(__c) fputc(__c, stdout)
678
679/**
680 The function \c printf performs formatted output to stream
681 \c stdout. See \c vfprintf() for details.
682*/
683extern int printf(const char *__fmt, ...);
684
685/**
686 Variant of \c printf() that uses a \c fmt string that resides
687 in program memory. See also #printf_F.
688*/
689extern int printf_P(const char *__fmt, ...);
690
691/**
692 The function \c vprintf performs formatted output to stream
693 \c stdout, taking a variable argument list as in vfprintf().
694
695 See vfprintf() for details.
696*/
697extern int vprintf(const char *__fmt, va_list __ap);
698
699/**
700 Variant of \c printf() that sends the formatted characters
701 to string \c s.
702*/
703extern int sprintf(char *__s, const char *__fmt, ...);
704
705/**
706 Variant of \c sprintf() that uses a \c fmt string that resides
707 in program memory. See also #sprintf_F.
708*/
709extern int sprintf_P(char *__s, const char *__fmt, ...);
710
711/**
712 Like \c sprintf(), but instead of assuming \c s to be of infinite
713 size, no more than \c n characters (including the trailing NUL
714 character) will be converted to \c s.
715
716 Returns the number of characters that would have been written to
717 \c s if there were enough space.
718*/
719extern int snprintf(char *__s, size_t __n, const char *__fmt, ...);
720
721/**
722 Variant of \c snprintf() that uses a \c fmt string that resides
723 in program memory. See also #snprintf_F.
724*/
725extern int snprintf_P(char *__s, size_t __n, const char *__fmt, ...);
726
727/**
728 Like \c sprintf() but takes a variable argument list for the
729 arguments.
730*/
731extern int vsprintf(char *__s, const char *__fmt, va_list __ap);
732
733/**
734 Variant of \c vsprintf() that uses a \c fmt string that resides
735 in program memory. See also #vsprintf_F.
736*/
737extern int vsprintf_P(char *__s, const char *__fmt, va_list __ap);
738
739/**
740 Like \c vsprintf(), but instead of assuming \c s to be of infinite
741 size, no more than \c n characters (including the trailing NUL
742 character) will be converted to \c s.
743
744 Returns the number of characters that would have been written to
745 \c s if there were enough space.
746*/
747extern int vsnprintf(char *__s, size_t __n, const char *__fmt, va_list __ap);
748
749/**
750 Variant of \c vsnprintf() that uses a \c fmt string that resides
751 in program memory. See also #vsnprintf_F.
752*/
753extern int vsnprintf_P(char *__s, size_t __n, const char *__fmt, va_list __ap);
754/**
755 The function \c fprintf performs formatted output to \c stream.
756 See \c vfprintf() for details.
757*/
758extern int fprintf(FILE *__stream, const char *__fmt, ...);
759
760/**
761 Variant of \c fprintf() that uses a \c fmt string that resides
762 in program memory. See also #fprintf_F.
763*/
764extern int fprintf_P(FILE *__stream, const char *__fmt, ...);
765
766/**
767 Write the string pointed to by \c str to stream \c stream.
768
769 Returns 0 on success and EOF on error.
770*/
771extern int fputs(const char *__str, FILE *__stream);
772
773/**
774 Variant of fputs() where \c str resides in program memory.
775 See also #fputs_F.
776*/
777extern int fputs_P(const char *__str, FILE *__stream);
778
779/**
780 Write the string pointed to by \c str, and a trailing newline
781 character, to \c stdout.
782*/
783extern int puts(const char *__str);
784
785/**
786 Variant of puts() where \c str resides in program memory.
787 See also #puts_F.
788*/
789extern int puts_P(const char *__str);
790
791/**
792 Write \c nmemb objects, \c size bytes each, to \c stream.
793 The first byte of the first object is referenced by \c ptr.
794
795 Returns the number of objects successfully written, i. e.
796 \c nmemb unless an output error occurred.
797 */
798extern size_t fwrite(const void *__ptr, size_t __size, size_t __nmemb, FILE *__stream);
799
800/**
801 The function \c fgetc reads a character from \c stream. It returns
802 the character, or \c EOF in case end-of-file was encountered or an
803 error occurred. The routines feof() or ferror() must be used to
804 distinguish between both situations.
805*/
806extern int fgetc(FILE *__stream);
807
808#if !defined(__DOXYGEN__)
809
810/* getc() function implementation, required by standard */
811extern int getc(FILE *__stream);
812
813/* getchar() function implementation, required by standard */
814extern int getchar(void);
815
816#endif /* not __DOXYGEN__ */
817
818/**
819 The macro \c getc used to be a "fast" macro implementation with a
820 functionality identical to fgetc(). For space constraints, in
821 AVR-LibC, it is just an alias for \c fgetc.
822*/
823#define getc(__stream) fgetc(__stream)
824
825/**
826 The macro \c getchar reads a character from \c stdin. Return
827 values and error handling is identical to fgetc().
828*/
829#define getchar() fgetc(stdin)
830
831/**
832 The ungetc() function pushes the character \c c (converted to an
833 unsigned char) back onto the input stream pointed to by \c stream.
834 The pushed-back character will be returned by a subsequent read on
835 the stream.
836
837 Currently, only a single character can be pushed back onto the
838 stream.
839
840 The ungetc() function returns the character pushed back after the
841 conversion, or \c EOF if the operation fails. If the value of the
842 argument \c c character equals \c EOF, the operation will fail and
843 the stream will remain unchanged.
844*/
845extern int ungetc(int __c, FILE *__stream);
846
847/**
848 Read at most <tt>size - 1</tt> bytes from \c stream, until a
849 newline character was encountered, and store the characters in the
850 buffer pointed to by \c str. Unless an error was encountered while
851 reading, the string will then be terminated with a \c NUL
852 character.
853
854 If an error was encountered, the function returns NULL and sets the
855 error flag of \c stream, which can be tested using ferror().
856 Otherwise, a pointer to the string will be returned. */
857extern char *fgets(char *__str, int __size, FILE *__stream);
858
859/**
860 Similar to fgets() except that it will operate on stream \c stdin,
861 and the trailing newline (if any) will not be stored in the string.
862 It is the caller's responsibility to provide enough storage to hold
863 the characters read. */
864extern char *gets(char *__str);
865
866/**
867 Read \c nmemb objects, \c size bytes each, from \c stream,
868 to the buffer pointed to by \c ptr.
869
870 Returns the number of objects successfully read, i. e.
871 \c nmemb unless an input error occurred or end-of-file was
872 encountered. feof() and ferror() must be used to distinguish
873 between these two conditions.
874 */
875extern size_t fread(void *__ptr, size_t __size, size_t __nmemb, FILE *__stream);
876
877/**
878 Clear the error and end-of-file flags of \c stream.
879 */
880extern void clearerr(FILE *__stream);
881
882
883/**
884 Test the end-of-file flag of \c stream. This flag can only be cleared
885 by a call to clearerr().
886 */
887extern int feof(FILE *__stream);
888
889
890/**
891 Test the error flag of \c stream. This flag can only be cleared
892 by a call to clearerr().
893 */
894extern int ferror(FILE *__stream);
895
896
897extern int vfscanf(FILE *__stream, const char *__fmt, va_list __ap);
898
899/**
900 Variant of vfscanf() using a \c fmt string in program memory.
901 See also #vfscanf_F.
902 */
903extern int vfscanf_P(FILE *__stream, const char *__fmt, va_list __ap);
904
905/**
906 The function \c fscanf performs formatted input, reading the
907 input data from \c stream.
908
909 See vfscanf() for details.
910 */
911extern int fscanf(FILE *__stream, const char *__fmt, ...);
912
913/**
914 Variant of fscanf() using a \c fmt string in program memory.
915 See also #fscanf_F.
916 */
917extern int fscanf_P(FILE *__stream, const char *__fmt, ...);
918
919/**
920 The function \c scanf performs formatted input from stream \c stdin.
921
922 See vfscanf() for details.
923 */
924extern int scanf(const char *__fmt, ...);
925
926/**
927 Variant of scanf() where \c fmt resides in program memory.
928 See also #scanf_F.
929 */
930extern int scanf_P(const char *__fmt, ...);
931
932/**
933 The function \c vscanf performs formatted input from stream
934 \c stdin, taking a variable argument list as in vfscanf().
935
936 See vfscanf() for details.
937*/
938extern int vscanf(const char *__fmt, va_list __ap);
939
940/**
941 The function \c sscanf performs formatted input, reading the
942 input data from the buffer pointed to by \c buf.
943
944 See vfscanf() for details.
945 */
946extern int sscanf(const char *__buf, const char *__fmt, ...);
947
948/**
949 Variant of sscanf() using a \c fmt string in program memory.
950 See also #sscanf_F.
951 */
952extern int sscanf_P(const char *__buf, const char *__fmt, ...);
953
954/**
955 Flush \c stream.
956
957 This is a null operation provided for source-code compatibility
958 only, as the standard IO implementation currently does not perform
959 any buffering.
960 */
961extern int fflush(FILE *stream);
962
963#ifndef __DOXYGEN__
964/* only mentioned for libstdc++ support, not implemented in library */
965#define BUFSIZ 1024
966#define _IONBF 0
967__extension__ typedef long long fpos_t;
968extern int fgetpos(FILE *stream, fpos_t *pos);
969extern FILE *fopen(const char *path, const char *mode);
970extern FILE *freopen(const char *path, const char *mode, FILE *stream);
971extern FILE *fdopen(int, const char *);
972extern int fseek(FILE *stream, long offset, int whence);
973extern int fsetpos(FILE *stream, fpos_t *pos);
974extern long ftell(FILE *stream);
975extern int fileno(FILE *);
976extern void perror(const char *s);
977extern int remove(const char *pathname);
978extern int rename(const char *oldpath, const char *newpath);
979extern void rewind(FILE *stream);
980extern void setbuf(FILE *stream, char *buf);
981extern int setvbuf(FILE *stream, char *buf, int mode, size_t size);
982extern FILE *tmpfile(void);
983extern char *tmpnam (char *s);
984#endif /* !__DOXYGEN__ */
985
986#ifdef __cplusplus
987}
988#endif
989
990/**@}*/
991
992#ifndef __DOXYGEN__
993/*
994 * The following constants are currently not used by AVR-LibC's
995 * stdio subsystem. They are defined here since the gcc build
996 * environment expects them to be here.
997 */
998#define SEEK_SET 0
999#define SEEK_CUR 1
1000#define SEEK_END 2
1001
1002#endif
1003
1004#endif /* __ASSEMBLER */
1005
1006#endif /* _STDIO_H_ */
unsigned char uint8_t
Definition: stdint.h:88
int vprintf(const char *__fmt, va_list __ap)
Definition: vprintf.c:35
int fscanf(FILE *__stream, const char *__fmt,...)
Definition: fscanf.c:35
int fprintf(FILE *__stream, const char *__fmt,...)
Definition: fprintf.c:35
int scanf_P(const char *__fmt,...)
Definition: scanf_p.c:36
int vsprintf(char *__s, const char *__fmt, va_list __ap)
Definition: vsprintf.c:37
int vsnprintf(char *__s, size_t __n, const char *__fmt, va_list __ap)
Definition: vsnprintf.c:36
int fputs(const char *__str, FILE *__stream)
Definition: fputs.c:35
#define putc(__c, __stream)
Definition: stdio.h:672
int sprintf_P(char *__s, const char *__fmt,...)
Definition: sprintf_p.c:37
int puts(const char *__str)
Definition: puts.c:35
int fprintf_P(FILE *__stream, const char *__fmt,...)
Definition: fprintf_p.c:36
int fputs_P(const char *__str, FILE *__stream)
Definition: fputs_p.c:36
int scanf(const char *__fmt,...)
Definition: scanf.c:35
int printf_P(const char *__fmt,...)
Definition: printf_p.c:36
int feof(FILE *__stream)
Definition: feof.c:37
int printf(const char *__fmt,...)
Definition: printf.c:35
FILE * fdevopen(int(*put)(char, FILE *), int(*get)(FILE *))
Definition: fdevopen.c:84
int snprintf_P(char *__s, size_t __n, const char *__fmt,...)
Definition: snprintf_p.c:36
size_t fread(void *__ptr, size_t __size, size_t __nmemb, FILE *__stream)
Definition: fread.c:35
int sscanf(const char *__buf, const char *__fmt,...)
Definition: sscanf.c:37
int vfprintf_P(FILE *__stream, const char *__fmt, va_list __ap)
Definition: vfprintf_p.c:36
int sprintf(char *__s, const char *__fmt,...)
Definition: sprintf.c:37
#define putchar(__c)
Definition: stdio.h:677
int vfscanf(FILE *__stream, const char *__fmt, va_list __ap)
Definition: vfscanf.c:827
char * gets(char *__str)
Definition: gets.c:35
int vfscanf_P(FILE *__stream, const char *__fmt, va_list __ap)
Definition: vfscanf_p.c:36
int snprintf(char *__s, size_t __n, const char *__fmt,...)
Definition: snprintf.c:36
int fscanf_P(FILE *__stream, const char *__fmt,...)
Definition: fscanf_p.c:36
int fgetc(FILE *__stream)
Definition: fgetc.c:35
int vscanf(const char *__fmt, va_list __ap)
Definition: vscanf.c:35
int vfprintf(FILE *__stream, const char *__fmt, va_list __ap)
int ferror(FILE *__stream)
Definition: ferror.c:37
void clearerr(FILE *__stream)
Definition: clearerr.c:37
int puts_P(const char *__str)
Definition: puts_p.c:36
int ungetc(int __c, FILE *__stream)
Definition: ungetc.c:35
int vsprintf_P(char *__s, const char *__fmt, va_list __ap)
Definition: vsprintf_p.c:37
#define getchar()
Definition: stdio.h:829
int vsnprintf_P(char *__s, size_t __n, const char *__fmt, va_list __ap)
Definition: vsnprintf_p.c:36
#define getc(__stream)
Definition: stdio.h:823
int fclose(FILE *__stream)
Definition: fclose.c:37
int fflush(FILE *stream)
Definition: fflush.c:35
char * fgets(char *__str, int __size, FILE *__stream)
Definition: fgets.c:35
size_t fwrite(const void *__ptr, size_t __size, size_t __nmemb, FILE *__stream)
Definition: fwrite.c:35
int sscanf_P(const char *__buf, const char *__fmt,...)
Definition: sscanf_p.c:37
struct __file FILE
Definition: stdio.h:272
int fputc(int __c, FILE *__stream)
Definition: fputc.c:35