<stdio.h>: Standard IO facilities


Defines

#define getchar()   fgetc(stdin)
#define FILE   struct __file
#define stdin   (__iob[0])
#define stdout   (__iob[1])
#define stderr   (__iob[2])
#define EOF   (-1)
#define fdev_set_udata(stream, u)   do { (stream)->udata = u; } while(0)
#define fdev_get_udata(stream)   ((stream)->udata)
#define fdev_setup_stream(stream, p, g, f)
#define _FDEV_SETUP_READ   __SRD
#define _FDEV_SETUP_WRITE   __SWR
#define _FDEV_SETUP_RW   (__SRD|__SWR)
#define _FDEV_ERR   (-1)
#define _FDEV_EOF   (-2)
#define FDEV_SETUP_STREAM(p, g, f)
#define fdev_close()   ((void)0)
#define putc(__c, __stream)   fputc(__c, __stream)
#define putchar(__c)   fputc(__c, stdout)
#define getc(__stream)   fgetc(__stream)
#define clearerror(s)   do { (s)->flags &= ~(__SERR | __SEOF); } while(0)
#define feof(s)   ((s)->flags & __SEOF)
#define ferror(s)   ((s)->flags & __SERR)

Functions

FILEfdevopen (int(*__put)(char, FILE *), int(*__get)(FILE *))
int fclose (FILE *__stream)
 Close a stream.
int vfprintf (FILE *__stream, const char *__fmt, va_list __ap)
int vfprintf_P (FILE *__stream, const char *__fmt, va_list __ap)
int fputc (int __c, FILE *__stream)
 Write a character to a stream.
int putc (int __c, FILE *__stream)
 Write a character to a stream.
int putchar (int __c)
 Write a character to standard output.
int printf (const char *__fmt,...)
int printf_P (const char *__fmt,...)
int vprintf (const char *__fmt, va_list __ap)
int sprintf (char *__s, const char *__fmt,...)
int sprintf_P (char *__s, const char *__fmt,...)
int snprintf (char *__s, size_t __n, const char *__fmt,...)
int snprintf_P (char *__s, size_t __n, const char *__fmt,...)
int vsprintf (char *__s, const char *__fmt, va_list ap)
int vsprintf_P (char *__s, const char *__fmt, va_list ap)
int vsnprintf (char *__s, size_t __n, const char *__fmt, va_list ap)
int vsnprintf_P (char *__s, size_t __n, const char *__fmt, va_list ap)
int fprintf (FILE *__stream, const char *__fmt,...)
int fprintf_P (FILE *__stream, const char *__fmt,...)
int fputs (const char *__str, FILE *__stream)
int fputs_P (const char *__str, FILE *__stream)
int puts (const char *__str)
int puts_P (const char *__str)
size_t fwrite (const void *__ptr, size_t __size, size_t __nmemb, FILE *__stream)
int fgetc (FILE *__stream)
 Read a character from a stream.
int getc (FILE *__stream)
 Read a character from a stream.
int ungetc (int __c, FILE *__stream)
 Push a character back onto a stream.
char * fgets (char *__str, int __size, FILE *__stream)
 Read a line from a stream.
char * gets (char *__str)
 Get a line from the standard input stream.
size_t fread (void *__ptr, size_t __size, size_t __nmemb, FILE *__stream)
 Read data from a stream.
void clearerr (FILE *__stream)
 Reset error status of a stream.
int feof (FILE *__stream)
 Test if a stream reached the end of file.
int ferror (FILE *__stream)
 Test for an error on a stream.
int vfscanf (FILE *__stream, const char *__fmt, va_list __ap)
int vfscanf_P (FILE *__stream, const char *__fmt, va_list __ap)
int fscanf (FILE *__stream, const char *__fmt,...)
int fscanf_P (FILE *__stream, const char *__fmt,...)
int scanf (const char *__fmt,...)
int scanf_P (const char *__fmt,...)
int vscanf (const char *__fmt, va_list __ap)
int sscanf (const char *__buf, const char *__fmt,...)
int sscanf_P (const char *__buf, const char *__fmt,...)
static __inline__ int fflush (FILE *stream __attribute__((unused)))

