Printf() 3+4 Scanf()

Printf() 3+4 Scanf()

ECE15: Introduction to Computer Using the C Language 2: Variables and Data Types A. Orlitsky and A. Vardy, based in part on slides by S. Arzi, G. Ruckenstein, E. Avior, S. Asmir, and M. Elad A. Orlitsky and A. Vardy, based in part on slides by & much more printf() 3+4 scanf() Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 2 Outline ❖ Writing ❖ Reading ❖ Variables and data types ‣ Text ‣ Integers ‣ Reals ‣ Characters ‣ Strings ❖ Rudimentary file input/output Lecture 5 ECE15: Introduction to Computer Programming Using the C Language 3 Writing (Printing) ❖ C printing function printf() “print formatted” ❖ Name shared by several programming languages ❖ Prints ‣ Text (“Hello world”) ‣ Integers ‣ Reals ‣ Characters ‣ Strings Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 4 Printing Text printf(“Hello world!”); Hello world!> printf(“Hello world!\n”); Hello world! > printf(“Hello\n world!\n”); Hello world! > printf(“Hello\nworld!\n”); Hello world! > Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 5 Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 6 Integer Constants ❖ Used to print, write in program, type ‣ 7 ‣ -13 ‣ 1234 (not 1,234) Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 7 Printing Integers ❖ Format using %d (decimal) or %i (integer) printf("%d", -13); Same as printf("-13"); -13 printf("my %i cents", 2); my 2 cents Format statement Regular text gets printed normally. printf("%d", 2*3+4); 10 More math later %d, %i, etc. don’t get printed. They printf("%d+%d=%d", 1, 2, 1+3); 1+2=4 determine how arguments after ❖ %3d - use at least 3 locations left adjust “...” get printed. printf("#%d#%3d#%3d#",1, 2, 3456); #1# 2#3456# printf("#%d#%3d#%-3d#",1, 2, 3); #1# 2#3 # printf("#%5.3d#%-5.3d#", 1, 2); # 001#002 # Precede w. 0’s Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 8 Formatting errors ❖ %d and %i indicate how subsequent arguments get printed ❖ # of %d %i should match # subsequent arguments ❖ What if these numbers don’t match? printf("%d", 1, 2); Compilation warning 1 printf("%d %d", 1); Compilation warning 1 73858 mismatch.c Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 9 Variables ❖ So far, everything fixed and rigid, nothing changes ‣program always prints same thing (“Hello, world”, -13) ❖ Need something more flexible, dynamic ‣ Print different values, read user input ❖ Variables ‣ Hold information ‣ Take user input ‣ Can change with time Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 10 Data Types ❖ All constants and variables have a type ‣ Integer, real number, character, string ❖ Type determines ‣ # memory bytes allocated ‣ Interpretation / meaning of the bits stored in these bytes ‣ Operations allowed ❖ Variables must be declared ‣ type, name, optional initial value int num,sum; double weight = 0.0; char digit = '4'; ‣ Instructs compiler to Shortly Now ๏ Allocate right # memory cells ๏ Interpret content of these cells according to type ๏ Associate variable name with these cells Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 11 int ❖ Basic integer type (more types later) ❖ Declaration int year; int temperature, humidity; ❖ Uninitialized (contains previously stored value) ❖ Initialization: Hint ‣ At declaration = does not mean equal. int a=0; int a=0, b=1; a = 5 means store 5 in the variable called a. ‣ Assignment int a; int a, b; int a, b; Hint a=0; a=0; a=b=0; Initialize or assign b=1; value to a variable ❖ Printing: same as constants before using its contents. printf("%d", year); Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 12 Quiz ❖ What’s printed? #include <stdio.h> quiz.c int main() { int a=0, b, c, d, e, f; b=1; c=d=2; printf("a=%d\n", a); a=0 printf("b=%d\n", b); b=1 printf("c=%d\n", c); c=2 printf("d=%d\n", d); d=2 printf("e=%d\n", e); e=589 Or whatever stored before printf("f=%d\n", f); f=-2381039 Likewise return 0; } Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 13 Reading Integers ❖ scanf() with %d (or %i) #include <stdio.h> scanf1.c int main() { int myvar; printf("Type an integer: "); scanf("%d", &myvar); printf("It is: %d\n", myvar); return 0; } Always (for now..) use scanf with & - stores value in that location Think of &myvar as the address of myvar More later Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 14 Non-integer input %d first skips white space Then stops reading when encounters non-integer #include <stdio.h> scanf1.c int main() { int value; printf("Type an integer: "); scanf("%d", &value); Ignored printf(“You typed %d\n", value); } return 0; 15 You typed 15 15 You typed 15 1b 1.5 1,234 You typed 1 rest left for future scanf’s b < .5 You typed -183793 whatever stored before Lecture 2 ECE15: Introduction to Computer Programming Using the C Language Reading More Values #include <stdio.h> scanf2.c int main() { int value1, value2; printf("Now gimme two: "); scanf("%d%d", &value1, &value2); printf("Their sum: %d\n", value1+value2); return 0; } ❖ Spaces between %d`s in format string don’t matter same scanf("%d %d", &value1, &value2); Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 16 Maximum Field Size Integer between % and d specifies maximum # digits read scanf("%2d", &a); 1 a=1 123 a=12 scanf("%1d%d", &a, &b); 123 a=1 b=23 same scanf("%1d", &a); 123 a=1 b=23 scanf("%d", &b); Lecture 2 ECE15: Introduction to Computer Programming Using the C Language Field Separators ❖ Text after %d is expected and read after integer is read scanf("%d,%d", &a, &b); 1,2 a=1 b=2 Remember mainly this scanf("%dxy%d", &a, &b); 1xy2 a=1 b=2 part! ❖ ALLOWED modifications Keep rest in ‣ Add space after separator mind, but scanf("%d,%d", &a, &b); 1, 2 a=1 b=2 less likely to scanf("%dxy%d", &a, &b); 1xy 2 a=1 b=2 be in exams. ‣ Change or remove existing spaces in and around separator scanf("%d x y %d", &a, &b); 1 x y 2 a=1 b=2 ❖ DISALLOWED 1 xy2 a=1 b=2 separator.c ‣ Add space before separator scanf("%d,%d", &a, &b); 1 ,2 a=1 b=#&<) scanf("%dxy%d", &a, &b); 1 xy2 a=1 b=<?)! ‣ Change or remove (non-space) separator symbols scanf("%d,%d", &a, &b); 1.2 a=1 b=!?^) 1 2 a=1 b=*)%- Lecturescanf( 2 "%dxy%d" ECE15: Introduction, &a, to Computer &b); Programming 1x2Using the C Languagea=1 b=<?)! 18 How Much Time Left ❖ Homework is due tonight at 23:59 ❖ Ask user for current time h:m, output time left Enter current time (hh:mm): 17:15 6 hours and 44 minutes left 4:5 19 hours and 54 minutes left #include <stdio.h> time.c int main() { int hour, minute; printf("Enter current time (hh:mm): "); scanf("%d:%d", &hour, &minute); printf("%d hours and %d minutes left\n", 23-hour, 59-minute); return 0; } Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 19 How Many Values scanf Read? ❖ Returns # of successfully read variables #include <stdio.h> scanf_num.c int main() { int numRead, firstVal, secondVal; printf("Two integers please: "); numRead = scanf("%d%d", &firstVal, &secondVal); printf("Read %d ints, first was %d, second %d\n ", numRead, firstVal, secondVal); return 0; } Try entering 5a Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 20 Using # Values scanf Read ❖ Calculate average of up to 4 integers #include <stdio.h> average.c int main() { int a1=0, a2=0, a3=0, a4=0; int number; printf("Enter up to 4 integers followed by *: "); number = scanf("%d%d%d%d", &a1,&a2,&a3,&a4); printf("Sum=%d, Number=%d\n",a1+a2+a3+a4,number); printf("APPROXIMATE average is %d\n", (a1+a2+a3+a4)/number); return 0; } Approximate because integer!! ❖ Later - better ways of doing that Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 21 Can we Print Everything? Means How to print? Like so ” end format ” \” \n newline \ \\ %d decimal % %% printf(“\””); ” printf(“\\\\”); \\ printf(“\\n”); \n printf(“%%d”); %d Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 22 ❖ Rest of the material about integers is important, and you should know it ❖ Yet a bit harder to memorize, hence less likely to appear in in-class midterm and exam Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 23 Representation of Integers ❖ Computers represent everything in binary: 0,1 ❖ Nonnegative integers: standard binary representation ‣ Reserve leftmost bit for sign ‣ For 3 bits: 0 → 000, 1 → 001, 2 → 010, 3 → 011 ‣ Start with 0 Though not on exam, ‣ With n bits, represent 0 to 2n-1-1 you should know this! ❖ Negative integers: 2’s complement, -x represented as 2n-x ‣ For 3 bits: -1 → 111, -2 → 110, -3 → 101, -4 → 100 ‣ Start with 1 ‣ With n bits, represent -1 to -2n-1 Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 24 Size of int ❖ Standards don’t determine size of int byte = 8 bits ❖ Most compilers allocate 4 bytes (32 bits) ❖ Range: -231,…,-1,0,1,…,231-1 31 ❖ 2 = 2,147,483,648 ~ 2 Billion 10 1,024 2 ~thousand #include <stdio.h> int main() { 220 1,048,567 ~million int x=2147483647; 230 1,073,741,824 ~billion printf("x=%d\n", x); printf("x+1=%d\n", x+1); return 0; > a.out } largest_int.c x=2147483647 x+1=-2147483648 Often not enough ❖ > ❖ How do we know # bytes? Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 25 sizeof ❖ sizeof(object) returns # bytes used to store object ❖ object: ‣ Type name: int ‣ Variable name: x ‣ Expression: 3*x+5 printf("Number of bytes:\n");

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    53 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us