Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

ecvt(3C)

nl_init(3C)

putc(3S)

scanf(3S)

stdio(3S)

PRINTF(3S)  —  HP-UX

NAME

printf, nl_printf, fprintf, nl_fprintf, sprintf, nl_sprintf − print formatted output

SYNOPSIS

#include <stdio.h>

int printf (format [ , arg ] ... )
char ∗format;

int nl_printf (format [ , arg ] ... )
char ∗format;

int fprintf (stream, format [ , arg ] ... )
FILE ∗stream;
char ∗format;

int nl_fprintf (stream, format [ , arg ] ... )
FILE ∗stream;
char ∗format;

int sprintf (s, format [ , arg ] ... )
char ∗s, ∗format;

int nl_sprintf (s, format [ , arg ] ... )
char ∗s, ∗format;

DESCRIPTION

Printf and nl_printf place output on the standard output stream stdout.

Fprintf and nl_fprintf place output on the named output stream.

Sprintf and nl_sprintf place "output", followed by the null character (\0), in consecutive bytes starting at ∗s. It is the user’s responsibility to ensure that enough storage is available.

Each function returns the number of bytes (not whole characters) transmitted (excluding the \0 in the case of sprintf and nl_sprintf), or a negative value if an output error was encountered.

Each function converts, formats, and prints its args under control of the format. The format is a character string containing two types of objects: plain characters (single- or multi-byte) that are copied to the output stream, and conversion specifications, each of which results in fetching zero or more args. The results are undefined if there are insufficient args for the format. If the format is exhausted while args remain, excess args are ignored.

Each conversion specification is introduced by the character %, or alternatively for the functions nl_printf, nl_fprintf, and nl_sprintf only, the character sequence %n$, where n is a decimal integer in the range (1-{NL_ARGMAX}) (NL_ARGMAX is defined in <limits.h>).  The %n$ construction indicates that this conversion should be applied to the nth argument, rather than to the next unused one. 

The user is responsible for ensuring that each %n$ argument is used exactly once.  The two forms of introducing a conversion specification, % and %n$, may not be mixed within a single format string.  Improper use of %n$ in a format string will result in a negative return value from nl_printf, nl_fprintf, or nl_sprintf. 

After the % or %n$, the following appear in sequence:

Zero or more flags, which modify the meaning of the conversion specification.

An optional string of decimal digits to specify a minimum field width in bytes. If the converted value has fewer characters than the field width, it will be padded on the left (or right, if the left-adjustment flag "−", described below, has been given) to the field width.  If the field width is preceded by a zero, the string is right adjusted with zero-padding on the left. 

A precision that gives the minimum number of digits to appear for the d, i, o, u, x, or X conversions, the number of digits to appear after the radix character for the e and f conversions, the maximum number of significant digits for the g conversion, or the maximum number of bytes to be printed from a string in s conversion.  The precision takes the form of a period (.) followed by a decimal digit string; a null digit string is treated as zero. 

An optional l (the letter "ell"), specifying that a following d, i, o, u, x, or X conversion character applies to a long integer arg, or an optional h, specifying that a following d, i, o, u, x, or X conversion character applies to a short integer arg. An l before any other conversion character is ignored. 

A conversion character that indicates the type of conversion to be applied. 

A field width or precision may be indicated by an asterisk (∗) instead of a digit string.  In this case, an integer arg supplies the field width or precision.  The arg that is actually converted is not fetched until the conversion letter is seen, so the args specifying field width or precision must appear before the arg to be converted.  Field width or precision always indicates bytes, not whole characters. 

The flag characters and their meanings are:

− The resulting conversion will be left-justified within the field. 

+ The resulting signed conversion will always begin with a sign (+ or −). 

blank If the first character of a signed conversion is not a sign, a blank will be prefixed to the result.  This implies that if the blank and + flags both appear, the blank flag will be ignored. 

