Input and Output: I/O Finish reading chapters 1 and 2 of the text Data into the Memory e.g. from the keyboard (via scanf) assignment statement ... Output: Data out of the Memory e.g. printing displaying on a monitor Examples: printf(“Enter the distance in miles”); scanf(“%lf”, &miles); printf(“This is %f kms\n”,1.609*miles); output input 142 D -1
Display Input and Output Use printf and scanf printf(“control string”,list of expressions); output format what to output input format what to input scanf(“control string”,&variable list); do not forget for printf placeholders (%d,...) int i; double pi; i=2; pi = 3.14; printf(“%d times %f is %f\n”,i,pi,i*pi); newline must match in number, order and type 2 times 3.140000 is 6.280000 142 D -2
Formatting Output to control the output format in printf double val; printf(“%4.2f”,val); to control the output format in printf 3.14 4 characters (2 after the decimal point) _ can have scientific or decimal notation e (or E) f 4.28e+01 42.8 _ other placeholders %i or %d for an int %c for a char check manual or text 142 D -3
printf(“pay, hours, and age? ”); scanf(“%lf%lf%i”,&pay,&hours,&age); for scanf double pay, hours; int age; printf(“pay, hours, and age? ”); scanf(“%lf%lf%i”,&pay,&hours,&age); placeholders must match in number, order and type lf for a double i for an int Notes When reading numbers (e.g double, int…), scanf skips spaces, tabs and newlines: '\t','\n' user can type spaces, tabs, newlines between numbers But NOT skipped when reading char scanf(“%c”,&letter); Try it out and learn from your mistakes! 142 D -4
Initializing variables giving something a value for the first time double radius; printf(“Radius of the circle? ”); scanf(“%lf”,&radius); We have seen: or double radius; radius = 2.14; or double radius = 2.14; Potential source of errors: forgetting to initialize a variable the value of the variable is random double pay, hours; pay = 26.5 * hours; the result is unpredictable! 142 D-5
Errors when programming C O M P I L E R L I N K E R executable program 010 110 .obj file .c file your program library (ANSI) header (stdio.h) debugger syntax errors: e.g. omitting ; check warnings e.g. variables not initialized link errors: I can’t find the functions you want! e.g. using printf without #include <stdio.h> run time errors: error, e.g. _ bad input (divide by 0, forgot & in scanf) _ bad program logic 142D-6
Programming Style A program is a document: The compiler reads some of it (comments are ignored) Some people have to read ALL of it (update, corrections…) A program MUST be easy to read by another programmer Use comments, spacing, indentation, meaningful names 142D-7
Identifiers Identifiers: names used throughout the program for variables and functions (see later, like printf, scanf…) Reminder: _ contains letters, underscores, numbers _ do not begin with a number or underscore _ not a key word, e.g. double _case sensitive (var and Var are different variables) _ all capital lettered names are for #define constants (see next slide) _ use meaningful names that say what the variable is for. NOT m but miles 142D-8
#define allows you to define constants. Think of it as a text replacement command (but it can do more). #define PI 3.14159 no ; no = everywhere we write PI, the preprocessor will write 3.14159. Then, the file is sent to the compiler. Style convention: use only uppercase letters for these constants. 142D-9
Other examples #define LENGTH 100 #define WIDTH 150 #define HALF_PERIMETER (WIDTH+LENGTH) parentheses are important (without, can lead to computational errors) Centralize changes No magic numbers avoid accidental assignments to constants PI=3.141492; /* Error */ 142D-10
Clarity Avoid obscure statements: NO: x = (y=x) + 1; YES: y = x; x = x + 1; Be clever for a GOOD reason and say why in a comment int i; i = 1; … /* toggle the value of i between 0 and 1*/ i = (i+1)%2; 142D-11