Core Function PrintfC

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "<pre> PrintfC( <format control>, <params> ) </pre> === Description === Prints a formatted string (similar to the C sprintf() function). === Parameters === ==== format control...")
 
Line 98: Line 98:
 
$strings = array("fmtSeparator" => "#", "One", "Two", "Three");
 
$strings = array("fmtSeparator" => "#", "One", "Two", "Three");
 
PrintfC("Ints '%d' \nFloats '%g' \nStrings '%s'\n", $ints, $floats, $strings);
 
PrintfC("Ints '%d' \nFloats '%g' \nStrings '%s'\n", $ints, $floats, $strings);
 +
</syntaxhighlight>
 +
 +
Example using "fmtSeparator",  "fmtBegin" and "fmtEnd"
 +
 +
<syntaxhighlight lang="sputnik">
 +
$arr = array("fmtSeparator" => "\n", "fmtBegin" => "0x", "fmtEnd" => "<-- Hex yup", 0..50);
 +
PrintfC("Hex of the array\n%x\n", $arr);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Revision as of 19:51, 29 August 2013

PrintfC( <format control>, <params> )

Contents

Description

Prints a formatted string (similar to the C sprintf() function).

Parameters

format control

See Fmt( <format control>, <params> ) for format control.

params

See Fmt( <format control>, <params> ) for params.

Return Value

None.

Remarks

This is basically a wrapper for the Fmt( <format control>, <params> ) function so it will do exactly what that function does but print the result instantly and return no string.

See Fmt( <format control>, <params> ) for remarks.

Example

Large example

$n = 43951789;
$u = -43951789;
 
// notice the double %%, this prints a literal '%' character
PrintfC("%%d = '%d'\n", $n);             //'43951789'          standard integer representation
PrintfC("%%e = '%e'\n", $n);             //'4.395179e+007'     scientific notation
PrintfC("%%u = '%u'\n", $n);             //'43951789'          unsigned integer representation of a positive integer
PrintfC("%%u <0 = '%u'\n", $u);          //'4251015507'        unsigned integer representation of a negative integer
PrintfC("%%f = '%f'\n", $n);             //'43951789.000000'   floating point representation
PrintfC("%%.2f = '%.2f'\n", $n);         //'43951789.00'       floating point representation 2 digits after the decimal point
PrintfC("%%o = '%o'\n", $n);             //'247523255'         octal representation
PrintfC("%%s = '%s'\n", $n);             //'43951789'          string representation
PrintfC("%%x = '%x'\n", $n);             //'29ea6ad'           hexadecimal representation (lower-case)
PrintfC("%%X = '%X'\n", $n);             //'29EA6AD'           hexadecimal representation (upper-case)
 
PrintfC("%%+d = '%+d'\n", $n);           //'+43951789'         sign specifier on a positive integer
PrintfC("%%+d <0= '%+d'\n", $u);         //'-43951789'         sign specifier on a negative integer
 
 
$s = 'monkey';
$t = 'many monkeys';
 
PrintfC("%%s = [%s]\n", $s);             //[monkey]            standard string output
PrintfC("%%10s = [%10s]\n", $s);         //[    monkey]        right-justification with spaces
PrintfC("%%-10s = [%-10s]\n", $s);       //[monkey    ]        left-justification with spaces
PrintfC("%%010s = [%010s]\n", $s);       //[0000monkey]        zero-padding works on strings too
PrintfC("%%10.10s = [%10.10s]\n", $t);   //[many monke]        left-justification but with a cutoff of 10 characters
 
PrintfC("%04d-%02d-%02d\n", 2008, 4, 1);

If you are wondering how the hell to do a 64-bit signed integer

PrintfC("Int64 value is: '%I64d' ok\n", @Int64_Max);

If you are wondering how the hell to do a 64-bit unsigned integer

PrintfC("Int64 value is: '%llu' ok\n", @UInt64_Max);

Example of using arrays

$n = 777;
$t = array(100, 200, 300, 400);
PrintfC("Single value is '%d' but lets do an array of values '%d' cool huh?\n", $n, $t);

Example of using arrays with seperator

$n = 777;
$t = array("fmtSeparator" => "-", 100, 200, 300, 400);
PrintfC("Single value is '%d' but lets do an array of values '%d' cool huh?\n", $n, $t);

Large example with arrays

$ints = array("fmtSeparator" => "-", 100, 200, 300, 400);
$floats = array("fmtSeparator" => "#", 100.2, 777.42, 133.77, 88.1);
$strings = array("fmtSeparator" => "#", "One", "Two", "Three");
PrintfC("Ints '%d' \nFloats '%g' \nStrings '%s'\n", $ints, $floats, $strings);

Example using "fmtSeparator", "fmtBegin" and "fmtEnd"

$arr = array("fmtSeparator" => "\n", "fmtBegin" => "0x", "fmtEnd" => "<-- Hex yup", 0..50);
PrintfC("Hex of the array\n%x\n", $arr);
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox