130 likes | 151 Views
I/O with scanf()/printf(). I/O stands for “Input/Output” In CSE 142, you will use: scanf() (reading from the keyboard) printf() for output (printing to the screen) Side note: scanf() and printf() are functions , which you’ll learn about later in the course
E N D
I/O with scanf()/printf() • I/O stands for “Input/Output” • In CSE 142, you will use: • scanf() (reading from the keyboard) • printf() for output (printing to the screen) • Side note: scanf() and printf() are functions, which you’ll learn about later in the course • “printf” == “print function” • “scanf” == “scan function”
The format for scanf()/printf() • Both scanf() and printf() follow the same format: they take a literal string describing the format of the input or output, optionally followed by a list of arguments (comma-seperated list of expressions) • The literal string (what’s between the double-quotes “ ”) is read/displayed exactly as it is typed by the programmer
Escape sequences • printf(“Farewell, cruel world!”); • Prints Farewell, cruel world! exactly as typed • “Exactly as typed”? What if I wanted to print • a new line? a tab character? • the value of a variable? • Printing special characters (ie - non-printing characters) such as a newline or a tab require escape sequences - character codes which represent them • Two useful escape sequences: • \n - newline • \t - tab
The placeholders • A placeholder: tells the compiler “I want to {read into/print} a variable, but I’m not sure of its value right now. Save me some space, and I’ll {get/tell you} its value at runtime” • Placeholders look like percent signs followed by a code describing the type of the variable • Placeholders are placed into the literal string (ie - between the “ ”), and must have a variable or expression which matches in type in the argument list
printf() and scanf() in detail Non-printing character int i = 10; char c; double d; printf(“Hello World!\n”); printf(“This is a number: 9\n”); Printf(“This is the name of a variable: d\n”); printf(“Enter a number: ”); scanf(“%lf”, &d); printf(“This is the value of a variable: %f\n”, d); Display a prompt scanf does the actual reading of the input These match in number and type
All variables in C have a type Review: We’ve seen int,double, and char so far What is the type of an expression made from variables of the same type? /* No surprises so far */ 2 + 7 2 - 7 2 * 7 2 / 7 2 % 7 6.03 + 6.2 6.03 - 6.2 6.03 * 6.2 6.03 / 6.2 Ints and Doubles and Chars …
Dilemma: What if you had an expression made from variables of differing types? 2 + 7.4 2 * 7.8 6.03 * 6 6.03 - 6 … Oh My!
A Golden Rule • One of the many golden rules to learn in CSE 142: • When evaluating expressions, data is never lost! 2 + 7.4 2 * 7.4 6.03 * 6 6.03 - 6 9.4 14.8 36.18 .03 9 14 36 0 An int and a double or Which of the above is more precise?
Division: It’s why we care • In other words, any expression involving a double evaluates to a double • Why is the type of an expression such a big deal? • Both of these expressions evaluate to 9 or 9.0 2.0 + 7.0 2 + 7 • What is the value of the following two expressions? 2 / 7 2.0 / 7.0 • The moral of the story: • Be aware of your types!
What’s really happening • When an expression involving mixed types is being evaluated: • The type of the result is the type of the most precise type • The operands are cast (that is, converted) to the resultant type before the expression is evaluated aDouble = 4 * 5.2; The most precise type is a double, so the entire expression evaluates to a double This 4 is cast to a double -- 4.0
Explicitness is a good thing … • In the previous example, aDouble = 4 * 5.2; 4 was implicitly cast to a double • You can also explicitly cast your variables! • An explicit cast has the following form: ( type ) expression; • Examples: (double) anInt; 2 + (int) 4.5; (double) 6 * myInt; • Like implicit casts, explicit castsare always done first!
Why explicitness is good • Explicit casts guarentee that the correct operator is being called (double) 2 / 7 • Explicit casts make it clear to readers (ie -- your TA’s!) what you intend • NB: Parentheses matter! (double) (2 / 7) “NB” == “Nota Bene” == Latin for “Note this well” That is, “Remember this!”
Casting Assignments • Casting also occurs during assignment statements if the type of the assigned variable differs from the type of the expression anInt = 4 * 5.2; The most precise type is a double, so the entire expression evaluates to a double But the variable being assigned to is an int! So the value assigned into anInt is an int