1 / 18

16.216 ECE Application Programming

16.216 ECE Application Programming. Instructor: Dr. Michael Geiger Fall 2011 Lecture 4: Variables printf() introduction. Lecture outline. Announcements/reminders Course home page: http://mgeiger.eng.uml.edu/16216/sp12/ Discussion group on piazza.com

ivor-cross
Download Presentation

16.216 ECE Application Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 16.216ECE Application Programming Instructor: Dr. Michael Geiger Fall 2011 Lecture 4: Variables printf() introduction

  2. Lecture outline • Announcements/reminders • Course home page: http://mgeiger.eng.uml.edu/16216/sp12/ • Discussion group on piazza.com • Search for “16.216”; remember, use Spring (not Winter) 2012 • Assignment 1 due 11:59 PM today • Source file name matters (prog1_simple.c)! • No .cpp files • No zipped archives of entire project • Submit only .c file • Assignment 2 posted, due 2/6 • Review: data representation • Number system basics • Constants • Types • Variables • Today • More on variables • Basic I/O ECE Application Programming: Lecture 4

  3. Review: Number system basics • Base conversions • Use power rule to go from binary/hex to decimal • Use repeated division (or practice) to go from decimal to binary/hex • Representing data in C • Four basic data types • int, float, double, char • Constants • Discussed viable ranges for all types • #define to give symbolic name to constant ECE Application Programming: Lecture 4

  4. Review: variables • Four basic data types • int, float, double, char • Variables • Have name, type, value, memory location • Variable declarations: examples • int x; • float a, b; • double m = 2.35; ECE Application Programming: Lecture 5

  5. Variables - declaring main(){ float hours, payrate; float grosspay; int j; var name memory loc hours ? 4278 payrate ? 427C 4280 grosspay ? j ? 4284 ECE Application Programming: Lecture 4

  6. Variables - assigning varname = expression; Declared variable single variable on left side of = expression any legal expression • Expression can be constant, variable, function call, arithmetic operation, etc. • Variable type (int, float, etc) and expression result type should match • If not, funny things can happen ... ECE Application Programming: Lecture 4

  7. Variables (cont.) main(){ float hours, payrate; float grosspay; int j; hours = 40.0; var name memory loc hours 40.0 4278 payrate ? 427C 4280 grosspay ? j ? 4284 ECE Application Programming: Lecture 4

  8. Variables (cont.) main(){ float hours, payrate; float grosspay; int j; hours = 40.0; payrate = 20.00; var name memory loc hours 40.0 4278 payrate 20.0 427C 4280 grosspay ? j ? 4284 ECE Application Programming: Lecture 4

  9. Variables (cont.) main(){ float hours, payrate; float grosspay; int j; hours = 40.0; payrate = 20.00; grosspay = hours * payrate var name memory loc hours 40.0 4278 payrate 20.0 427C 4280 grosspay 800.00 j ? 4284 note: referencing a variable only "reads" it (non-destructive). Assigning to a variable overwrites whatever was there (destructive). ECE Application Programming: Lecture 4

  10. Variables (cont.) main(){ float hours, payrate; float grosspay; int j; hours = 40.0; payrate = 20.00; grosspay = hours * payrate j = 5; var name memory loc hours 40.0 4278 payrate 20.0 427C 4280 grosspay 800.00 j 5 4284 note: referencing a variable only "reads" it (non-destructive). Assigning to a variable overwrites whatever was there (destructive). ECE Application Programming: Lecture 4

  11. Variables (cont.) main(){ float hours, payrate; float grosspay; int j; hours = 40.0; payrate = 20.00; grosspay = hours * payrate j = 5; j = j + 1; var name memory loc hours 40.0 4278 payrate 20.0 427C 4280 grosspay 800.00 j 5 6 4284 note: referencing a variable only "reads" it (non-destructive). Assigning to a variable overwrites whatever was there (destructive). ECE Application Programming: Lecture 4

  12. Example 1: Variables • What values do w, x, y, and z have at the end of this program? int main() { int w = 5; float x; double y; char z = ‘a’; x = 8.579; y = -0.2; w = x; y = y + 3; z = w – 5; return 0; } ECE Application Programming: Lecture 4

  13. Example 1 solution int main() { int w = 5; float x; double y; char z = ‘a’; x = 8.579; y = -0.2; w = x; y = y + 3; z = w – 5; return 0; } w = 5 z = ‘a’ (ASCII value 97) x = 8.579 y = -0.2 w = 8 (value is truncated) y = (-0.2) + 3 = 2.8 z = 8 – 5 = 3 ECE Application Programming: Lecture 4

  14. I/O basics • Need ability to • Print variables (or results calculated using them) • Read values from input • Output: printf() • Already seen basics • Input: scanf() ECE Application Programming: Lecture 4

  15. printf() formatting • To print variables (or constants), insert %<type> in your 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 • Prints 6 digits after decimal point by default • %s: string ECE Application Programming: Lecture 2

  16. examples - printf() float a=67.49,b=9.999925;printf("hello %f there %f\n",a,b);printf("%f%f%f%f\n",a,a,b,b);printf("a=%f, b=%f",a,b);printf("Cool huh?\n"); Printed: hello 67.490000 there 9.99992567.49900067.4990009.9999259.999925a=67.490000, b=9.999925Cool huh?

  17. printf() example float a=67.49,b=9.999925;printf("hello %f there %f\n",a,b);printf("%f%f%f%f\n",a,a,b,b);printf("a=%f, b=%f",a,b);printf("Cool huh?\n"); Printed: hello 67.490000 there 9.99992567.49000067.4900009.9999259.999925a=67.490000, b=9.999925Cool huh? ECE Application Programming: Lecture 5

  18. Next time • printf() and scanf()—more details ECE Application Programming: Lecture 4

More Related