Variables

struct __file__iob []

Detailed Description

 #include <stdio.h> 

Introduction to the Standard IO facilities

This file declares the standard IO facilities that are implemented in 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 printf conversions that have been left out). Nevertheless, potential users of this implementation should be warned: the printf and 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.

Tunable options for code size vs. feature set

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 scanf family of functions.

Outline of the chosen API

The standard streams stdin, stdout, and 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 fopen() that could be used to associate a stream to some device. (See note 1.) Instead, the function 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 \n is sent literally down to the device's put() function. If the device requires a carriage return (\r) character to be sent before the linefeed, its put() routine must implement this (see note 2).

As an alternative method to fdevopen(), the macro fdev_setup_stream() might be used to setup a user-supplied FILE structure.

It should be noted that the automatic conversion of a newline character into a carriage return - newline sequence breaks binary transfers. If binary transfers are desired, no automatic conversion should be performed, but instead any string that aims to issue a CR-LF sequence must use "\r\n" explicitly.

For convenience, the first call to fdevopen() that opens a stream for reading will cause the resulting stream to be aliased to stdin. Likewise, the first call to fdevopen() that opens a stream for writing will cause the resulting stream to be aliased to both, stdout, and 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 fclose() on such a stream will also effectively close all of its aliases (note 3).

It is possible to tie additional user data to a stream, using fdev_set_udata(). The backend put and get functions can then extract this user data using fdev_get_udata(), and act appropriately. For example, a single put function could be used to talk to two different UARTs that way, or the put and get functions could keep internal state between calls there.

Format strings in flash ROM

All the printf and 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 PSTR (explained in avr_pgmspace) becomes very handy for declaring these format strings.

Running stdio without malloc()

By default, fdevopen() requires malloc(). As this is often not desired in the limited environment of a microcontroller, an alternative option is provided to run completely without malloc().

The macro fdev_setup_stream() is provided to prepare a user-supplied FILE buffer for operation with stdio.

Example

    #include <stdio.h>

    static int uart_putchar(char c, FILE *stream);

    static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
                                             _FDEV_SETUP_WRITE);

    static int
    uart_putchar(char c, FILE *stream)
    {

      if (c == '\n')
        uart_putchar('\r', stream);
      loop_until_bit_is_set(UCSRA, UDRE);
      UDR = c;
      return 0;
    }

    int
    main(void)
    {
      init_uart();
      stdout = &mystdout;
      printf("Hello, world!\n");

      return 0;
    }

This example uses the initializer form FDEV_SETUP_STREAM() rather than the function-like fdev_setup_stream(), so all data initialization happens during C start-up.

If streams initialized that way are no longer needed, they can be destroyed by first calling the macro fdev_close(), and then destroying the object itself. No call to fclose() should be issued for these streams. While calling fclose() itself is harmless, it will cause an undefined reference to free() and thus cause the linker to link the malloc module into the application.

Notes

Note 1:
It might have been possible to implement a device abstraction that is compatible with 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.
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 put() for fdevopen() that talks to a UART interface might look like this:
    int
    uart_putchar(char c, FILE *stream)
    {

      if (c == '\n')
        uart_putchar('\r');
      loop_until_bit_is_set(UCSRA, UDRE);
      UDR = c;
      return 0;
    }

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 printf() instead of fprintf(mystream, ...) 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 stdin will also save some execution time.

Define Documentation

#define _FDEV_EOF   (-2)

Return code for an end-of-file condition during device read.

To be used in the get function of fdevopen().

Definition at line 373 of file stdio.h.

#define _FDEV_ERR   (-1)

Return code for an error condition during device read.

To be used in the get function of fdevopen().

Definition at line 366 of file stdio.h.

#define _FDEV_SETUP_READ   __SRD

fdev_setup_stream() with read intent

