Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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”

2 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

3 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

4 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 intdoublechar scanf()%d%lf%c printf()%d%f%c

5 printf() and scanf() in detail 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); Non-printing character Display a prompt scanf does the actual reading of the input These match in number and type

6 Ints and Doubles and Chars … 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 */ * 7 2 / 7 2 % * / 6.2

7 … Oh My! Dilemma: –What if you had an expression made from variables of differing types? * *

8 A Golden Rule One of the many golden rules to learn in CSE 142: –When evaluating expressions, data is never lost! * * An int and a double or Which of the above is more precise?

9 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 What is the value of the following two expressions? 2 / / 7.0 The moral of the story: –Be aware of your types!

10 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

11 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 casts are always done first!

12 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!”

13 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


Download ppt "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."

Similar presentations


Ads by Google