printf Function (tigcc.a)

stdio.h

void printf (const char *format, ...);

Sends formatted output to the screen.

printf is nearly full implementation of standard ANSI C printf function, which sends the formatted output to the screen in terminal (TTY) mode. In fact, it does the following:

The printed text will wrap on the right end of the screen. Characters '\n' will be translated to "next line" (and this is the only control code which has a special implementation). The screen will scroll upwards when necessary (i.e. after printing a text in the last screen line). Note that all TI fonts are supported. Of course, printf will update current "print position" to a new one after the text is printed.

printf applies the first format specifier to the first argument after format, the second to the second, and so on. The format string, controls how printf will convert and format its arguments. There must be enough arguments for the format; if there are not, the results will be unpredictable and likely disastrous. Excess arguments (more than required by the format) are merely ignored. The format string is a character string that contains two types of objects: plain characters and conversion specifications. Plain characters are simply copied verbatim to the output string. Conversion specifications fetch arguments from the argument list and apply formatting to them. printf format specifiers have the following form:

% [flags] [width] [.prec] [{h|l}] type

Here is a complete table of supported formatting options (see any book about C language for more info):

FlagsMeaning
noneRight align (pad spaces or zeros to left)
-Left align (pad spaces to right)
+Always force sign (include prefix '+' before positive values)
zDon't postfix padding (this option is non-ANSI, i.e. TI specific)
spaceInsert space before positive values
#Prefix octal values with 0 and hex values (>0) with '0x')
Force '.' in float output (and prevent trunctation of trailing zeros)
^TI-Float format: special character for the exponent and for the minus sign, no '+' prefix in the exponent, 0. instead of 0, no leading zeros if the magnitude is smaller than 1 (this option is non-ANSI, i.e. TI specific)
|Center the output in the field (this option is non-ANSI, i.e. TI specific)


WidthMeaning
numPrint at least num characters - padded the rest with blanks
0num(Zero prefixed) Same as above but padded with '0'
*The width is specified in the arguments list (before value being formatted)


PrecisionMeaning
noneDefault precision
numnum is number of chars, decimal places, or number of significant digits (num<=16) to display depending on type (see below)
-1Default = 6 digits (this option is non-ANSI, i.e. TI specific)
*The precision is specified in the argument list (before value being formatted)


Size {h|l}Meaning
hForce short integer
lForce long integer


TypeMeaning
d, iSigned decimal integer
uUnsigned decimal integer
xLowercase hexadecimal integer
XUppercase hexadecimal integer
eFloating point, format [-]d.dddde[sign]ddd (exponential format)
ELike 'e' but with uppercase letter for the exponent
fFloating point, format [-]dddd.dddd
gFloating point: most compact float format available ('e' or 'f'); this is the most common option, used for most dialog floats
GLike 'g' but with uppercase letter for the exponent
rFloating point, engineering form (this option is non-ANSI, i.e. TI specific)
RLike 'r' but with uppercase letter for the exponent
yFloating point, mode specified float format (this option is non-ANSI, i.e. TI specific)
YLike 'y' but with uppercase letter for the exponent
cCharacter
sString
pPointer; principally the same as 'x' - do not use without 'l' modifier
%None; the character '%' is printed instead

Here is a short demonstration of usage:
int i, j;
for (j = F_4x6; j <= F_8x10; j++)
  {
    clrscr ();
    FontSetSys (j);
    for (i = 1; i <= 1000; i++)
      printf ("%d ", i);
    ngetchx ();
  }
Note: In ANSI C, printf is an int function, and it returns the number of printed characters. Due to some practical reasons, this implementation of printf is a void function. This difference is usually not important.


Uses: fputchar, vcbprintf
Used by: gets, getsn