Basic Input/Output and Variables Ethan Cerami New York University @1998.
-
date post
22-Dec-2015 -
Category
Documents
-
view
214 -
download
0
Transcript of Basic Input/Output and Variables Ethan Cerami New York University @1998.
![Page 1: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/1.jpg)
Basic Input/Outputand Variables
Ethan Cerami
New York University
@1998
![Page 2: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/2.jpg)
This Week
• Quick Review
• Integer Variables
• Basic Input: scanf()
• Float Variables
• Mathematical Operators
![Page 3: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/3.jpg)
Review: First Program in C
/* First Program in C */#include <stdio.h>
main(){ printf("Hello, World!");
/* Wait for user to Press Enter */ getchar();}
CommentsInclude Library Files
Main Function
Pauses Program
printf () Statement
![Page 4: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/4.jpg)
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.
![Page 5: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/5.jpg)
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.
![Page 6: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/6.jpg)
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
![Page 7: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/7.jpg)
Integer Data Type
• Integer: data type that can hold only whole numbers, e.g. 5, 76, 1000.
• Cannot hold numbers with decimal points, such as 4.2 or 3.14. Cannot hold strings, such as “Hello” or “Ethan”
• Example:
int x = 5;
![Page 8: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/8.jpg)
Integer Program (Program 2.0)#include <stdio.h>
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 Type Variable Names
Assignment Statements
![Page 9: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/9.jpg)
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
![Page 10: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/10.jpg)
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
![Page 11: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/11.jpg)
V. Basic Input: scanf()
• Input: any user supplied data.– Keyboard input, mouse input.
• scanf (): read in keyboard input.
![Page 12: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/12.jpg)
scanf() Example (Program 1.2)#include <stdio.h>#include <conio.h>
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
![Page 13: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/13.jpg)
Using scanf()
scanf ("%d", &integer1);
%d indicates integer values (just like printf)
Format SpecifierVariable Name
& Character is required
![Page 14: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/14.jpg)
Other tid-bits• getch();
Waits for user to press any key. getch() requires the #include <conio.h> library <conio.h>: 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.
![Page 15: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/15.jpg)
VI. Mathematical Operators
• Basic Mathematical Operators
+ - Addition / Subtraction
* Multiplication
/ Integer Division
% Modulus Division
![Page 16: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/16.jpg)
Integer Division - The Problem
• Suppose you have the following code:
• Using a calculator, the answer is 1.75.
• But x can only hold integer values. 1.75 is clearly not an integer value.
int x;x = 7 / 4;
![Page 17: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/17.jpg)
Integer Division - The Solution
• To understand the solution, you need to remember your 3rd Grade Math (really.)
• 7/4 = 1 (Integer Division)
• 7%4 = 3 (Modulus Division)
4 74
3
1
The answer: 1 remainder 3
![Page 18: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/18.jpg)
Integer/Modulus Division (Program 2.1)
/* Integer and Modulus Division */#include <stdio.h>
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: 05%10: 5 Output
![Page 19: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/19.jpg)
Modulus Division (cont)
• Second Example:
• No matter what, you answers must be integers.
5/10 = 0
5%10 = 5
5100
0
5
![Page 20: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/20.jpg)
Operator Precedence
• Here’s another problem. What’s the answer to this:x = 7 + 3 * 6;
• Two Options (depending on the order of operations):
• Perform addition first: 7 + 3 = 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.
![Page 21: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/21.jpg)
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.
![Page 22: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/22.jpg)
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 = 7 + 3 * 6;Evaluates to x = 7 + 18 = 25
avg = 90 + 95 + 95 + 100 / 4
![Page 23: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/23.jpg)
Float Variables
• Float Data Type: Data type that can hold numbers with decimal values, e.g. 5.14, 3.14.
![Page 24: Basic Input/Output and Variables Ethan Cerami New York University @1998.](https://reader031.fdocuments.in/reader031/viewer/2022032611/56649d7f5503460f94a61f8a/html5/thumbnails/24.jpg)
Float Example (Program 2.2)/* Float Example Program */#include <stdio.h>
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: 276.50