Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECE.2160 ECE Application Programming

Similar presentations


Presentation on theme: "EECE.2160 ECE Application Programming"— Presentation transcript:

1 EECE.2160 ECE Application Programming
Instructors: Dr. Michael Geiger & Dr. Lin Li Spring 2019 Lecture 5: Basic variable output with printf() Basic variable input with scanf()

2 ECE Application Programming: Lecture 5
Lecture outline Announcements/reminders Textbook exercises due 3 days after each lecture Program 2 due Monday, 2/11 Today’s lecture Program 2 overview Review: operators, printf() basics printf() examples scanf() 4/23/2019 ECE Application Programming: Lecture 5

3 ECE Application Programming: Lecture 5
Program 2 overview Learn basic I/O Basic circuit analysis All formulas on last page of figures document zyBooks-specific note Must write all input for program ahead of time Visual Studio-specific note To avoid scanf() warnings, include following line before #include <stdio.h>: #define _CRT_SECURE_NO_WARNINGS 4/23/2019 ECE Application Programming: Lecture 5

4 Review: Arithmetic Operations
Operator Addition + Subtraction - Multiplication * Division / Modulus Division (Remainder) % 4/23/2019 ECE Application Programming: Lecture 5

5 Review: printf() basics
To print variables (or constants), insert %<type> in your printf() format string %c: single character %d or %i: signed decimal integer %f: float; %lf: double Prints 6 digits after decimal point by default To control # digits, use precision "%.4lf" prints with 4 digits (4th digit rounds) "%.0lf" prints with 0 digits (round to nearest integer) Each %<type> must correspond to a variable or constant that follows printf("a=%.3f, b=%.2f", a, b); 4/23/2019 ECE Application Programming: Lecture 6

6 ECE Application Programming: Lecture 5
printf() example float a=67.49,b= ; printf("hello %f there %f\n",a,b); printf("%f%f%f%f\n",a,a,b,b); printf("a=%.2f, b=%.1f",a,b); printf("Cool huh?\n"); Printed: hello there a=67.49, b=10.0Cool huh? 4/23/2019 ECE Application Programming: Lecture 5

7 ECE Application Programming: Lecture 5
Example: printf() Show the output from each programs(assume #include <stdio.h> for all) void main() { int i=2, j=3, k, m; k = j * i; m = i + j; printf("%d %d %d %d\n", i, j, k, m); } double f, g; f = 1.0 / 4.0; g = f * 20; printf("f = %lf,\ng = %.2lf\n", f, g); int a = 5, b = 2; printf("Output%doesn't%dmake%dsense", a, b, a + b); 4/23/2019 ECE Application Programming: Lecture 5

8 ECE Application Programming: Lecture 5
Example solution void main() { int i=2, j=3, k, m; k = j * i; k = 2 * 3 = 6 m = i + j; m = = 5 printf("%d %d %d %d\n", i, j, k, m); } Output: 4/23/2019 ECE Application Programming: Lecture 5

9 Example solution (cont.)
void main() { double f, g; f = 1.0 / 4.0; f = 0.25 g = f * 20; g = 0.25 * 20 = 5 printf("f = %lf,\ng = %.2lf\n", f, g); } Output: f = , g = 5.00 (remember, 6 places after decimal point printed by default with floating-point data) 4/23/2019 ECE Application Programming: Lecture 5

10 Example solution (cont.)
void main() { int a = 5, b = 2; printf("Output%doesn't%dmake%dsense", a, b, a + b); } Output: Output5oesn't2make7sense (Every %d gets replaced with a number, which is underlined above to show what happens—in practice, the console isn’t going to underline your output!) 4/23/2019 ECE Application Programming: Lecture 5

11 ECE 160 - Introduction to Computer Engineering I
02/09/2005 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 4/23/2019 ECE Application Programming: Lecture 6 (c) 2005, P. H. Viall

12 ECE Application Programming: Lecture 6
scanf() and scanf_s() Visual Studio users will see an error message when using scanf() Function is technically not secure (not that it matters for our purposes) Suggests use of scanf_s() Windows-specific “secure” scan function Preferred method of removing warnings: #define _CRT_SECURE_NO_WARNINGS That line must come before #include <stdio.h> 4/23/2019 ECE Application Programming: Lecture 6

13 ECE 160 - Introduction to Computer Engineering I
02/09/2005 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 4/23/2019 ECE Application Programming: Lecture 6 (c) 2005, P. H. Viall

14 ECE 160 - Introduction to Computer Engineering I
02/09/2005 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 4/23/2019 ECE Application Programming: Lecture 6 (c) 2005, P. H. Viall

15 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) 4/23/2019 ECE Application Programming: Lecture 6

16 ECE Application Programming: Lecture 6
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:  x = 3, y = 7, return value = 2 Input:  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: int numRead; // # input values read numRead = scanf("%d%d", &x, &y); 4/23/2019 ECE Application Programming: Lecture 6

17 ECE Application Programming: Lecture 6
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: 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) 4/23/2019 ECE Application Programming: Lecture 6

18 ECE Application Programming: Lecture 6
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) 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' 4/23/2019 ECE Application Programming: Lecture 6

19 Using scanf() and printf() together
ECE Introduction to Computer Engineering I 02/09/2005 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); } 4/23/2019 ECE Application Programming: Lecture 6 (c) 2005, P. H. Viall

20 scanf() function - Payroll Ver 2
ECE Introduction to Computer Engineering I 02/09/2005 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); } 4/23/2019 ECE Application Programming: Lecture 6 (c) 2005, P. H. Viall

21 ECE Application Programming: Lecture 5
Final notes Next time Finish scanf() introduction PE1: Flowcharts and debugging Reminders: Textbook exercises due 3 days after each lecture Program 2 due Monday, 2/11 4/23/2019 ECE Application Programming: Lecture 5


Download ppt "EECE.2160 ECE Application Programming"

Similar presentations


Ads by Google