Definition at line 357 of file stdio.h.

#define _FDEV_SETUP_RW   (__SRD|__SWR)

fdev_setup_stream() with read/write intent

Definition at line 359 of file stdio.h.

#define _FDEV_SETUP_WRITE   __SWR

fdev_setup_stream() with write intent

Definition at line 358 of file stdio.h.

#define clearerror (  )     do { (s)->flags &= ~(__SERR | __SEOF); } while(0)

Definition at line 850 of file stdio.h.

#define EOF   (-1)

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.

Definition at line 312 of file stdio.h.

 
#define fdev_close (  )     ((void)0)

This macro frees up any library resources that might be associated with stream. It should be called if stream is no longer needed, right before the application is going to destroy the stream object itself.

(Currently, this macro evaluates to nothing, but this might change in future versions of the library.)

Definition at line 448 of file stdio.h.

#define fdev_get_udata ( stream   )     ((stream)->udata)

This macro retrieves a pointer to user defined data from a FILE stream object.

Definition at line 323 of file stdio.h.

#define fdev_set_udata ( stream,
 )     do { (stream)->udata = u; } while(0)

This macro inserts a pointer to user defined data into a FILE stream object.

The user data can be useful for tracking state in the put and get functions supplied to the fdevopen() function.

Definition at line 319 of file stdio.h.

#define FDEV_SETUP_STREAM ( p,
g,
 ) 

Value:

{ \
                .put = p, \
                .get = g, \
                .flags = f, \
                .udata = 0, \
        }

Definition at line 387 of file stdio.h.

#define fdev_setup_stream ( stream,
p,
g,
 ) 

Value:

do { \
                (stream)->put = p; \
                (stream)->get = g; \
                (stream)->flags = f; \
                (stream)->udata = 0; \
        } while(0)

Definition at line 348 of file stdio.h.

#define feof (  )     ((s)->flags & __SEOF)

Definition at line 861 of file stdio.h.

#define ferror (  )     ((s)->flags & __SERR)

Definition at line 872 of file stdio.h.

#define FILE   struct __file

FILE is the opaque structure that is passed around between the various standard IO functions.

Definition at line 275 of file stdio.h.

#define getc ( __stream   )     fgetc(__stream)

The macro getc used to be a "fast" macro implementation with a functionality identical to fgetc(). For space constraints, in avr-libc, it is just an alias for fgetc.

Definition at line 788 of file stdio.h.

 
int getchar (  )     fgetc(stdin)

The macro getchar reads a character from stdin. Return values and error handling is identical to fgetc().

Definition at line 794 of file stdio.h.

#define putc ( __c,
__stream   )     fputc(__c, __stream)

The macro putc used to be a "fast" macro implementation with a functionality identical to fputc(). For space constraints, in avr-libc, it is just an alias for fputc.

Definition at line 638 of file stdio.h.

#define putchar ( __c   )     fputc(__c, stdout)

The macro putchar sends character c to stdout.

Definition at line 643 of file stdio.h.

Referenced by getbuf().

#define stderr   (__iob[2])

Stream destined for error output. Unless specifically assigned, identical to stdout.

If stderr should point to another stream, the result of another fdevopen() must be explicitly assigned to it without closing the previous stderr (since this would also close stdout).

Definition at line 304 of file stdio.h.

#define stdin   (__iob[0])

Stream that will be used as an input stream by the simplified functions that don't take a stream argument.

The first stream opened with read intent using fdevopen() will be assigned to stdin.

Definition at line 284 of file stdio.h.

#define stdout   (__iob[1])

Stream that will be used as an output stream by the simplified functions that don't take a stream argument.

The first stream opened with write intent using fdevopen() will be assigned to both, stdin, and stderr.

Definition at line 293 of file stdio.h.


Function Documentation

void clearerr ( FILE stream  ) 

Reset error status of a stream.

Clear the error and end-of-file flags of stream.

Parameters:
stream Pointer to a previously opened stream.
Note:
This function does nothing.

