The Math Functions (MATH.H)

Chapter 11 - Complete I/O in C

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Formatting Output
C’s rich assortment of output formatting controls makes it easy to create a neatly printed graph, report, or table. The two main functions that accomplish this formatted output are printf( ) and the file equivalent form, fprintf( ). These functions can use any of the format specifiers shown in Table 11-2. The format specification uses the following form:
Table 11-2: Format Specifiers for printf() and fprintf() Functions
TYPE FIELD:
Character

Type

Format of Output
c
int or wint_t
printf( ) means a
single-byte character.
wprintf( ) means a
wide character.
C
int or wint_t
printf( ) means a
wide character.
wprintf( ) means a single-byte character.
d
int
Signed decimal integer.
e
double
Signed number of the form [-]d.ddd e [sign]dddd.
Here, d is a single decimal digit, ddd is one or more decimal digits, dddd is exactly four decimal digits, and the sign is a + or -.
E
double
Same as e, except “E” is in front of exponent.
f
double
Signed number of the form [-]ddd.ddd. Here, ddd is one or more decimal digits. The number of digits after the decimal point depends upon the precision.
g
double
Signed number in f or e format. The most compact format is used. No trailing zeroes. No decimal point if no digits follow it.
G
double
Same as g format, except “E” is in front of exponent.
i
int
Signed decimal integer.
n
Pointer to integer
The # of characters written
to the stream or buffer.
Address of buffer given as integer argument.
o
int
Unsigned octal integer.
p
Pointer to void
Address (given by argument) is printed.
s
String
printf( ) gives a single-byte-character string.
wprintf( ) gives a wide-character string.
(print to NULL or
max precision)
S
String
printf( ) gives a wide-character string.
wprintf( ) gives a single-byte-character string.
(print to NULL or max precision)
u
int
Unsigned decimal integer.
x
int
Unsigned hexadecimal integer (lowercase letters used).
X
int
Unsigned hexadecimal integer (uppercase
letters used).
FLAG FIELD:
Flag

Meaning
-
Left-align the result (right alignment is the default).
+
Use a leading sign (+ or -) if number is a signed type (sign used with negative number only is the default).
0
When width has 0 prefix, zeroes will be added until the minimum width is reached (no padding is the default).
blank (‘ ‘)
Output is prefixed with a blank. If positive and signed, the blank will be ignored
(no appearing blanks is
the default).
#
Prefixes nonzero output value with 0, 0x, or 0X (no appearing blank is the default).
.
For e, E, or f formats, a # makes the output value contain a decimal point in all cases (point appears only if digits follow is the default).
%[flags] [width] [.precision] [{h | l | L}]type
The field of the specification is a character or a number that gives a format option. The simplest form can be just a percent sign and a type. For example, %f. type is used to determine if the argument is to be interpreted as a character, a string, or a number. flags are used to control the printing of signs, blanks, decimal points, radix of output, and so on. width refers to the minimum number of characters to print. precision refers to the maximum number of characters that are printed for the output. h | l | L are optional prefixes for giving the argument size.
Using printf( ) and fprintf( )
The following example program defines four variable types: character, array-of-characters, integer, and real. It then demonstrates how to use the appropriate format controls on each variable. The source code has been heavily commented and output line numbering has been included to make associating the output generated with the statement that created it as simple as possible:
/*
*   printf.c
*   A C program demonstrating advanced conversions and  
*   formatting
*   Copyright (c) Bill/Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>

void main( )
{
 char   c        =   ‘A’,
        psz1[]   =   “In making a living today many no ”,
        psz2[]   =   “longer leave any room for life.”;
 int    iln      =   0,
        ivalue   =   1234;
 double dPi      =   3.14159265;
 /*          conversions            */

 /
* print the c                     */
 printf(“\n[%2d] %c”,++iln,c);

 /
* print the ASCII code for c      */
 printf(“\n[%2d] %d”,++iln,c);

 /
* print character with ASCII 90   */
 printf(“\n[%2d] %c”,++iln,90);

 /
* print ivalue as octal value     */
 printf(“\n[%2d] %o”,++iln,ivalue);

 /
* print lower-case hexadecimal    */
 printf(“\n[%2d] %x”,++iln,ivalue);

 /
