DOC PREVIEW
UW CSE 142 - Study Notes

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

E-1E-1CSE 142Computer Programming IInput and Output (I/O)© 2000 UW CSEE-2OverviewTopicsOutput: printfInput: scanfBasic format codesMore on initializing variablesE-3Writing Useful ProgramsIt’s hard to write useful programs using only variables and assignment statementsEven our Fahrenheit to Celsius program needed more:Needed a way to get data into and out of the programWe’ll learn more about doing this todayLots of terminology and messy details, but worthwhile.E-4CentralProcessingUnitMainMemoryMonitorNetworkDisk (Files)KeyboardmouseWhat’s a Computer?E-5Input: movement of data into memory from outside world (e.g., from keyboard).Changes the value of a variable“read” operationOutput: movement of data from memory to outside world (e.g., to monitor)“write” operationDoes not change value of memoryBasic DefinitionsE-6Text OutputE-2E-7printf("Enter a Fahrenheit temperature: ");scanf("%lf", &fahrenheit);celsius = (fahrenheit - 32.0) * 5.0 / 9.0;printf("That equals %f degrees Celsius.",celsius);I/O Statements from a Familiar ProgramE-8The functions printf and scanf provide basic display I/O services.printf("control string", list of expressions) ;scanf("control string", list of &variables) ;Control string gives the format of output or input.Expressions are what to output.Variables are where to store the input.‘&’ is magic (that is REQUIRED for scanf!)Display Input and OutputE-9int numPushups;numPushups = 5 ;printf("Hello. Do %d pushups. \n", numPushups);output: Hello. Do 5 pushups.%d is a placeholder (“conversion character”) for an int value.\n is an escape sequence for “newline” character.printf( ): Display OutputE-10int numPushups;numPushups= 5 ;printf("Hello.");printf(" Do %d pushups. \n", numPushups);printf("Do them now. \n");output: Hello. Do 5 pushups.Do them now.What Does the ‘\n’ Do?E-11printf("control string", list of expressions) ;printf might have more than one expression in its list:printf("%d times %f is %f. \n",multiplier , pi , (double) multiplier * pi);Getting a Little FancierE-12% placeholders in format string match expressions in output list in number, order, and type.int multiplier;double pi;pi = 3.14;multiplier = 2;printf(" %d times %f is %f. \n",multiplier , pi , (double) multiplier * pi );Output: 2 times 3.14000 is 6.28000.Multiple Output ExpressionsE-3E-13Advanced Output FormattingThis is only the beginning! A few of many other things you can do:Control number of decimals3.1 vs 3.100000Exponential (scientific) or decimal notation3.1 vs 3.1E0 Control total width (including spaces)_______3.1 vs __3.1How? Look in textbook or a reference manual, or online help!E-14Output Format Examples%10.2f _ _ _ _ 1 2 3 . 5 5 double%10.4f _ _ 1 2 3 . 5 5 0 0%.2f 1 2 3 . 5 5 %10d _ _ _ _ _ _ _ 4 7 5 int%-10d 4 7 5 _ _ _ _ _ _ _%10c _ _ _ _ _ _ _ _ _ a charE-15scanf ( "control string", &input list ) ;int numPushups ;printf ( "Hello. Do how many pushups? " ) ;scanf ( " %d " , &numPushups) ;printf ( "Do %d pushups.\n", numPushups) ;output: Hello. Do how many pushups? 5Do 5 pushups.input list variables MUST be preceded by an &.input list variables MUST be preceded by an &.scanf( ): Read InputE-16If You Forget the ‘&’The program will compile, but when you execute...E-17space (’ ’), tab (’\t’), newline (’\n') are “whitespace”Whitespace is skipped by scanf for int ("%d"), and double ("%lf")This means the user can type spaces before a number and they are ignoredNot skipped for char input "%c"each character typed, including spaces, is usedWhitespaceE-18Basic rule: % placeholders in the format must match variables in the input listMUST! match one-for-one in number, order,and type.int studentID ;double grade ;scanf (" %d %lf", &studentID , &grade ) ;Multiple InputsE-4E-19Type scanf() printf()char %c %cint %d %d %i also worksdouble %lf %f (long) floatWhat happens if types don’t match?printf -- garbled outputscanf -- unpredictable errorsand don’t forget the & !Format Items SummaryE-20Output: printf("control string", output list);output list – expressions; values to be printedcontrol string – types and desired formatfor now, NO “&”, ever!Input: scanf("control string", &input list);input list – variables; values to be readcontrol string – types and expected formatcan be a way of initializing variablesfor now, YES “&”, always!Both: %x’s, I/O list match in number, order, typeprintf/scanf SummaryE-21Input is the movement of data into memoryIn C, we use scanf for input from the keyboardOutput is the movement of data from memoryIn C, use printf for output to the screenKnow the basic printf/scanf rules, and know them wellBe aware that advanced formatting options exist and can be looked up when neededI/O SummaryE-22Bonus Topic: More on Initializing VariablesReview: Initialization means giving something a value for the first time.Potential ways to initialize:Assignment statementscanfYet another way: initializer with declarationE-23Initializers are part of the declaration;they are not assignment statements (despite the = sign).Declarations without initializersint product, i;product = 40;i = 5;Declarations with initializersint product = 40, i =5;i = 6; Initializing when DeclaringE-24Initialization Quizint main (void) { /*line 1*/int a, b, c, d=10; /*line 2*/b=5; /*line 3*/d=6; /*line 4*/scanf("%d %d", &b, &c); /*line 5*/return 0; /*line 6*/}Q: Where is each of a, b, c, and d initialized?E-5E-25Next TimeWe’ll learn about a powerful new type of statement, the conditional or “if” statementPlease join me


View Full Document

UW CSE 142 - Study Notes

Download Study Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?