E. Format Specifier Tricks - University of Virginia
[Pages:3]E. Format Specifier Tricks
Here are a few tips and tricks for format specifiers (placeholders) in printf and scanf statements. This isn't an exhaustive list of the things you can do with format specifiers, but it includes the things you're likely to use most often. For complete information, see the excellent Wikipedia article on "printf format strings".
To start with, here's a list of format specifiers for some common variable types:
Format %d %lf %le %lg
%c %s %p %u %ld %lu %lld %llu %x %X %o
Description Format for printing or reading an integer. Format for printing or reading a double. Print a double in scientific notation. Print a double in either scientific notation or normal notation, whichever is more appropriate. A single character. An array of characters (also called a "character string") A memory address (also called a "pointer"). An unsigned integer. A long integer. An unsigned long integer A long long integer. An unsigned long long integer. A hexadecimal integer with lower-case letters. A hexadecimal integer with upper-case letters. An octal integer.
You can adjust the behavior of these format specifiers by adding modifiers to them. The following tables shows some tricks to help you make your program's output look just the way you want it.
576 practical computing for science and engineering
Generic tricks Example printf("%%") Tricks for Integers Example printf("%20d",1234567890)
printf("%-20d",1234567890)
printf("%8d",1234567890)
printf("%020d",1234567890)
Tricks for doubles Example printf("%20lf",M_PI)
printf("%5lf",M_PI*1e8) printf("%20.10lf",M_PI)
printf("%-20lf",M_PI) printf("%020lf",M_PI)
Result %
Result 1234567890
1234567890 1234567890 00000000001234567890
Result
3.141593
314159265.358979
3.1415926536
3.141593 0000000000003.141593
Description Print a literal % symbol.
Description Print an integer, reserving a 20-digit-wide space for it. If the number isn't this long, add blank spaces on the left-hand side. The same as above, but add blank spaces on the right-hand side if necessary. If the number won't fit in the specified width, use as much space as necessary. Pad the number with zeros on the left (if necessary) to make it 20 digits long.
Description Print a double, reserving enough space for 20 digits. If the number won't fit in the specified width, use as much space as necessary. Reserve enough space for 20 digits (including the decimal point) and print 10 of those digits after the decimal point. Pad with spaces on the left-hand side if necessary. As above, but pad on the right-hand side. Pad the number with zeros on the left (if necessary) to make it 20 digits long.
chapter e. format specifier tricks 577
Tricks for Characters Example printf("%20c",'A')
Result
printf("%-20c",'A')
A
Tricks for Strings Example printf("%20s","Testing")
Result
printf("%-20s","Testing") Testing printf("%4s","Testing") Testing
Description A Print a character, reserv-
ing a 20-character-wide space for it. Fill the extra width with spaces on the left-hand side. As above, but fill on the right-hand side.
Testing
Description Print a character string, reserving a 20-characterwide space for it. Fill the extra width, if any, with spaces on the left-hand side. As above, but fill with spaces on the right-hand side. If the string won't fit in the specified width, use as much space as necessary.
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- format string vulnerability printf user input
- 6 087 lecture 4 january 14 2010 mit opencourseware
- output printf
- description syntax stata
- write output using printf use backslash to include special
- secrets of printf don colton
- cis 190 c c programming university of pennsylvania
- c function copyright © tutorialspoint
- e format specifier tricks university of virginia
- the c printf fact sheet bytellect
Related searches
- value of series e bonds
- types of e business models
- advantages of e learning education
- advantages of e business
- benefits of e crm
- history of e learning
- state of michigan collections e service
- list of e commerce businesses
- business model of e commerce
- types of e commerce models
- advantages of e learning
- examples of e business models