Basic Input/Output and Variables Ethan Cerami New York
This Week Quick Review Integer Variables Basic Input: scanf() Float Variables Mathematical Operators
Review: First Program in C /* First Program in C */ #include main() { printf("Hello, World!"); /* Wait for user to Press Enter */ getchar(); } Comments Include Library Files Main Function Pauses Program printf () Statement
printf () Escape Characters \ is an escape character reserved for special characters such as new line and tab. Examples: \n insert a newline \t tab \a bell sound \" double quote other escape characters listed on page 26 of text.
III: Variables Variable: a small piece or “chunk” of data –Could be a number (5, PI), or someone's name or someone's address. –Could be your bank account balance. –Could be a list of the courses you have registered for this semester.
Data Types Every variable must have both a data type and a name. –Data type: defines the kind of data the variable can hold –Can this variable hold numbers? can it hold text? The “Bucket” Analogy
Integer Data Type Integer: data type that can hold only whole numbers, e.g. 5, 76, Cannot hold numbers with decimal points, such as 4.2 or Cannot hold strings, such as “Hello” or “Ethan” Example: int x = 5;
Integer Program (Program 2.0) #include main () { int x, y, z; x = 5; y = 10; z = x + y; printf ("x: %d\n", x); printf ("y: %d\n", y); printf ("z: %d\n", z); /* Wait for user to Press Enter */ getchar(); } Variable Declaration Data TypeVariable Names Assignment Statements
Printing Variables To print integer variables: format specifier: indicates the data type. %d: print integer values you must include both a format specifier and a variable name. Example: printf ("x: %d\n", x); Format Specifier Variable Name
Rules for Naming Variables Variable Names: Can contain letters, digits, or underscores _ Cannot begin with a digit. Examples of Valid Variable names int variable1, variable_2; Examples of Invalid Variable names: int 1variable, variable#1 C is case sensitive, e.g. variable1 VARIABLE1
V. Basic Input: scanf() Input: any user supplied data. –Keyboard input, mouse input. scanf (): read in keyboard input.
scanf() Example (Program 1.2) #include main() { int integer1, integer2, sum; /* declaration */ printf("Enter first integer\n"); /* prompt */ scanf("%d", &integer1); /* read an integer */ printf("Enter second integer\n"); /* prompt */ scanf("%d", &integer2); /* read an integer */ sum = integer1 + integer2; /* assignment of sum */ printf("Sum is %d\n", sum); /* print sum */ /* Wait for user to Any Key */ getch(); return 0; /* indicate that program ended successfully */ } Variable Declaration
Using scanf() scanf ("%d", &integer1); %d indicates integer values (just like printf) Format Specifier Variable Name & Character is required
Other tid-bits getch(); Waits for user to press any key. getch() requires the #include library : Console Input/Output similar to getchar(), but getchar() requires that the user press the ENTER key. return 0; Returns the value 0 to the operating system. Tells the operating system that everything went well. Anything other than 0 indicates an error.
VI. Mathematical Operators Basic Mathematical Operators + -Addition / Subtraction * Multiplication /Integer Division % Modulus Division
Integer Division - The Problem Suppose you have the following code: Using a calculator, the answer is But x can only hold integer values is clearly not an integer value. int x; x = 7 / 4;
Integer Division - The Solution To understand the solution, you need to remember your 3 rd Grade Math (really.) 7/4 = 1 (Integer Division) 7%4 = 3 (Modulus Division) The answer: 1 remainder 3
Integer/Modulus Division (Program 2.1) /* Integer and Modulus Division */ #include main () { int x = 5, y =10; printf ("5/10: %d\n", x/y); printf ("5%10: %d\n", x%y); getchar(); } Note: % will print a single % 5/10: 0 5%10: 5 Output
Modulus Division (cont) Second Example: No matter what, you answers must be integers. 5/10 = 0 5%10 =
Operator Precedence Here’s another problem. What’s the answer to this: x = * 6; Two Options (depending on the order of operations): Perform addition first: = 10 10 * 6 = 60 Perform multiplication first: 3*6 =18 7+18 = 25 Which option is correct? Cleary, we cannot have this kind of ambiguity.
Operator Precedence Rules for evaluating mathematical expressions. From left to right: –Parentheses are always evaluated first. –Multiplication, division and modulus are evaluated next. –Addition and subtraction are evaluated last.
Operator Precedence Hence, option #2 is correct: To find a student’s grade average, what’s wrong with this? When in doubt, use parentheses. x = * 6; Evaluates to x = = 25 avg = / 4
Float Variables Float Data Type: Data type that can hold numbers with decimal values, e.g. 5.14, 3.14.
Float Example (Program 2.2) /* Float Example Program */ #include main () { float var1, var2, var3, sum; var1 = 87.25; var2 = 92.50; var3 = 96.75; sum = var1 + var2 + var3; printf ("Sum: %.2f", sum); getchar(); } %f: indicates floating values %.2f displays a floating point value with 2 decimal points. Output: Sum: