Project

General

Profile

Up

sprintf

sprintf has a same objective than the C-standard function : formatted output conversion. See the note to the protoype.

Prototype :

fun [S u0] S

  • S : the formatted string. Currently, theses conversion specifiers are availables such as %d, %f, %x, %s, eventually with a precision :
    • %s : string
    • %d or %i : integer
    • %N or %n : integer with exposant
    • %c : character
    • %b : integer to binary (base 2)
    • %f or %F : float
    • %e or %E : float with exposant
    • %g or %G : float
    • %x or %X : integer in hexadecimal (base 16)
    • %h or %H : float in hexadecimal (base 16)
    • %o : in octal (base 8)
    • %p : pointer (memory reference)

  • u0 : a polymorphic tuple. Its size should be equal at the number of conversion specifiers. Otherwise, if the size is lesser, the last converters will be ignored. If the size is greater, the last items will be ignored. If an item is nil, the converter will be write again.
    Note : if it is not a tuple, the behavior is undefined.

Return : S a new string or nil if error

Error :

  • EOK if success.
  • EVM probably a memory error. The VM should be already crashed, see the log file, if any.
  • ERANGE if the size of the tuple and the number of converters is different.
  • EARG if the tuple could not be a tuple (this is not sure, it is just a strong probability).

See also :

printf

strcat

strcatn

Examples :

typeof string = S;;
...
set string = sprintf "%s is %s %d" ["Christmas Day" "December" 25]; 
_fooS string;
// >>>>> Christmas Day is December 25

typeof mylist = [[S S I] r1];; /* a string, an hex, an integer */
...
fun readMyList (list)=
	if list == nil then
		0
	else
	(
		// 'hd list' is a tuple [S S I] ...
		_fooS sprintf "1th : %s, 2nd : %x, 3rd : %i" hd list;
		readMyList tl list
	);;
	
fun main ()=
	_showconsole;
	...
	readMyList myList;
	...

An example with an item at nil :

typeof string = S;;
...
set string = sprintf "This %ith value is %i" [4 nil]; 
_fooS string;
// >>>>> This 4th is %i

Note