Format Commands

Format commands insert values into character strings known as variable substitution. These commands specify where to insert the character string and how to format it. Format commands begin with a percent (%) sign followed by a format code. There are two categories of format commands: Character format and Time format.

Character Format Commands

The sprintf() function AND fprintf and printf procedures use character format commands. The following table describes the commands.

Character Format Command
%d Decimal value
%i Integer value
%o Octal value
%s String of characters
%u Unsigned decimal value
%x Character hexadecimal value without a leading zero and with letters in lowercase (that is, 0x87a4)
%X Character hexadecimal value without a leading zero and with letters in uppercase (that is, 0X87A4)
%% Percent sign
This demonstrates how character format commands work. Given the following character string,
I have x dogs, y cats, and z fish

The character format commands can be used to insert actual numeric values for x, y and z. This is done as follows:

printf ("I have %d dogs, %d cats, and %d fish", DogCount, CatCount,\FishCount);

DogCount, CatCount and FishCount are variables containing numeric values.

The interpreter sequentially replaces each format command with one of the provided variables.

The replacement is done in sequential order. The first format command gets the first variable, and the second format command gets the second variable, etc.

Format commands can also use field modifiers to specify field width and whether to left justify a field.

Minimum Field-Width Modifier

An integer placed between the percent sign and the command character determines the minimum width of a field. By default, the pad character is a blank. To pad with zeros instead of spaces, place a zero before the minimum field-width specifier.

For example, %04d pads an integer value with zeros if the integer value is less than four digits in length.

Maximum Field-Width Modifier

A decimal point, followed by a maximum field width determines the maximum width of a field. If the value is longer than the specified maximum length, the value truncates on the right.

For example, %2.4d generates a field with a minimum length of two digits and a maximum length of four characters.

Left-Justification Field Modifier

By default, all output is right-justified. To left-justify a field, place a minus sign directly after the percent sign.

For example, %-2.4d generates a left-justified field with a minimum length of two digits and a maximum length of four digits.

Time Format Commands

The strftime() function uses time format commands. The following table describes the commands.

Time format commands can vary based on the operating system. We recommend that you consult the strftime manual pages for your local pbmasterd system.

Character Command
%a The abbreviated weekday name according to the current locale.
%A The full weekday name according to the current locale.
%b The abbreviated month name according to the current locale.
%B The full month name according to the current locale.
%c The preferred date and time representation for the current locale.
%C The century number (year/100) as a two-digit integer.
%d The day of the month as a decimal number (range 01 - 31).
%D Equivalent to %m/%d/%y.
%e Like %d, the day of the month as a decimal number, but space replaces a leading zero.
%E Modifier. Use alternative format.
%g Like %G but without the century, (that is, with a 2-digit year, 00-99).
%G The ISO 8601 year with century as a decimal number. The four-digit year that corresponds to the ISO week number (see %V). This has the same format as %y except that if the ISO week number belongs to the previous or next year, that year is used instead.
%h Equivalent to %b.
%H The hour as a decimal number using a 24-hour clock (00-23).
%I The hour as a decimal number using a 12-hour clock (01-12).
%j The day of the year as a decimal number (001-366).
%k The hour (24-hour clock) as a decimal number (0-23). A blank precedes single digits. See also %H.
%l The hour (12-hour clock) as a decimal number (1-12). A blank precedes single digits. See also %I.
%m The month as a decimal number (01-12).
%M The minute as a decimal number (00-59).
%n A new line character.
%O Modifier. Use alternative format.
%p Either AM or PM according to the given time value or the corresponding strings for the current locale. Noon is PM and midnight is AM.
%P Like %p but in lowercase: am or pm or a corresponding string for the current locale.
%r The time in AM or PM notation.
%R The time in 24-hour notation (%H:%M). For a version that includes seconds, see %T.
%s The number of seconds since the Epoch.
%S The second as a decimal number (00-61).
%t A tab character.
%T The time in 24-hour notation (%H:%M:%S).
%u The day of the week as a decimal (1-7) with Monday being 1.
%U The week number of the current year as a decimal number (00-53) starting with the first Sunday as the first day of week 01.
%V The ISO 8601:1998 week number of the current year as a decimal number (01-53) where week 1 is the first week that has at least four days in the current year and Monday as the first day of the week.
%w The day of the week as a decimal (0-6) with Sunday being 0.
%W The week number of the current year as a decimal number (00-53) starting with the first Monday as the first day of week 01.
%x The preferred date representation for the current locale without the time.
%X The preferred time representation for the current locale without the date.
%y The year as a decimal number without a century (00-99).
%Y The year as a decimal number including the century.
%z The time zone as hour offset from GMT.
%Z The time zone name or abbreviation.
%+ The date and time in date(1) format.
%% A % character.

The time format commands work in the same manner as character format commands.