Important

   

This documentation space contains information about PATROL Agents when deployed in a TrueSight Operations Management environment. If you are a BMC Helix Operations Management user, see PATROL Agent 22.1 for BMC Helix Operations Management. Open link

printf()

Print text formatted to the C library printf() routine specification.

Syntax

printf(format)

Parameter

Parameter

Definition

format

text, variable names, and control characters that specify the content and format of output to the computer or task output window

Valid Values 
%d signed decimal (identical to %i) 
%i signed decimal (identical to %d) 
%u unsigned decimal 
%o unsigned octal 
%x unsigned hexadecimal using abcedf 
%X unsigned decimal using ABCDEF 
%c unsigned character 
%s character string 
%e double precision form drddde ± ddd where each d is a digit and r is the radix character 
%E double precision form drdddE ± ddd where each d is a digit and r is the radix character 
%f decimal notation form dddrddd where each d is a digit and r is the radix character 
%g print in the style of %e if the exponent after conversion is less than -4, else print in style %f 
%G print in the style of %E with the precision specifying the number of significant digits 
%N Group digits into threes and separate with commas beginning at the right of the string 
%% print a % character 
The format parameter does not support the standard C pointer conversion characters %p and %n.

The format parameter permits the following flags: 
- left-justify and pad on the right with spaces 
+ display plus sign when value is greater than zero 
0 pad with zeros if no other padding is specified 
# alters the meaning of a conversion: appends 0x or 0X to the %x and %X conversions always appends the radix character to the %e, %E, %f, %g, and %G conversions retains trailing zeros in the %g and %G conversions 
The # flag does not affect the %c, %d, %s, or %i conversions.

Description

The printf() function displays output to the computer or task output window using formatting similar to the standard Cprintf() function. 

A bad format or one that is valid for the C language but not for the printf() function results in a PSL run-time error that sets the PSL errno variable. 

The printf() function return value is always the NULL string.

C Conventions Not Supported by the PSL printf Function

The printf() function does not support the C convention of using the asterisk (*) as a field width or precision indicator. Theprintf() function does not support the %p and %n conversion characters. 

The length modifiers hl (ell), and L are not valid and are ignored by the printf() function. 

The printf() function format conversions are passed directly to the C library printf() routine on each platform. The output for obscure formatting features may differ across platforms, especially on platforms with libraries that do not conform to ANSI C requirements.

Conversion Differences between the C printf Routine and PSL printf Function

The format conversions have the same meaning between standard C and PSL, but the concept of variable types differs between the two. 

PSL supports only string types for its variables, and thus string arguments to the printf() function are converted in a manner appropriate for the format conversion:

  • Integral formats such as %d convert the string to signed integers.
  • Noninteger numeric formats such as %f convert to floating point values.
  • %c prints the ASCII equivalent of its integer argument or for nonnumeric arguments the first character of its argument. (Applying %c to "65" will print 'A' and to "AB" will print 'A'.)
  • %s causes no conversion.
  • %% requires no argument.

%N Format Conversion

The printf() function provides one nonstandard C extension--the %N conversion. The %N conversion preprocesses a numeric string so that commas separate each group of three digits beginning at the right side of the string. 

For example, the %N conversion causes the following conversions: 

1234 ⇒ 1,234 12345 ⇒ 12,345 123456 ⇒ 123,456 

The %N conversions ignores initial minus signs and blanks while searching for the first sequence of digits so that %N can be applied to negative values. If no digits are found after the skipped characters, the printed argument is unchanged. 

The %N conversion only modifies the first sequence of digits. For example, the %N conversion changes floating point numbers like 1234.1234 to become 1,234.1234 without changing to the digit sequence to the right of the decimal point. 

As part of the %N conversion, the printf() function performs a %s conversion using the field width and precision specifiers supplied in format . The printf() function prints the string resulting from the combined %N and %s conversions. Because of the embedded %s conversion, field width and precision under %N conversion have the same effect as with %s.

Note

Currently, no localization is supported by %N, and so the formatting achieved by %N does not change in different locales. 

Example

The following series of printf() function calls illustrate several formatting characters used for numbers and strings:

string = "1257384.00234600000";
printf(" string as an integer : %d\n",string);
printf(" string as a formatted integer : %7N\n",int(string));
printf(" string as a real : %f\n",string);
printf(" string as a formatted real : %12.4f\n",string);
printf(" string as a character string : %s\n",string);
printf(" string in scientific notation : %e\n",string);

The example produces the following output:

string as an integer : 1257384
string as a formatted integer : 1,257,384
string as a real : 1257384.002346
string as a formatted real : 1257384.0023
string as a character string : 1257384.00234600000
string in scientific notation : 1.257384e+06


Was this page helpful? Yes No Submitting... Thank you

Comments