# This flag specifies that the value is converted to an "alternate form."  For c, d, i, s, and u conversions, the flag has no effect.  For o conversion, it increases the precision to force the first digit of the result to be a zero.  For x or X conversion, a non-zero result will have 0x or 0X prefixed to it.  For e, E, f, g, and G conversions, the result will always contain a radix character, even if no digits follow the point (normally, a radix character appears in the resulting conversions only if followed by a digit).  For g and G conversions, trailing zeroes will not be removed from the result (which they normally are). 

The conversion characters and their meanings are:

d,i,o,u,x,X The integer arg is converted to signed decimal (d and i are identical), unsigned octal, decimal, or hexadecimal notation (x and X), respectively; the letters abcdef are used for x conversion and the letters ABCDEF for X conversion.  The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it will be expanded with leading zeroes.  (For compatibility with older versions, padding with leading zeroes may alternatively be specified by prepending a zero to the field width.  This does not imply an octal value for the field width.)  The default precision is 1.  The result of converting a zero value with a precision of zero is a null string. 

f The float or double arg is converted to decimal notation in the style "[−]dddrddd", where r is the radix character specified by the currently loaded NLS environment (see nl_init(3C)). If nl_init has not been called successfully, the default NLS environment ­"n-computer" is used, and specifies a period (.) as the radix character.  The number of digits after the radix character is equal to the precision specification.  If the precision is missing, six digits are output.  If the precision is explicitly zero, no radix character appears. 

e,E The float or double arg is converted in the style "[−]drddde±ddd", where r is the radix character specified by the NLS environment loaded (see nl_init(3C)). If nl_init has not been called successfully, the default NLS environment ­"n-computer" is used which specifies a period (.) as the radix character.  There is one digit before the radix character and the number of digits after it is equal to the precision; when the precision is missing, six digits are produced; if the precision is zero, no radix character appears.  The E format code will produce a number with E instead of e introducing the exponent.  The exponent always contains at least two digits. 

g,G The float or double arg is printed in style f or e (or in style E in the case of a G format code), with the precision specifying the number of significant digits.  The style used depends on the value converted: style e will be used only if the exponent resulting from the conversion is less than −4 or greater than the precision.  Trailing zeroes are removed from the result; a radix character appears only if it is followed by a digit. 

c The character arg is printed. 

s The arg is taken to be a string (character pointer) and characters from the string are printed until a null character (\0) is encountered or the number of characters indicated by the precision specification is reached.  Only whole single- or multi-byte characters are printed.  If a precision might result in truncation of a multi-byte character, the entire character is dropped and the result is padded as needed.  If the precision is missing, it is taken to be infinite, so all characters up to the first null character are printed.  A NULL value for arg will yield undefined results. 

% Print a %; no argument is converted. 

In no case does a nonexistent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result. 

Characters generated by printf, fprintf, nl_printf, and nl_fprintf are printed as if putc(3S) had been called.

EXAMPLES

To print a date and time in the form "Sunday, July 3, 10:02", where weekday and month are pointers to null-terminated strings:

printf("%s, %s %d, %d:%.2d", weekday, month, day, hour, min);

To print π to 5 decimal places:

printf("pi = %.5f", 4 ∗ atan(1.0));

To create a language independent date and time printing routine write:

nl_printf(format,weekday,month,day,hour,min);

For American usage, format would point to the string:

"%1$, %2$s %3$d, %4$d:%5$.2d"

and result in the output:

Sunday, July 3, 10:02

For German usage, the string:

"%1$, %3$s %2$d, %4$d:%5$.2d"

results in the output:

Sonntag, 3 Juli 10:02

AUTHOR

Printf, fprintf and sprintf were developed by AT&T and HP.  Nl_printf, nl_fprintf and nl_sprintf were developed by HP. 

SEE ALSO

ecvt(3C), nl_init(3C), putc(3S), scanf(3S), stdio(3S). 

INTERNATIONAL SUPPORT

8-bit data. 

Hewlett-Packard Company  —  May 11, 2021

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026