Definition at line 61 of file clrerr.c.

int fclose ( FILE stream  ) 

Close a stream.

This function closes stream, and disallows and further IO to and from it.

When using fdevopen() to setup the stream, a call to fclose() is needed in order to free the internal resources allocated.

If the stream has been set up using fdev_setup_stream() or FDEV_SETUP_STREAM(), use fdev_close() instead.

It currently always returns 0 (for success).

The calling thread may be suspended until all buffered output data has been written.

Parameters:
stream Pointer to a previously opened stream.
Returns:
0 if the stream is successfully closed, EOF otherwise.

Definition at line 69 of file fclose.c.

References __iob, _close(), EBADF, EOF, errno, FOPEN_MAX, free, and __iobuf::iob_fd.

FILE* fdevopen ( int(*)(char, FILE *)  __put,
int(*)(FILE *)  __get 
)

int feof ( FILE stream  ) 

Test if a stream reached the end of file.

Test the end-of-file flag of stream. This flag can only be cleared by a call to clearerr().

Parameters:
stream Pointer to a previously opened stream.
Returns:
0 if the current position is not the end of the file.

Definition at line 61 of file feof.c.

References _IOEOF, and __iobuf::iob_flags.

int ferror ( FILE stream  ) 

Test for an error on a stream.

Test the error flag of stream. This flag can only be cleared by a call to clearerr().

Parameters:
stream Pointer to a previously opened stream.
Returns:
0 if no error occured.

Definition at line 62 of file ferror.c.

References _IOERR, and __iobuf::iob_flags.

static __inline__ int fflush ( FILE *stream   __attribute__(unused)  )  [static]

Definition at line 938 of file stdio.h.

int fgetc ( FILE stream  ) 

Read a character from a stream.

The function fgetc reads a character from stream. It returns the character, or EOF in case end-of-file was encountered or an error occurred. The routines feof() or ferror() must be used to distinguish between both situations.

Parameters:
stream Pointer to a previously opened stream.
Returns:
Character read or EOF to indicate an error or end of file. In the latter case feof() or ferror() can be used to determine the cause of the failure.
Warning:
The function will not check, if the stream pointer points to a valid stream.

Definition at line 71 of file fgetc.c.

References _IOEOF, _IOERR, _IOUNG, _read(), EOF, __iobuf::iob_fd, __iobuf::iob_flags, and __iobuf::iob_unget.

char* fgets ( char *  buffer,
int  count,
FILE stream 
)

Read a line from a stream.

Read at most size - 1 bytes from stream, until a newline character was encountered, and store the characters in the buffer pointed to by str. Unless an error was encountered while reading, the string will then be terminated with a NUL character.

If an error was encountered, the function returns NULL and sets the error flag of stream, which can be tested using ferror(). Otherwise, a pointer to the string will be returned.

Read at most one less than the specified number of characters from a stream or stop when a newline has been read.

Parameters:
buffer Pointer to the buffer that receives the data including the linefeed character.
count Maximum number of characters to read.
stream Pointer to a previously opened stream.
Returns:
Pointer to the given buffer or NULL to indicate an error or the end of the file.
Warning:
The function will not check, if the stream pointer points to a valid stream.

Definition at line 68 of file fgets.c.

References EOF, and fgetc().

int fprintf ( FILE __stream,
const char *  __fmt,
  ... 
)

The function fprintf performs formatted output to stream. See vfprintf() for details.

int fprintf_P ( FILE __stream,
const char *  __fmt,
  ... 
)

Variant of fprintf() that uses a fmt string that resides in program memory.

int fputc ( int  c,
FILE stream 
)

Write a character to a stream.

The function fputc sends the character c (though given as type int) to stream. It returns the character, or EOF in case an error occurred.

Parameters:
c Character to write.
stream Pointer to a previously opened stream.
Returns:
The character written or EOF to indicate an error.
Warning:
The function will not check, if the stream pointer points to a valid stream.

Definition at line 65 of file fputc.c.

