<<

9/7/2016

Allowing Input from the Keyboard, Output to the Monitor University of Illinois at Urbana-Champaign Dept. of Electrical and Computer Engineering To control input and output (I/O), we use two functions from the standard library. ECE 120: Introduction to Put this line at the top of your C program: #include

This directive tells the C compiler that your Basic I/O in C program uses the standard C I/O functions.

ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 1 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 2

Write Output Using printf Use to Include Special ASCII Characters

To write text onto the display, use printf. Certain ASCII characters The “f” means “formatted.” ◦ control text appearance, and ◦ When using the function, ◦ are hard to put between quotes. ◦ you must specify the desired format For example between quotation marks. ◦ ASCII’s linefeed (or lf, sometimes called newline) Example: ◦ starts a new line of text. printf ("Here is an example."); To include linefeed, write \n between quotes. The function call above writes the text The backslash indicates a special ASCII between the quotes to the monitor. character. Use \\ for one backslash.

ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 3 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 4 9/7/2016

One Can Include Many Linefeeds Use Format Specifiers to Print Expressions

Example: printf also prints expression values printf("This\ntext\\has\nlines!\n"); For example, specifies what and how to print The call above prints the three lines below printf (": %d %d %d\n", (at the left of the screen). 6 * 7, 17 + 200, 32 & 100); Output: [followed by ASCII linefeed] This text\has Integers: 42 217 32 lines! The expressions to print The next printf also starts on a new line ◦ appear after the format specification, and (because of the linefeed at the end of the format). ◦ are separated by .

ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 5 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 6

Many Format Specifiers are Supported These Tables Suffice for Our Class

Format Specifier Interpretation Format Specifier Interpretation %c int or char as %u unsigned int ASCII character as decimal %d int as decimal %x as lower-case %e double as decimal scientific notation %X integer as upper-case %f double as decimal hexadecimal %% one percent sign See man pages on a lab machine for more.

ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 7 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 8 9/7/2016

Format Specifiers Print Only the Expression Values Pitfall: Passing the Wrong Type of Expression

If you want spacing, include it Be sure that your expressions (and in the format. ordering) match the format. an int Example: Example: a double printf("%d%d%d", 12, -34, 56); printf("%d %f", 10.0, 17); prints may print (output is system dependent) 12-3456 0 0.000000 Except for format specifiers and special ASCII characters like linefeed, characters print A C compiler may be able to warn you exactly as they appear. about this kind of error.

ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 9 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 10

Pitfall: Too Few/Many Expressions Read Input Using scanf

If you pass more expressions than format To read values from the keyboard, use scanf. specifiers, the last expressions are The “f” again means “formatted.” ignored. scanf also takes If you pass fewer expressions than format ◦ a format in quotation marks, and specifiers, printf prints … bits! ◦ a -separated list of variable addresses (In other words, behavior is unspecified.) Example: int A; memory address of variable A Again, a C compiler may be able to warn scanf ("%d", &A); you about this kind of error. reads a decimal integer, converts it to 2’s complement, and stores the bits in A.

ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 11 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 12 9/7/2016

scanf Ignores White Typed by User Other Characters in Format Must be Typed Exactly

Example: int A; If format includes characters int ; ◦ other than format specifiers and white space scanf ("%d%d", &A, &B); ◦ user must type them exactly with no extra The user can separate the two with spaces. Rarely useful. spaces, tabs, and/or linefeeds, such as … Example: intA;intB; 5 42 /* A is 5, B is 42 */ scanf ("%d<>%d", &A, &B); 5 /* two lines -> same result */ Type “5<>42” and A==5, B==42. 42 But type “5 <>42” and A==5, while B is The user must push when done. unchanged (no initializer, so B contains bits).

ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 13 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 14

Conversion Specifiers Similar to printf Conversion Specifiers Similar to printf

Format Specifier Interpretation Format Specifier Interpretation %c store one ASCII %u convert decimal character (as char) integer to %d convert decimal unsigned int integer to int %x or %X convert hexadecimal %f convert decimal real integer to to float unsigned int %lf convert decimal real number to double

ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 15 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 16 9/7/2016

More Pitfalls for scanf than for printf printf Returns the Number of Characters Printed

scanf has the same pitfalls as printf Function calls are expressions. ◦ Be sure to match format specifiers (and Both printf and scanf return int ordering) to variable types. (the calls evaluate to values of type int). ◦ Be sure to match number of specifiers to printf returns the number of characters number of addresses given. printed to the display. And more! Writing a printf followed by a ◦ Don’t forget to write “&” before each ◦ evaluates the expression (calls printf ), variable. (Behavior is again undefined, but ◦ then discards the return value. can be quite difficult to find the bug.) The return value of printf is rarely used.

ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 17 ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 18

scanf Returns the Number of Conversions

scanf returns the number of conversions performed successfully, or -1 for no conversions. The return value is important for checking user input. For example, if (2 != scanf ("%d%d", &A, &B)) { printf ("Bad input!\n"); A = 42; B = 10; /* defaults */ }

ECE 120: Introduction to Computing © 2016 Steven S. Lumetta. All rights reserved. slide 19