printf


Description:

[ CCode ( cname = "fprintf" ) ]
[ PrintfFormat ]
public void printf (string format, ...)

Writes the results to a file stream stream.

The format string consists of whitespace characters, non-whitespace characters (except %) and conversion specifications. Each conversion specification has the following format:

%[flags][width][.precision][length]specifier

Flags:

Character Description
+ The sign of signed conversions is always prepended to the result of the conversion
<space> if the result of a signed conversion does not start with a sign character, or is empty, space is prepended to the result. It is ignored if + flag is present.
- The result of the conversion is left-justified within the field
# Alternative form of the conversion is performed.
0 Use 0 instead of spaces for padding when width is specified

Width:

Integer value or * that specifies minimum field width. The result is padded with space characters (by default), if required, on the left when right-justified, or on the right if left-justified. In the case when * is used, the width is specified by an additional argument of type int. If the value of the argument is negative, it results with the - flag specified and positive field width.

Precision:

. followed by integer number or * that specifies precision of the conversion. In the case when * is used, the precision is specified by an additional argument of type int. If the value of this argument is negative, it is ignored.

Length:

Character Description
hh char
h short
l long
ll long-long
L long double
z sizue_t
j intmax_t
t ptrdiff_t

Specifier:

specifier Output Example
a Floating point, hexadecimal, lowercase -0xc.90fep-2
A Floating point, hexadecimal, uppercase -0XC.90FEP-2
c Character c
d, i integer 251
e Mantissa/exponent (scientific), lowercase 2.5121e+2
E Mantissa/exponent (scientific), uppercase 2.5121E+2
f Floating point, decimal, lowercase 2.32
F Floating point, decimal, uppercase 2.32
g Floating point, shortest representation (%e or %f) 512.23
G Floating point, shortest representation (%E or %F) 512.23
n Nothing
o Octal 320
p Pointer address b2010020
s String my str
u Decimal integer 93535
x Integer, hexadecimal, lowercase 3ab
X Integer, hexadecimal, uppercase 3AB
% %% to output % %

Examples:

Format: Parameters: Output:
"Characters:\t%c %%\n" 65 "Characters: A %"
"Decimal:\t%i %d %.6i %i %.0i %+i %u\n" 1, 2, 3, 0, 0, 4, -1 "Decimal: 1 2 000003 0 +4 4294967295"
"Hexadecimal:\t%x %x %X %#x\n" 5, 10, 10, 6 "Hexadecimal: 5 a A 0x6"
"Octal:\t%o %#o %#o\n" 10, 10, 4 "Octal: 12 012 04"
"Rounding:\t%f %.0f %.32f\n" 1.5, 1.5, 1.3 "Rounding: 1.500000 2 1.30000000626"
"Padding:\t%05.2f %.2f %5.2f\n" 1.5, 1.5, 1.5 "Padding: 01.50 1.50 1.50"
"Scientific:\t%E %e\n" 1.5, 1.5 "Scientific: 1.500000E+00 1.500000e+00"
"Hexadecimal:\t%a %A\n" 1.5, 1.5 "Hexadecimal: 0x1.8p+0 0X1.8P+0"

Example: Write a formatted string to the stream:

public static int main (string[] args) {
string mystr = "mystr";
short myshort = 3;
ushort myushort = 3;
int myint = -10;
uint myuint = 10;
long mylong = 100;
long myulong = 100;
float myfloat = 0.10f;
char mychar = 'a';
char myuchar = 'a';
unichar myunichar = 'z';
void* myptr = null;
bool mybool = true;
size_t mysizet = 10;
ssize_t myssizet = 10;
int8 myint8 = 2;
uint8 myuint8 = 2;
int16 myint16 = 2;
uint16 myuint16 = 2;
int32 myint32 = 2;
uint32 myuint32 = 2;
int64 myint64 = 2;
uint64 myuint64 = 2;

// Output:
// `` 1. ptr: (nil)``
// `` 2. percent: % 3. string: mystr``
print (" 1. ptr: %p\n", myptr);
print (" 2. percent: %%\n");

// Output:
// `` 3. string: mystr``
// `` 4. char: 97``
// `` 5. uchar: 97``
// `` 6. unichar: z
print (" 3. string: %s\n", mystr);
print (" 4. char: %d\n", mychar);
print (" 5. uchar: %hhu\n", myuchar);
print (" 6. unichar: %s\n", myunichar.to_string ());

// Output:
// `` 7. short: 3``
// `` 8. ushort: 3``
print (" 7. short: %hi\n", myshort);
print (" 8. ushort: %hu\n", myushort);

// Output:
// `` 9. int8: 2``
// ``10. uint8: 2``
print (" 9. int8: %hhi\n", myint8);
print ("10. uint8: %hhu\n", myuint8);

// Output:
// ``11. int16: 2``
// ``12. uint16: 2``
print ("11. int16: %" + int16.FORMAT + "\n", myint16);
print ("12. uint16: %" + uint16.FORMAT + "\n", myuint16);

// Output:
// ``13. int32: 2``
// ``14. uint32: 2``
print ("13. int32: %" + int32.FORMAT + "\n", myint32);
print ("14. uint32: %" + uint32.FORMAT + "\n", myuint32);

// Output:
// ``15. int64: 2``
// ``16. uint64: 2``
print ("15. int64: %" + int64.FORMAT + "\n", myint64);
print ("16. uint64: %" + uint64.FORMAT + "\n", myuint64);

// Output:
// ``17. int: -10``
// ``18. int: -10``
// ``19. octal: 37777777766``
// ``20. hex,lower: fffffff6``
// ``21. hex,upper: FFFFFFF6``
// ``22. uint: 10``
print ("17. int: %d\n", myint);
print ("18. int: %i\n", myint);
print ("19. octal: %o\n", myint);
print ("20. hex,lower: %x\n", myint);
print ("21. hex,upper: %X\n", myint);
print ("22. uint: %u\n", myuint);

// Output:
// ``23. long: 100``
// ``24. ulong: 100``
print ("23. long: %li\n", mylong);
print ("24. ulong: %lu\n", myulong);

// Output:
// ``25. size_t: 10``
// ``26. ssize_t: 10``
print ("25. size_t: %" + size_t.FORMAT + "\n", mysizet);
print ("26. ssize_t: %" + ssize_t.FORMAT + "\n", myssizet);

// Output:
// ``27. float,lower: 0.100000``
// ``28. float,upper: 0.100000``
// ``29. scientific,lower: 1.000000e-01``
// ``30. scientific,upper: 1.000000E-01``
// ``31. shortest,lower: 0.1``
// ``32. shortest,upper: 0.1``
// ``33. hex,lower: 0x1.99999ap-4``
// ``34. hex,upper: 0X1.99999AP-4``
print ("27. float,lower: %f\n", myfloat);
print ("28. float,upper: %F\n", myfloat);
print ("29. scientific,lower: %e\n", myfloat);
print ("30. scientific,upper: %E\n", myfloat);
print ("31. shortest,lower: %g\n", myfloat);
print ("32. shortest,upper: %G\n", myfloat);
print ("33. hex,lower: %a\n", myfloat);
print ("34. hex,upper: %A\n", myfloat);

// Output:
// ``35. true``
print ("35. %s\n", mybool.to_string ());
return 0;
}

valac --pkg glib-2.0 GLib.FileStream.printf.vala

Parameters:

format

a string specifying how to interpret the data. (See GLib.FileStream.printf)

...

variable argument list containing the data to print.