ILE C/C++ Programmer's Guide

Formatting the Value of a Formatted C Input or Output Function

The behavior of the fprintf(), sprintf(), vfprintf(), vprintf() and vsprintf() functions is the same as the printf() function. The behavior of the fscanf() and sscanf() functions is the same as the scanf() function.

To control the format of the output use the flags, width and precision fields of the printf() function.

To control the format of the scanf() function use the * and width fields of the scanf() function. See the VisualAge for C++ for AS/400 C Library Reference for information on these fields.

Print Function Flags

Table 16 describes the flag characters and their meanings for D(n,p) conversions.

Table 16. Flag Meanings for Printing the Value of a _DecimalT Class Template Object

Flag Meaning
# The result always contains a decimal-point character, even if no digits follow it.
0 Leading zeros (following any indication of sign or base) are used to pad to the field width; no space padding is performed.
- The result is always left-justified within the field.
+ The result always begins with a plus or minus sign.
space The result is always prefixed with a space where the result of a signed conversion is no sign or the signed conversion results in no characters.

Print Function Field Width

The optional minimum field width for the printf() function indicates the minimum number of digits to appear in the integral part, fractional part or both parts of a _DecimalT class template object. If there are fewer characters than the field width, then the field is padded with spaces. The field width can be an *. If n is an *, the value of n is derived from the corresponding position in the parameter list.

Print Function Field Precision

The optional precision for the printf() function indicates the number of digits to appear in the fractional part of a _DecimalT class template object. The default precision is p. If precision is greater than p, extra zeros are padded. If precision is less than p, rounding is performed. The field precision can be an *. If p is an *, the value of p is derived from the corresponding position in the parameter list.

Conversion Specifiers

The conversion specifier for the printf() function is:

 D(n,p) 
The _DecimalT class template object is converted in the style [ - ] ddd.ddd where the number of digits after the decimal-point character is equal to the precision of the specification. If the precision is missing, it is taken as 0; if the precision is zero and the #flag is not specified, no decimal-point character appears. If a decimal-point character appears, at least one digit appears before it. The value is truncated to the appropriate number of digits. The (n,p) descriptor is used to describe the characteristic of the _DecimalT class template object. Both n and p have to be in the form of decimal integers. If p is missing, a default value of zero is assumed. If the specifier is in another form not stated above, the behavior is undefined.

If n and p of the variable to be printed do not match with the n and p in the conversion specifier %D(n,p), the behavior is undefined. Use the unary operators __digitsof (expression) and __precisionof (expression) in the argument list to replace the * in D(*,*) whenever the size of the resulting class of a _DecimalT class template object expression is not known.

The conversion specifier for the scanf() function is as follows:

 D(n,p) 
Matches a decimal number, the expected form of the subject sequence is an optional plus or minus sign, then a non-empty sequence of digits optionally containing a decimal point. The subject sequence is defined as the longest initial subsequence of the input string, starting with the first non-white-space character, that is of the expected form. The subject sequence contains no characters if the input string is empty or consists entirely of white space, or if the first non-white-space character is other than a sign, a digit, or a decimal-point character.


[ Top of Page | Previous Page | Next Page | Table of Contents ]