Module 2 Variables Data Types and Arithmetic Naming

Module 2 Variables, Data Types and Arithmetic

Naming Rules and Conventions • • • Contain only letters, digits and underscore Cannot begin with a digit Are case-sensitive Begin with lowercase Do not begin with underscore Multi-word variables www. umbctraining. com @UMBC Training Centers 2013 2

Keywords • Commonly used keywords break double goto short typedef case else if signed union char enum int sizeof unsigned continue extern long static void default float register struct while do for return switch www. umbctraining. com @UMBC Training Centers 2013 3

Other Rules • Variables may be declared anywhere in your code, but must be declared before they are used • Global variables are permitted and declared outside of any block www. umbctraining. com @UMBC Training Centers 2013 4

Basic Data Types • int – integer values, no decimal point • float – floating point values with decimal part • double – same as float, but more precision • char – a single character • _Bool – boolean values 0 (false) and 1 (true) – This a C 99 data type addition www. umbctraining. com @UMBC Training Centers 2013 5

Type Specifiers • long – Applied to int to give a (possibly) larger range of values – Also applicable to double • long – Applied to int to give an even larger range of values • short – Applied to int to (possibly) limit the range of values • unsigned – Applied to int to indicate only zero and positive values • signed (default type) – Applied to int to specify positive, negative and zero values allowed www. umbctraining. com @UMBC Training Centers 2013 6

Integral Data Types • The table below shows the usual sizes and ranges of the integral data types on a 32 -bit machine • Your sizes and ranges may vary Data Type Size (in bytes) Range of values signed short int 2 -32768 to 32767 unsigned short int 2 0 to 65535 signed int 4 -2147483648 to 2147483647 unsigned int 4 0 to 4294967295 signed long int 4 -2147483648 to 2147483647 unsigned long int 4 0 to 4294967295 signed long int 8 -9 x 1018 to 9 x 1018 unsigned long int 8 0 to 18 x 1018 www. umbctraining. com @UMBC Training Centers 2013 77

Floating Point Data Types • The table below shows the usual sizes and ranges of the floating point data types on a 32 -bit machine • Your sizes and ranges may vary Data Type Size (in bytes) Precision Values float 4 Approximately 6 digits Approximately -10^38 to 10^38 double 8 Approximately 15 digits Approximately -10^308 to 10^308 long double Machine Dependent Even more precision ? ? ? www. umbctraining. com @UMBC Training Centers 2013 8 8

Booleans • Data type: _Bool • Size in bytes: (probably) 1 • Range of values: 0 , 1 • stdbool. h Defines bool data type Defines true and false www. umbctraining. com @UMBC Training Centers 2013 9

Literal Constants • Decimal (base 10) integer constant 1345 L • Octal (base 8) constant 0173 • Hexadecimal (base 16) constant 0 x 12 F 4 C • Floating Point constant 7. 23 723 e-2 • String constant “Hello World” • Character constant ‘z’ www. umbctraining. com ‘n’ @UMBC Training Centers 2013 10

Named Constants • #define preprocessor directive • Literal string substitution #define VOTING_AGE 18 #define GREETING “Hello!” #define PI 3. 14159 #define QUIT ‘q’ • Typically DO NOT end with semi-colon www. umbctraining. com @UMBC Training Centers 2013 11

Exercises • Questions – Text pg 40 - #2, #3 – Just write down your answers – They do not involve coding www. umbctraining. com @UMBC Training Centers 2013 12

Variable Declarations int height = 72; long max. Number. Of. Passengers; char middle. Initial = ‘L’, new. Line = ‘n’; double average. Score = 0. 0; unsigned int nr. Rows = 5, nr. Columns = 10; short average; www. umbctraining. com @UMBC Training Centers 2013 13

Displaying Basic Types • printf characters for basic type output – Integers • %d, %i • %u (unsigned) – Floating Point • %f – default (6 decimal places) • %e – scientific notation • %g – automatically chooses between %f and %e format – Characters • %c – a single character – Booleans • %d, %i, %u www. umbctraining. com @UMBC Training Centers 2013 14

In-Class Example • Var. Basic. Output project – Look at floating. Var and it’s results – Also look at the boolean output as an integer – what’s the difference between %d and %i – pg 26 in book www. umbctraining. com @UMBC Training Centers 2013 15

Operator Definitions • Operand – An expression which is manipulated by an operator • Operator – A symbol that represents the action to be taken – Unary, Binary or Ternary www. umbctraining. com @UMBC Training Centers 2013 16

In-Class Example • Var. Data. Demo project www. umbctraining. com @UMBC Training Centers 2013 17

Arithmetic • • • Addition Subtraction Multiplication Division Modulus www. umbctraining. com x = 3 + 2. 5; x = 3. 7 – 2; x = 3. 6 * 2. 5; x = 3 / 2; x = 3 % 2; @UMBC Training Centers 2013 y = z + w; d = 5 – z; x = 5 * y; x = 3 / 2. 0; y = z % 6; 18

Exploring Division • Integer Division – int / int 3 / 2 = 1 • Floating point division – float / float 3. 0 / 2. 0 = 1. 5 float 3 / 2. 0 = 1. 5 3. 0 / 2 = 1. 5 • Mixed Division – int / float – float / int www. umbctraining. com @UMBC Training Centers 2013 19

Type Conversion int i 1, i 2 = -150; float f 1 = 123. 125, f 2; i 1 = f 1; f 1 = i 2 / 100; f 2 = i 2 / 100. 0; f 2 = (float)i 2 / 100; www. umbctraining. com @UMBC Training Centers 2013 20

Binary Operators • Two operands x = x + 5 ; y = y * 6; z = z – (x + 2); • Shortcut notation x += 5; y *= 6; z -= x + 2; www. umbctraining. com @UMBC Training Centers 2013 21

Pre/post-increment • x++ (post-increment) int z, x = 5; x++; z = 3 * x++; • ++x (pre-increment) int z, x = 5; ++x; z = 3 * ++x; www. umbctraining. com @UMBC Training Centers 2013 22

Pre/post-decrement • x-- (post-decrement) int z, x = 5; x-z = 3 * x-- • --x (pre-decrement) int z, x = 5; --x; z = 3 * --x; www. umbctraining. com @UMBC Training Centers 2013 23

Precedence and Associativity Level Operators 1 () expr++, expr -- 2 ++expr, --expr 3 +, - 4 *, /, % 5 +, - 6 =, -=, +=, -=, *=, /=, %= www. umbctraining. com Associativity Operation L R Parentheses R L Unary Operators L R Binary Operators R L Assignment operators @UMBC Training Centers 2013 24

In-Class Exercises • Solve these expressions – http: //www. csee. umbc. edu/courses/undergradua te/201/spring 09/misc/arithmetic. shtml www. umbctraining. com @UMBC Training Centers 2013 25

Lab Exercises • Text Questions pg 40 – #1 • not all • do first one, then another of your choice – #2 -8 • Ex 1 -Arithmetic. Practice. docx—from CMSC 201 • Ex 2 -Coins. docx – quarters, nickels, dime, pennies to dollars and cents www. umbctraining. com @UMBC Training Centers 2013 26

www. umbctraining. com @UMBC Training Centers 2013 27
- Slides: 27