* print upper-case hexadecimal    */
 printf(“\n[%2d] %X”,++iln,ivalue);

 /
* conversions and format options  */

 /
* minimum width 1                 */
 printf(“\n[%2d] %c”,++iln,c);

 /
* minimum width 5, right-justify  */
 printf(“\n[%2d] %5c”,++iln,c);

 /
* minimum width 5, left-justify   */
 printf(“\n[%2d] %-5c”,++iln,c);

 /
* 33 non-null, automatically      */
 printf(“\n[%d] %s”,++iln,psz1);

 /
* 31 non-null, automatically      */
 printf(“\n[%d] %s”,++iln,psz2);

 /
* minimum 5 overridden, auto 33   */
 printf(“\n[%d] %5s”,++iln,psz1);
 /* minimum width 38, right-justify */
 printf(“\n[%d] %38s”,++iln,psz1);

 /
* minimum width 38, left-justify  */
 printf(“\n[%d] %-38s”,++iln,psz2);

 /
* default ivalue width, 4         */
 printf(“\n[%d] %d”,++iln,ivalue);

 /
* printf ivalue with + sign       */
 printf(“\n[%d] %+d”,++iln,ivalue);

 /
* minimum 3 overridden, auto 4    */
 printf(“\n[%d] %3d”,++iln,ivalue);

 /
* minimum width 10, right-justify */
 printf(“\n[%d] %10d”,++iln,ivalue);

 /
* minimum width 10, left-justify  */
 printf(“\n[%d] %-d”,++iln,ivalue);
 /* right justify with leading 0’s  */
 printf(“\n[%d] %010d”,++iln,ivalue);

 /
* using default number of digits  */
 printf(“\n[%d] %f”,++iln,dPi);

 /
* minimum width 20, right-justify */
 printf(“\n[%d] %20f”,++iln,dPi);

 /
* right-justify with leading 0’s  */
 printf(“\n[%d] %020f”,++iln,dPi);

 /
* minimum width 20, left-justify  */
 printf(“\n[%d] %-20f”,++iln,dPi);

 /
* no longer available since R1.2  */
 /
* left-justify with trailing 0’s  */


 /
* additional formatting precision */

 /
* minimum width 19, print all 17  */
 printf(“\n[%d] %19.19s”,++iln,psz1);

 /
* prints first 2 chars            */
 printf(“\n[%d] %.2s”,++iln,psz1);

 /
* prints 2 chars, right-justify   */
 printf(“\n[%d] %19.2s”,++iln,psz1);

 /
* prints 2 chars, left-justify    */
 printf(“\n[%d] %-19.2s”,++iln,psz1);

 /
* using printf arguments          */
 printf(“\n[%d] %
*.*s”,++iln,19,6,psz1);

 /
* width 10, 8 to right of ‘.’     */
 printf(“\n[%d] %10.8f”,++iln,dPi);
 /* width 20, 2 to right-justify    */
 printf(“\n[%d] %20.2f”,++iln,dPi);

 /
* 4 decimal places, left-justify  */
 printf(“\n[%d] %-20.4f”,++iln,dPi);

 /
* 4 decimal places, right-justify */
 printf(“\n[%d] %20.4f”,++iln,dPi);

 /
* width 20, scientific notation   */
 printf(“\n[%d] %20.2e”,++iln,dPi);
}
The output generated by the program looks like this:
[ 1] A
[ 2] 65
[ 3] Z
[ 4] 2322
[ 5] 4d2
[ 6] 4D2
[ 7] A
[ 8]     A
[ 9] A    
[10] In making a living today many no
[11] longer leave any room for life.
[12] In making a living today many no
[13]      In making a living today many no
[14] longer leave any room for life.       
[15] 1234
[16] +1234
[17] 1234
[18]       1234
[19] 1234
[20] 0000001234
[21] 3.141593
[22]             3.141593
[23] 0000000000003.141593
[24] 3.141593            
[25] In making a living
[26] In
[27]                  In
[28] In                 
[29]              In mak
[30] 3.14159265
[31]                 3.14
[32] 3.1416              
[33]               3.1416
[34]            3.14e+000
You can neatly format your application’s output by studying the preceding example and selecting those combinations that apply to your program’s data types.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net