contents   index   previous   next



Clib.printf()

syntax:

Clib.printf(formatString[, variables ...])

where:

formatString - string that specifies the final format.

 

variables - values to be converted to and formatted as a string.

 

return:

number - characters written, or a negative number if there is an error.

 

description:

This method writes output to the standard output device according to the format string and returns a number equal to the number of characters written, or a negative number if there is an error. The format string can contain character combinations indicating how following parameters are to be treated. Characters are printed as read to standard output until a percent character, %, is reached. % indicates that a value is to be printed from the parameters following the format string. Each subsequent parameter specification takes from the next parameter in the list following format. A parameter specification has the following form in which square brackets indicate optional fields and angled brackets indicate required fields:

 

%[flags][width][.precision]<type>

 

flags may be:

 

-
Left justification in the field with blank padding; else right justifies with zero or blank padding

+
Force numbers to begin with a plus (+) or minus ()

blank
Negative values begin with a minus (); positive values begin with a blank

#
Convert using the following alternate form, depending on output data type:

c, s, d, i, u
No effect

o
0 (zero) is prepended to nonzero output

x, X
0x, or 0X, are prepended to output

f, e, E
Output includes decimal even if no digits follow decimal

g, G
Same as e or E but trailing zeros are not removed

 

width may be:

 

n
(n is a number e.g., 14) At least n characters are output, padded with blanks

0n
At least n characters are output, padded on the left with zeros

*
The next value in the argument list is an integer specifying the output width

.precision
If precision is specified, then it must begin with a period (.), and may be as follows:

 

0
For floating point type, no decimal point is output

n
n characters or n decimal places (floating point) are output

*
The next value in the argument list is an integer specifying the precision width

 

type may be:

 

d, i
signed integer

u
unsigned integer

o
octal integer x

x
hexadecimal integer with 09 and a, b, c, d, e, f

X
hexadecimal integer with 09 and A, B, C, D, E, F

f
floating point of the form []dddd.dddd

e
floating point of the form []d.ddde+dd or []d.dddedd

E
floating point of the form []d.dddE+dd or []d.dddEdd

g
floating point of f or e type, depending on precision

G
floating point of For E type, depending on precision

c
character (e.g. 'a', 'b', '8')

s
string

 

To include the % character as a character in the format string, you must use two % characters together, %%, to prevent the computer from trying to interpret it as one of the above forms.

 

see:

Clib.sprintf()

 

example:

//Each of the following lines shows

// a printf example followed by what would show

// on the output in boldface:

 

Clib.printf("Hello world!")

// Hello world! 

Clib.printf("I count: %d %d %d.",1,2,3)

// I count: 1 2 3 

var a = 1;

var b = 2;

Clib.printf("%d %d %d", a, b, a +b)

// 1 2 3 

 


Clib.getch()