References _write(), EOF, and __iobuf::iob_fd.

int fputs ( const char *  __str,
FILE __stream 
)

Write the string pointed to by str to stream stream.

Returns 0 on success and EOF on error.

int fputs_P ( const char *  __str,
FILE __stream 
)

Variant of fputs() where str resides in program memory.

size_t fread ( void *  buffer,
size_t  size,
size_t  count,
FILE stream 
)

Read data from a stream.

Read nmemb objects, size bytes each, from stream, to the buffer pointed to by ptr.

Returns the number of objects successfully read, i. e. nmemb unless an input error occured or end-of-file was encountered. feof() and ferror() must be used to distinguish between these two conditions.

Parameters:
buffer Pointer to the buffer that receives the data.
size Item size in bytes.
count Maximum number of items to read.
stream Pointer to a previously opened stream.
Returns:
The number of full items read, which may be less then the specified number.
Warning:
The function will not check, if the stream pointer points to a valid stream.

Definition at line 71 of file fread.c.

References _IOEOF, _IOERR, _IOUNG, _read(), __iobuf::iob_fd, __iobuf::iob_flags, and __iobuf::iob_unget.

int fscanf ( FILE __stream,
const char *  __fmt,
  ... 
)

The function fscanf performs formatted input, reading the input data from stream.

See vfscanf() for details.

int fscanf_P ( FILE __stream,
const char *  __fmt,
  ... 
)

Variant of fscanf() using a fmt string in program memory.

size_t fwrite ( const void *  __ptr,
size_t  __size,
size_t  __nmemb,
FILE __stream 
)

Write nmemb objects, size bytes each, to stream. The first byte of the first object is referenced by ptr.

Returns the number of objects successfully written, i. e. nmemb unless an output error occured.

int getc ( FILE stream  ) 

Read a character from a stream.

Same as fgetc().

Parameters:
stream Pointer to a previously opened stream.
Returns:
Character read or EOF to indicate an error or end of file.
Warning:
The function will not check, if the stream pointer points to a valid stream.

Definition at line 66 of file getc.c.

References fgetc().

char* gets ( char *  buffer  ) 

Get a line from the standard input stream.

Similar to fgets() except that it will operate on stream stdin, and the trailing newline (if any) will not be stored in the string. It is the caller's responsibility to provide enough storage to hold the characters read.

Read characters from a stream until a newline has been read.

Parameters:
buffer Pointer to the buffer that receives the line excluding the linefeed character.
Returns:
The given argument on success, 0 otherwise.
Warning:
This function is potentially unsafe, because it may write pass the end of the buffer.

Definition at line 64 of file gets.c.

References EOF, fgetc(), and stdin.

int printf ( const char *  __fmt,
  ... 
)

The function printf performs formatted output to stream stderr. See vfprintf() for details.

int printf_P ( const char *  __fmt,
  ... 
)

Variant of printf() that uses a fmt string that resides in program memory.

int putc ( int  c,
FILE stream 
)

Write a character to a stream.

Same as fputc().

Parameters:
c Character to write.
stream Pointer to a previously opened stream.
Returns:
The character written or EOF to indicate an error.
Warning:
The function will not check, if the stream pointer points to a valid stream.

Definition at line 67 of file putc.c.

References fputc().

int putchar ( int  c  ) 

Write a character to standard output.

Parameters:
c Character to write.
Returns:
The character written or EOF to indicate an error.

Definition at line 61 of file putchar.c.

References fputc(), and stdout.

int puts ( const char *  __str  ) 

Write the string pointed to by str, and a trailing newline character, to stdout.

int puts_P ( const char *  __str  ) 

Variant of puts() where str resides in program memory.

int scanf ( const char *  __fmt,
  ... 
)

The function scanf performs formatted input from stream stdin.

See vfscanf() for details.

int scanf_P ( const char *  __fmt,
  ... 
)

Variant of scanf() where fmt resides in program memory.

int snprintf ( char *  __s,
size_t  __n,
const char *  __fmt,
  ... 
)

Like sprintf(), but instead of assuming s to be of infinite size, no more than n characters (including the trailing NUL character) will be converted to s.

Returns the number of characters that would have been written to s if there were enough space.

int snprintf_P ( char *  __s,
size_t  __n,
const char *  __fmt,
  ... 
)

Variant of snprintf() that uses a fmt string that resides in program memory.

int sprintf ( char *  __s,
const char *  __fmt,
  ... 
)

Variant of printf() that sends the formatted characters to string s.

int sprintf_P ( char *  __s,
const char *  __fmt,
  ... 
)

Variant of sprintf() that uses a fmt string that resides in program memory.

int sscanf ( const char *  __buf,
const char *  __fmt,
  ... 
)

The function sscanf performs formatted input, reading the input data from the buffer pointed to by buf.

See vfscanf() for details.

int sscanf_P ( const char *  __buf,
const char *  __fmt,
  ... 
)

Variant of sscanf() using a fmt string in program memory.

int ungetc ( int  c,
FILE stream 
)

Push a character back onto a stream.

The ungetc() function pushes the character c (converted to an unsigned char) back onto the input stream pointed to by stream. The pushed-back character will be returned by a subsequent read on the stream.

Currently, only a single character can be pushed back onto the stream.

The ungetc() function returns the character pushed back after the conversion, or EOF if the operation fails. If the value of the argument c character equals EOF, the operation will fail and the stream will remain unchanged.

Parameters:
c Character to push back.
stream Pointer to a previously opened stream.
Warning:
Only a single character can be pushed back. Any previously pushed and not yet read character will be lost.

Definition at line 60 of file ungetc.c.

References _IOUNG, EOF, __iobuf::iob_flags, and __iobuf::iob_unget.

int vfprintf ( FILE __stream,
const char *  __fmt,
va_list  __ap 
)

vfprintf is the central facility of the printf family of functions. It outputs values to stream under control of a format string passed in fmt. The actual values to print are passed as a variable argument list ap.

vfprintf returns the number of characters written to stream, or EOF in case of an error. Currently, this will only happen if stream has not been opened with write intent.

The format string is composed of zero or more directives: ordinary characters (not %), 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 % character. The arguments must properly correspond (after type promotion) with the conversion specifier. After the %, the following appear in sequence:

  • Zero or more of the following flags:
    • # 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.
    • 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.
    • - 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.
    • ' ' (space) A blank should be left before a positive number produced by a signed conversion (d, or i).
    • + A sign must always be placed before a number produced by a signed conversion. A + overrides a space if both are used.

  • 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-adjustment flag has been given) to fill out the field width.
  • An optional precision, in the form of a period . followed by an optional digit string. If the digit string is omitted, the precision is taken as zero. This gives the minimum number of digits to appear for d, i, o, u, x, and X conversions, or the maximum number of characters to be printed from a string for s conversions.
  • An optional l or h length modifier, that specifies that the argument for the d, i, o, u, x, or X conversion is a "long int" rather than int. The h is ignored, as "short int" is equivalent to int.
  • A character that specifies the type of conversion to be applied.

