Sams Teach Yourself C++ in 21 Days

(singke) #1
Working with Streams 621

17


%l longinteger
%ld double
%f float

Each of the conversion specifiers can also provide a width statement and a precision
statement, expressed as a float, where the digits to the left of the decimal are used for
the total width, and the digits to the right of the decimal provide the precision for floats.
Thus,%5dis the specifier for a 5-digit-wide integer, and %15.5fis the specifier for a 15-
digit-wide float, of which the final five digits are dedicated to the decimal portion.
Listing 17.15 illustrates various uses of printf().

LISTING17.15 Printing with printf()


0: //17.15 Printing with printf()
1: #include <stdio.h>
2:
3: int main()
4: {
5: printf(“%s”,”hello world\n”);
6:
7: char *phrase = “Hello again!\n”;
8: printf(“%s”,phrase);
9:
10: int x = 5;
11: printf(“%d\n”,x);
12:
13: char *phraseTwo = “Here’s some values: “;
14: char *phraseThree = “ and also these: “;
15: int y = 7, z = 35;
16: long longVar = 98456;
17: float floatVar = 8.8f;
18:
19: printf(“%s %d %d”, phraseTwo, y, z);
20: printf(“%s %ld %f\n”,phraseThree,longVar,floatVar);
21:
22: char *phraseFour = “Formatted: “;
23: printf(“%s %5d %10d %10.5f\n”,phraseFour,y,z,floatVar);
24:
25: return 0;
26: }

TABLE17.2 continued

Free download pdf