260 likes | 342 Views
16.216 ECE Application Programming. Instructor: Dr. Michael Geiger Fall 2013 Lecture 4: Input with scanf() Output formatting. Lecture outline. Announcements/reminders Program 2 due 9/16 Program 1 grading complete; regrades due 9/12 Sign up for the course discussion group Review
E N D
16.216ECE Application Programming Instructor: Dr. Michael Geiger Fall 2013 Lecture 4: Input with scanf() Output formatting
Lecture outline • Announcements/reminders • Program 2 due 9/16 • Program 1 grading complete; regrades due 9/12 • Sign up for the course discussion group • Review • Operators • Basic variable output with printf() • Today’s lecture • Input using scanf() • Output formatting ECE Application Programming: Lecture 4
Review: Arithmetic Operations ECE Application Programming: Lecture 4
Review: printf() basics • To print variables (or constants), insert %<type> in your printf() format string • %c: single character • %d or %i: signed decimal integer • %u: unsigned decimal integer • %x or %X: unsigned hexadecimal integer • %f: float • %lf: double • %s: string • Each %<type> must correspond to a variable or constant that follows • printf("a=%f, b=%f",a,b); ECE Application Programming: Lecture 4
scanf() function • Used to get input from user • Returns number of items successfully assigned • First argument is format specifiers • Essentially same as printf() format string • Every format specifier (%d, %lf, etc.) corresponds to an input value to be read • Format string can contain other characters, which will be ignored if they are present • If they’re not, you have a problem … • Remaining arguments are variable addresses • Use “address of” operator: & • For example, given: int a; The address of a is: &a ECE Application Programming: Lecture 4
scanf() function Documentation info: int scanf(const char *format [,argument] ...) format - is format specifiers similar to printf() specifiers arguments - are ADDRESSES of where to store what the user enters ECE Application Programming: Lecture 4
scanf() function int hours;float rate; scanf("%d %f",&hours,&rate); If user types: 34 5.7 hours ? 1284 rate ? 1288 hours 34 1284 rate 5.7 1288 ECE Application Programming: Lecture 4
scanf() format strings • scanf() will skip space characters for all types but %c • Read input until it finds something that’s not a space, then see if it matches the desired type • If type matches, value will be stored in specified variable • If type doesn’t match, nothing stored; function stops • Space in string only matters if using %c • %c will read any character • Includes spaces, newlines, etc. • Example: given scanf("%d%c", &i, &c); • Input: 3a i = 3, c = 'a' • Input: 3 a i = 3, c = ' ' • Input: 3 a i = 3, c = '\n' (assuming newline directly after 3) ECE Application Programming: Lecture 4
scanf() return value • scanf() returns # of successfully read items • Ex.: given scanf("%d%d", &x, &y); • Input: 3 7 x = 3, y = 7, return value = 2 • Input: 3 7.2 x = 3, y = 7, return value = 2 • Input: 3 .3 x = 3, y = ?, return value = 1 • y is unchanged • Input: x1 7 x = ?, y = ?, return value = 0 • x, y both unchanged • Can assign return value to variable • Example: intnumRead; // # input values read numRead = scanf("%d%d", &x, &y); ECE Application Programming: Lecture 4
Example • Variables: int i; double d; char c; • What values are read for each of the following inputs and scanf() calls? Assume the input is as follows: 34 5.7 • scanf("%d%lf", &i, &d) • scanf("%d %lf", &i, &d) • scanf("%lf%d", &d, &i) • scanf("%d%c", &i, &c) • scanf("%d %c", &i, &c) ECE Application Programming: Lecture 4
Example solution • What values are read for each of the following inputs and scanf() calls? • scanf("%d%lf", &i, &d) i = 34, d = 5.7 • scanf("%d %lf", &i, &d) i = 34, d = 5.7 • scanf("%lf%d", &d, &i) d = 34, i = 5 • scanf("%d%c", &i, &c) i = 34, c = ' ' (space) • scanf("%d %c", &i, &c) i = 34, c = '5' ECE Application Programming: Lecture 4
Using scanf() and printf() together #include <stdio.h> int main(){ int hours; float rate; float grosspay; printf("Enter hours: "); scanf("%d",&hours); printf("Enter pay rate: "); scanf("%f",&rate); grosspay = hours * rate; printf("You earned $%f\n",grosspay);} ECE Application Programming: Lecture 4
scanf() function - Payroll Ver 2 #include <stdio.h> int main(){ double hours; double rate; double grosspay; printf("Enter hours: "); scanf("%lf",&hours); printf("Enter pay rate: "); scanf("%lf",&rate); grosspay = hours * rate; printf("You earned $%lf\n",grosspay);} ECE Application Programming: Lecture 4
Formatted output: field width • Specifying field width (min # characters) (assume n = 12): • printf(“%10d”, n); 12(8 spaces, then number 12) • To left justify value in field, use –flag before width • printf(“%-10d”, n); 12________(Number 12, then 8 spaces) • To force the sign to show, use +flag before width • printf(“%+10d”, n); +12 (7 spaces, a plus sign, then number 12) • To place 0s before the value, use 0flag before width • printf(“%010d”, n); 0000000012 • printf(“%+010d”, n); +000000012 • printf(“%-010d”, n); 12________ • To use a variable to specify field width, use * as the width • printf(“%*d”, n, n); 12 (Field width = 12 10 spaces, then the number 12) • Field width never truncates • printf(“%1d”, n); still prints 12 ECE Application Programming: Lecture 3
Formatted output: precision • For examples, int n = 12; double x = 3.754 • Specifying precision: • # chars after decimal point for FP (%f, %lf) • Rounds last digit • printf(“%.6lf”, x); 3.754000 • printf(“%.1lf”, x); 3.8 • printf(“%.0lf”, x); 4 • Minimum # chars for integer (%d, %i, %u, %x) • Does not truncate; will pad with leading 0s • printf(“%.1d”, n); 12 • printf(“%.3d”, n); 012 • Max # chars for string (%s) • printf(“%.5s”, “one”); one • printf(“%.5s”, “one two”); one t • No effect for char (%c) • As with field width, can use * to specify that field width is a variable ECE Application Programming: Lecture 3
Formatting and scanf() • Formatting specified above used only for printf() • Have very different meaning for scanf() • printf("%3d", x) print x w/field width of 3 • scanf("%3d", &x) read 3 characters into x • If you enter: 12 x = 12 • If you enter: 1234 x = 123 • Bottom line: you probably don’t want to “format” your input! ECE Application Programming: Lecture 3
Note • The next three slides contain full specification for printf() flags, field width, and precision • Read on if you want lots of details; skip these slides if the previous ones were sufficient ECE Application Programming: Lecture 3
printf format specifications General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type This slide adapted from information in MSDN Library ECE Application Programming: Lecture 3
printf format specifications General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the – flag (for left alignment) is specified — until the minimum width is reached. Notes: • If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers). • The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification). ECE Application Programming: Lecture 3 This slide adapted from information in MSDN Library
printf format specifications General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type ECE Application Programming: Lecture 3 This slide adapted from information in MSDN Library
examples - printf() float a=67.49,b=9.999925;printf("Price:█%10f█%10f\n",a,b);printf("%8.2f█%8.4f█%5.1f█%7.5f\n",a,a,b,b);printf("a=%5.5f,█b=%0.2f\n",a,b); Printed: 00000000011111111112222222222333333 print12345678901234567890123456789012345 positionPrice:██67.499000███9.999925 columns███67.49██67.4900██10.0█9.99993 1 to 35a=67.49000,█b=10.00 Note: On last line of output, the actual value was output; the printf routine overrode the specified width. ECE Application Programming: Lecture 3
examples - printf() float a=67.49,b=9.999925;inti=184,j=-51;doublex=123.456, y=-22.33; printf("%7.1lf%5d%8.2f\n",y,i,a);printf("%13.2f%4d%9.2lf\n",b,j,x); //changed Printed: 00000000011111111112222222222333333 print12345678901234567890123456789012345 position -22.3 184 67.49 columns 10.00 -51 -22.33 1 to 35 Notes: d indicates decimal integerf indicates floatlf (that’s lower case L and f) indicates double(for double output, f OK too; not OK for input) ECE Application Programming: Lecture 3
printf() examples printf("vv% dww%-dxx%+dyy%dzz\n",-12,-34,-56,-78); ..../....1..../....2..../....3..../ <= rulervv-12ww-34xx-56yy-78zz printf("vv% dww%-dxx%+dyy%dzz\n",12,34,56,78); ..../....1..../....2..../....3..../ <= rulervv 12ww34xx+56yy78zz printf("vv% 5dww%-5dxx%+5dyy%5dzz\n",-12,-34,-56,-78); ..../....1..../....2..../....3..../ <= rulervv -12ww-34 xx -56yy -78zz printf("vv% 5dww%-5dxx%+5dyy%5dzz\n",12,34,56,78); ..../....1..../....2..../....3..../ <= rulervv 12ww34 xx +56yy 78zz ECE Application Programming: Lecture 3
printf() examples printf("v% 05dw%-05dx%+05dy%05dz\n",-12,-34,-56,-78); ..../....1..../....2..../....3..../ <= rulerv-0012w-34 x-0056y-0078z printf("v% 05dw%-05dx%+05dy%05dz\n",12,34,56,78); ..../....1..../....2..../....3..../ <= rulerv 0012w34 x+0056y00078z printf("v%7.3dw% 7.3dx%-7.3dy%+7.3dz\n",-12,-34,-56,-78); ..../....1..../....2..../....3..../ <= rulerv -012w -034x-056 y -078z print("v%7.3dw% 7.3dx%-7.3dy%+7.3dz\n", 12, 34, 56, 78); ..../....1..../....2..../....3..../ <= rulerv 012w 034x056 y +078z ECE Application Programming: Lecture 3
printf() examples printf("w%7.2fx%7.2fy%7.2fz\n",-1.234,-3.456,-5.6); ..../....1..../....2..../....3..../ <= rulerw -1.23x -3.46y -5.60z printf("w%7.2fx%7.2fy%7.2fz\n", 1.234, 3.456, 5.6); ..../....1..../....2..../....3..../ <= rulerw 1.23w 3.46y 5.60z printf("w%7.2fx%7.2f\n",-1234567.8,1234567.8); ..../....1..../....2..../....3..../ <= rulerw-1234567.8x1234567.8z ECE Application Programming: Lecture 3
Final notes • Next time • PE1: Flowcharts and debugging basics • Reminders: • Sign up for the course discussion group on Piazza! • Program 2 due 9/16 ECE Application Programming: Lecture 4