<< Previous | Next >> | |
|
Description
- This function is similar to
sprintf()
, but outputs the formatted string to the Stdio window. Prior to Dynamic C 7.25,printf()
would work only with the controller in program mode connected to a PC running Dynamic C. As of Dynamic C 7.25, it is possible to redirectprintf()
output to a serial port during run mode by defining a macro to specify the serial port. See the sample programSAMPLES/STDIO_SERIAL.C
for more information.
- See below for the complete list of Dynamic C Conversion Specifiers.
- The user should make sure that:
there are enough arguments after
fmt
to fill in the format parameters in the format stringthe types of arguments after
fmt
match the conversion specifiers infmt
- The macro
STDIO_DISABLE_FLOATS
can be defined if it is not necessary to format floating point numbers. If this macro is defined, %e, %f and %g will not be recognized. This can save thousands of bytes of code space.
- The macro
STDIO_ENABLE_LONG_STRINGS
can be defined if it is necessary to print strings to the Stdio window that are longer than the default of 127 bytes. Without defining this macro, such strings are truncated. The drawback of defining this macro is that if it is defined in a multi-tasking application where more than one task is utilizing printf and at least one of the tasks is printing strings longer than 127 bytes, the user must ensure that calls to printf are serialized via a semaphore or similar means. If calls to printf are not serialized under these conditions, printf output from the different tasks may be interleaved in the Stdio window.
NOTE this function is task reentrant and it has a 128 byte buffer. Parameters
- fmt
- String to be formatted.
- ...
- Format arguments.
Return Value
- Number of characters written
Library
- STDIO.LIB
See also
- sprintf
Dynamic C Conversion Specifiers
- %s - string
%ls - null terminated string in xmem
%d - signed decimal
%u - unsigned decimal
%f - float
%e - exponential
%g - floating point, same as %f or %e depending upon value and precision
%p - pointer
%lp - pointer
%x - hexadecimal, result in lowercase
%X - hexadecimal, same as %x but result in uppercase
%c - single character
- The precision specifier (the number between "%" and "s") determines the maximum number of characters to display.
- As shown in the screenshot above, a value to the right of "
.
" causes the string to be displayed with that number of characters, ignoring extra characters. A value by itself or to the left of ".
" causes padding. Negative values cause the string to be left justified, with spaces added to the right if necessary. Positive values cause the string to be right justified, with spaces added to the left if necessary.%l
s
- null terminated string in xmem
- This conversion specifier is identical to "%s" but the strings come from extended memory instead of root memory.
xdata mystring {"Now is the time."};
printf("%ls", mystring); // Now is the time.
- Width specifier l: short values must not include l; without l, long values are treated as short
- Precision specifier n: includes '-' and if necessary treats argument as signed
short n;
n = 30000;
printf("%d", n); // 30000
printf("%5d", n); // 30000
printf("%6d", n); // 30000
printf("%4d", n); // ****
unsigned short n;
n = 40000;
printf("%d", n); // -25536
printf("%6d", n); // -25536
printf("%7d", n); // -25536
printf("%5d", n); // *****
long n;
n = 300000;
printf("%ld", n); // 300000
printf("%7ld", n); // 300000
- Width specifier l: long values must include l, short values must not:
- Precision specifier n: includes '-' if necessary treats argument as if it were unsigned
short n;
n = -25536;
printf("%u", n); // 40000
unsigned short n;
n = 40000;
printf("%d", n); // 40000
- Width specifier l is ignored for Dynamic C float and double (both 4 bytes)
- Precision specifier
n.d
:n
is the total width including '-' and '.' ; if n is zero or is omitted, it is ignored and only d is used.float f;
f = -88.8888;
printf("%f", f); // -88.888801
printf("%10f", f); // -88.888801
printf("%9f", f); // *********
printf("%.0f", f); // -89
printf("%.3f", f); // -88.889
printf("%.0f", f); // -88.889
printf("%0.3f", f); // -88.889
printf("%7.3f", f); // -88.889
printf("%8.3f", f); // -88.889
printf("%6.3f", f); // ******
- Width specifier l is ignored for Dynamic C float and double (both 4 bytes)
- Precision specifier n.d: n is the total width excluding exponent sign; if n is zero or is omitted, it is ignored and only d is used; if n larger than width, the result is not padded. d is decimal places of n.nnn..E[+/-]nn format
float f;
f = -88.8888;
printf("%e\n", f); // -8.888880E+01
printf("%13e\n", f); // -8.888880E+01
printf("%12e\n", f); // -8.888880E+01
printf("%.0e\n", f); // -9E+01
printf("%.1e\n", f); // -8.9E+01
printf("%.3e\n", f); // -8.889E+01
printf("%0.3e\n", f); // -8.889E+01
printf("%9.3e\n", f); // -8.889E+01
printf("%15.3e\n", f); // -8.889E+01
printf("%8.3e\n", f); // ********
printf("%8.3e\n", -f); // 8.889E+01
- (Same as %f or %e depending upon value and precision.)
float f, g, h;
f = -888.8888;
g = 888888.0
g = 8888880.0
printf("%g\n", g); // 888888.0
printf("%g\n", h); // 8.888880E+06
printf("%g\n", f); //-888.888790
printf("%13g\n", f); // -888.888790
printf("%12g\n", f); // -888.888790
printf("%.0g\n", f); // -8.9E+02
printf("%.1g\n", f); // -8.9E+02
printf("%.2g\n", f); // -8.89E+02
printf("%.3g\n", f); // -888.889
printf("%7.3g\n", f); // *******
printf("%0.3g\n", f); // -888.889
printf("%9.3g\n", f); // -888.889
printf("%15.3g\n", f); // -888.889
printf("%8.3g\n", f); // -888.889
printf("%8.3g\n", -f); // 888.889
- Specifies a 16-bit logical pointer.
int i, *iptr;
i = 0;
ptr = &i;
printf("%p\n",ptr); // prints value of ptr in hex.
// logical memory location of i
- Specifies a 32-bit physical pointer.
long i, *iptr;
i = 0;
ptr = &i;
printf("%lp\n",ptr); // prints value of ptr in hex.
// physical memory location of i
- Result in lowercase
- Width specifier l: short values must not include l; without l, long values are treated as short
- Precision specifier n: must be at least as large as total width; treats argument as if it were unsigned
short n;
n = 30000;
printf("%x", n); //7530
printf("%5x", n); // 7530
printf("%6x", n); // 7530
printf("%3x", n); // ***
unsigned short n;
n = 40000;
printf("%x", n); // 9c40
long m, n;
m = -25536;
n = 0x10000 + 0xabc;
printf("%x\n", m); // 9c40
printf("%x\n", z); // abc
- Same as %x except the result is in uppercase.
- Precision specifier
n
is ignored for%c
; treats argument as if it werechar
long n;
n = 0x10000 + 0x100 + 'A';
printf("%0c", n); // A
short n;
n = 0x100 + 'A';
printf("%0c", n); // A
char n;
n = 'A';
printf("%0c", n); // A
- %o - octal
%E - same as %e, result uppercase (the result is always in uppercase in Dynamic C)
%G - same as %g, result uppercase (the result is always in uppercase in Dynamic C)
Dynamic C Functions | << Previous | Next >> | rabbit.com |