The conversion specifiers and their meanings are:

  • diouxX The int (or appropriate variant) argument is converted to signed decimal (d and i), unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal (x and X) notation. The letters "abcdef" are used for x conversions; the letters "ABCDEF" are used for X conversions. The precision, if any, gives the minimum number of digits that must appear; if the converted value requires fewer digits, it is padded on the left with zeros.
  • p The void * argument is taken as an unsigned integer, and converted similarly as a %#x command would do.
  • c The int argument is converted to an "unsigned char", and the resulting character is written.
  • s The "char *" argument is expected to be a pointer to an array of character type (pointer to a string). Characters from the array are written up to (but not including) a terminating NUL character; if a precision is specified, no more than the number specified are written. If a precision is given, no null character need be present; if the precision is not specified, or is greater than the size of the array, the array must contain a terminating NUL character.
  • % A % is written. No argument is converted. The complete conversion specification is "%%".
  • eE The double argument is rounded and converted in the format "[-]d.dddeħdd" where there is one digit before the decimal-point character and the number of digits after it is equal to the precision; if the precision is missing, it is taken as 6; if the precision is zero, no decimal-point character appears. An E conversion uses the letter 'E' (rather than 'e') to introduce the exponent. The exponent always contains two digits; if the value is zero, the exponent is 00.
  • fF The double argument is rounded and converted to decimal notation in the format "[-]ddd.ddd", where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is explicitly zero, no decimal-point character appears. If a decimal point appears, at least one digit appears before it.
  • gG The double argument is converted in style f or e (or F or E for G conversions). The precision specifies the number of significant digits. If the precision is missing, 6 digits are given; if the precision is zero, it is treated as 1. Style e is used if the exponent from its conversion is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit.
  • S Similar to the s format, except the pointer is expected to point to a program-memory (ROM) string instead of a RAM string.

In no case does a non-existent or small field width cause truncation of a numeric field; if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result.

Since the full implementation of all the mentioned features becomes fairly large, three different flavours of vfprintf() can be selected using linker options. The default vfprintf() implements all the mentioned functionality except floating point conversions. A minimized version of vfprintf() is available that only implements the very basic integer and string conversion facilities, but only the # additional option can be specified using conversion flags (these flags are parsed correctly from the format specification, but then simply ignored). This version can be requested using the following compiler options:

   -Wl,-u,vfprintf -lprintf_min

If the full functionality including the floating point conversions is required, the following options should be used:

   -Wl,-u,vfprintf -lprintf_flt -lm

Limitations:
  • The specified width and precision can be at most 255.
Notes:
  • For floating-point conversions, if you link default or minimized version of vfprintf(), the symbol ? will be output and double argument will be skiped. So you output below will not be crashed. For default version the width field and the "pad to left" ( symbol minus ) option will work in this case.
  • The hh length modifier is ignored (char argument is promouted to int). More exactly, this realization does not check the number of h symbols.
  • But the ll length modifier will to abort the output, as this realization does not operate long long arguments.
  • The variable width or precision field (an asterisk * symbol) is not realized and will to abort the output.

int vfprintf_P ( FILE __stream,
const char *  __fmt,
va_list  __ap 
)

Variant of vfprintf() that uses a fmt string that resides in program memory.

int vfscanf ( FILE __stream,
const char *  __fmt,
va_list  __ap 
)

int vfscanf_P ( FILE __stream,
const char *  __fmt,
va_list  __ap 
)

Variant of vfscanf() using a fmt string in program memory.

int vprintf ( const char *  __fmt,
va_list  __ap 
)

The function vprintf performs formatted output to stream stdout, taking a variable argument list as in vfprintf().

See vfprintf() for details.

int vscanf ( const char *  __fmt,
va_list  __ap 
)

The function vscanf performs formatted input from stream stdin, taking a variable argument list as in vfscanf().

See vfscanf() for details.

int vsnprintf ( char *  __s,
size_t  __n,
const char *  __fmt,
va_list  ap 
)

Like vsprintf(), but instead of assuming s to be of infinite size, no more than n characters (including the trailing NUL character) will be converted to s.

Returns the number of characters that would have been written to s if there were enough space.

int vsnprintf_P ( char *  __s,
size_t  __n,
const char *  __fmt,
va_list  ap 
)

Variant of vsnprintf() that uses a fmt string that resides in program memory.

int vsprintf ( char *  __s,
const char *  __fmt,
va_list  ap 
)

Like sprintf() but takes a variable argument list for the arguments.

int vsprintf_P ( char *  __s,
const char *  __fmt,
va_list  ap 
)

Variant of vsprintf() that uses a fmt string that resides in program memory.


Variable Documentation

struct __file* __iob[]

Definition at line 56 of file fopen.c.


Generated on Sun Aug 31 13:31:33 2008 for FrankenRTOS by  doxygen 1.5.6