Bought the text book yet Lecture 2 Revised
Bought the text book yet? Lecture 2 Revised Fall 2009
Agenda • • Team Building Exercise Data Types and how Data Types are stored Printf Scanf Class Assignment Decisions Class Assignment
Team Building Exercise • Be prepared to introduce one member of your ‘team’. Include: – Their name – Favourite colour – Why their studying computers? – One thing that they did this weekend
What’s a variable?
What is a Variable? • Named area of memory • Symbolic name for an area of memory • Used to hold data – Data Types? • How could we retrieve information from memory if we didn’t have variables?
Data Types • Integers • Floating Points • Characters
Integers • Referred to as int • Positive or negative number without a decimal point • Short Integer (default) – Referred to as short – Two bytes are used to store integer • -32768 to 32767 • Long Integer – Referred to as long – Four bytes are used to store integer • -2147483648 to 2147438647
Floating Points • Used to store numbers with decimal points • Single-Precision (Float) – 4 bytes to store decimal • 1. 2 e-38 to 3. 4 e 38 • Double-Precision (Double) – 8 bytes to store decimal • 2. 2 e-308 to 1. 8 e 308
Characters • (CHAR) • Letters of the alphabet, Numbers and Special Characters • Sort Order – – Special Characters Numbers Uppercase Lowercase • See ASCII table in Text
Test Value -40, 000 -400 ‘ 4’ 400. 00 Appropriate Data Type
Characters • Stored using either ASCII or EBCDIC Codes • Conversion from Upper Case to Lower Case – ‘UC Value’ – 65 + 97
Variables in C ü A variable name can only consist of letters, numbers or the underscore “_” character ü The first character of a variable cannot be a number ü Try to create a descriptive name to make it easy to identify, but do not make variable names unnecessarily long especially if you do not type well.
Variable Name Examples
Purpose of a Program Input Program Output
printf Prints output to the output device
Escape characters Character Meaning n a b New Line Beep (Alarm) Backspace f t Form Feed (printer output) Go to next Tab (usually every 8 columns) Output a backslash \
scanf Scans the input device for data
Representation of data in printf & scanf Data Type C Format Keyword Specifier Integer Long Integer Floating Point Decimal Int Long Float %d %ld %f Double-Precision Floating Point Character Double %lf Char %c
printf(“what you want to print including format specifiers and escape characters”, variables) Example: Print the contents of the integer variable, x. printf(“x is: %dn”, x);
scanf(“formatspecifier”, &variable) Example: Scan in the contents of the integer variable, x. scanf(“%d”, &x);
Example
Write a program that declares the following variables Letter which is a character Age which is a whole number Cost of carpet which is a decimal Magnesium/gram which is a big decimal
The program should prompt for a value for each of the variables and them print the values on the screen
Decisions (if statements) if (condition) { statements; } else { statements; }
Conditions are comparison Condition Symbol Equal == (double equal) Not Equal != Greater than > Less than < Greater than or equal to >= Less than or equal to <=
Math in C Function Symbol Assignment = (single equals) Multiplication * Division / Addition + Subtraction - Modulus %
Problem Write a C program that asks the operator to enter a monetary amount and then gives the operator the following choices: 1. PST Only (8%) 2. GST Only (7%) If the operator enters a number other than 1 or 2, an error message is given and the program exits. If the selection is correct, the original number, the tax value and the total including the tax is displayed.
For Next Class • From IPC Notes – Read Chapters 1 & 2. – For extra practice, work on exercise 2. 2 in text.
- Slides: 28