Eduard Sjoukes
/
IntegerFormatSpecifiers
The use of integer format specifiers
main.cpp@0:12c7cd918566, 2010-12-05 (annotated)
- Committer:
- Eduard
- Date:
- Sun Dec 05 19:44:09 2010 +0000
- Revision:
- 0:12c7cd918566
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Eduard | 0:12c7cd918566 | 1 | /************************************************************************* |
Eduard | 0:12c7cd918566 | 2 | * Test of "format specifiers" and display those on a 20x4 Optrex LCD |
Eduard | 0:12c7cd918566 | 3 | * This LCD needs a different timing than a normal LCD, see the |
Eduard | 0:12c7cd918566 | 4 | * OptrexLCD program for more information. |
Eduard | 0:12c7cd918566 | 5 | * Probally this program will work also with a "normal" 20x4 LCD |
Eduard | 0:12c7cd918566 | 6 | * if not replace the OptrexLCD library with the original TextLCD library. |
Eduard | 0:12c7cd918566 | 7 | ************************************************************************** |
Eduard | 0:12c7cd918566 | 8 | On www.mekong.net/tech/printf.htmt I found: |
Eduard | 0:12c7cd918566 | 9 | << start |
Eduard | 0:12c7cd918566 | 10 | Format Specifiers for C |
Eduard | 0:12c7cd918566 | 11 | Back in the day, I used to occasionally trot out my faithful old copy of Borland C++ Version 5.02, and throw |
Eduard | 0:12c7cd918566 | 12 | together a kludgy little program to cope with some obscure problem. However, I never learned C well, and I don't |
Eduard | 0:12c7cd918566 | 13 | use it often enough to remember even the most basic stuff... such as how to tell the "printf" function what type |
Eduard | 0:12c7cd918566 | 14 | of expression I want to display. I finally got tired of looking through the help to try to find the complete list, |
Eduard | 0:12c7cd918566 | 15 | so I just threw together the following list, pulled from Tony Zhang's fine book Teach Yourself C in 24 Hours. (OK, |
Eduard | 0:12c7cd918566 | 16 | there is no way in hell anyone learns C in 24 hours, but still... it's a good book.) And here is the list of |
Eduard | 0:12c7cd918566 | 17 | available format specifiers: |
Eduard | 0:12c7cd918566 | 18 | %c The character format specifier. |
Eduard | 0:12c7cd918566 | 19 | %d The integer format specifier. |
Eduard | 0:12c7cd918566 | 20 | %i The integer format specifier (same as %d). |
Eduard | 0:12c7cd918566 | 21 | %e The scientific notation format specifier. |
Eduard | 0:12c7cd918566 | 22 | %E The scientific notation format specifier. |
Eduard | 0:12c7cd918566 | 23 | %f The floatingpoint format specifier. |
Eduard | 0:12c7cd918566 | 24 | %g Uses %f or %e, whichever result is shorter. |
Eduard | 0:12c7cd918566 | 25 | %G Uses %f or %E, whichever result is shorter. |
Eduard | 0:12c7cd918566 | 26 | %o The unsigned octal format specifier. |
Eduard | 0:12c7cd918566 | 27 | %s The string format specifier. |
Eduard | 0:12c7cd918566 | 28 | %u The unsigned integer format specifier. |
Eduard | 0:12c7cd918566 | 29 | %x The unsigned hexadecimal format specifier. |
Eduard | 0:12c7cd918566 | 30 | %X The unsigned hexadecimal format specifier. |
Eduard | 0:12c7cd918566 | 31 | %p Displays the corresponding argument that is a pointer. |
Eduard | 0:12c7cd918566 | 32 | %n Records the number of characters written so far. |
Eduard | 0:12c7cd918566 | 33 | %% Outputs a percent sign. |
Eduard | 0:12c7cd918566 | 34 | |
Eduard | 0:12c7cd918566 | 35 | I don't understand what the hell all of these do. I also never understood the difference between %e and %E, or %x |
Eduard | 0:12c7cd918566 | 36 | and %X, until a guy named Cary Wyman was kind enough to email an explanation: while %e and %x will display any |
Eduard | 0:12c7cd918566 | 37 | letters in lowercase, %E and %X will display them in uppercase. Cary also explains that "%d is the normal |
Eduard | 0:12c7cd918566 | 38 | specifier for integers; for input, slightly different formatting rules apply when you use % (eg, you don't write |
Eduard | 0:12c7cd918566 | 39 | 32L to indicate that 32 is a long); for output, %i makes no difference." I still don't quite get that, but at this |
Eduard | 0:12c7cd918566 | 40 | point I'd say chances are just about zero that I'll ever need to. |
Eduard | 0:12c7cd918566 | 41 | >> end |
Eduard | 0:12c7cd918566 | 42 | |
Eduard | 0:12c7cd918566 | 43 | Also I found a list of specifiers in the book of Warwick E.Smith C programming pubished by Elektor p.220. This is almost the same list. |
Eduard | 0:12c7cd918566 | 44 | And in the book C++ in 21 days of Jesse Liberty Published by SAMS on p.577 (dutch edition) I found some other usefull |
Eduard | 0:12c7cd918566 | 45 | information for reducing the output string length. There is also a lot usefull info at http://en.wikipedia.org/wiki/Printf. |
Eduard | 0:12c7cd918566 | 46 | |
Eduard | 0:12c7cd918566 | 47 | I'll try to use al those format specifiers in different programs and show the results on a 20x4 LCD. |
Eduard | 0:12c7cd918566 | 48 | |
Eduard | 0:12c7cd918566 | 49 | */ |
Eduard | 0:12c7cd918566 | 50 | |
Eduard | 0:12c7cd918566 | 51 | #include "mbed.h" |
Eduard | 0:12c7cd918566 | 52 | #include "OptrexLCD.h" |
Eduard | 0:12c7cd918566 | 53 | |
Eduard | 0:12c7cd918566 | 54 | DigitalOut BlinkLed(LED4); |
Eduard | 0:12c7cd918566 | 55 | |
Eduard | 0:12c7cd918566 | 56 | TextLCD lcd(p10, p12, p15, p16, p17, p18, TextLCD::LCD20x4 ); // rs, e, d0-d3 |
Eduard | 0:12c7cd918566 | 57 | |
Eduard | 0:12c7cd918566 | 58 | void WaitRefres(int a) // procedure for showing the result for 3 seconds before getting the next datatypes |
Eduard | 0:12c7cd918566 | 59 | { |
Eduard | 0:12c7cd918566 | 60 | wait(a); |
Eduard | 0:12c7cd918566 | 61 | lcd.cls(); |
Eduard | 0:12c7cd918566 | 62 | BlinkLed = !BlinkLed; |
Eduard | 0:12c7cd918566 | 63 | } |
Eduard | 0:12c7cd918566 | 64 | |
Eduard | 0:12c7cd918566 | 65 | int main() |
Eduard | 0:12c7cd918566 | 66 | { |
Eduard | 0:12c7cd918566 | 67 | lcd.cls(); |
Eduard | 0:12c7cd918566 | 68 | BlinkLed = 1; // show the program is running |
Eduard | 0:12c7cd918566 | 69 | while(1) |
Eduard | 0:12c7cd918566 | 70 | { |
Eduard | 0:12c7cd918566 | 71 | int i = 0; |
Eduard | 0:12c7cd918566 | 72 | lcd.printf(" The use of\n"); |
Eduard | 0:12c7cd918566 | 73 | lcd.printf(" Integer Format\n"); |
Eduard | 0:12c7cd918566 | 74 | lcd.printf(" Specifiers\n"); |
Eduard | 0:12c7cd918566 | 75 | lcd.printf(" @e.sjoukes"); |
Eduard | 0:12c7cd918566 | 76 | WaitRefres(3); |
Eduard | 0:12c7cd918566 | 77 | |
Eduard | 0:12c7cd918566 | 78 | lcd.printf("The use of %%d and %%i"); // with %% its posible to print a single % character |
Eduard | 0:12c7cd918566 | 79 | i = 65; |
Eduard | 0:12c7cd918566 | 80 | lcd.printf(" %%d i= %d\n", i); |
Eduard | 0:12c7cd918566 | 81 | i = -38; |
Eduard | 0:12c7cd918566 | 82 | lcd.printf(" %%i i= %i\n", i); |
Eduard | 0:12c7cd918566 | 83 | i = 15; |
Eduard | 0:12c7cd918566 | 84 | lcd.printf(" %%+d i= %+d", i); // The + sign causes printf to always denote the sign '+' or '-' of a number |
Eduard | 0:12c7cd918566 | 85 | // (the default is to omit the sign for positive numbers). Only applicable to numeric types. |
Eduard | 0:12c7cd918566 | 86 | WaitRefres(5); |
Eduard | 0:12c7cd918566 | 87 | |
Eduard | 0:12c7cd918566 | 88 | lcd.printf("Use of fixed numbers"); |
Eduard | 0:12c7cd918566 | 89 | i = 58; |
Eduard | 0:12c7cd918566 | 90 | lcd.printf("5 nums %%5d: %5d\n", i); // Causes printf to left-pad the output with spaces until |
Eduard | 0:12c7cd918566 | 91 | // the required length of output is attained. |
Eduard | 0:12c7cd918566 | 92 | i = 694; |
Eduard | 0:12c7cd918566 | 93 | lcd.printf("5 nums %%5i: %5i\n", -i); |
Eduard | 0:12c7cd918566 | 94 | i =12345; |
Eduard | 0:12c7cd918566 | 95 | int n = 5; |
Eduard | 0:12c7cd918566 | 96 | lcd.printf("X nums %%*d: %*d\n", n, i); // Causes printf to pad the output until it is n characters wide. |
Eduard | 0:12c7cd918566 | 97 | WaitRefres(5); |
Eduard | 0:12c7cd918566 | 98 | |
Eduard | 0:12c7cd918566 | 99 | lcd.printf("With leading zero's\n"); |
Eduard | 0:12c7cd918566 | 100 | i =65; |
Eduard | 0:12c7cd918566 | 101 | lcd.printf("6nums %%06d: %06d\n", i); // Causes printf to use 0 instead of spaces to left-fill a fixed-length field. |
Eduard | 0:12c7cd918566 | 102 | i = 89; |
Eduard | 0:12c7cd918566 | 103 | lcd.printf("6nums %%+06d: %+06d\n", i); |
Eduard | 0:12c7cd918566 | 104 | i = 578; |
Eduard | 0:12c7cd918566 | 105 | n = 6; |
Eduard | 0:12c7cd918566 | 106 | lcd.printf("X nums %%0*i: %0*i\n", n, -i); |
Eduard | 0:12c7cd918566 | 107 | |
Eduard | 0:12c7cd918566 | 108 | WaitRefres(5); |
Eduard | 0:12c7cd918566 | 109 | } |
Eduard | 0:12c7cd918566 | 110 | } |