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
@UMBC Training Centers 2013 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
@UMBC Training Centers 2013 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
@UMBC Training Centers 2013 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 C99 data type addition www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 Type Specifiers long Applied to int to give a (possibly) larger range of values Also applicable to double long 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
@UMBC Training Centers 2013 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 0 to 65535 signed int 4 -2147483648 to 2147483647 unsigned int 0 to 4294967295 signed long int unsigned long int signed long long int 8 -9 x 1018 to 9 x 1018 unsigned long long int 0 to 18 x 1018 www.umbctraining.com @UMBC Training Centers 2013 7
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
@UMBC Training Centers 2013 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
@UMBC Training Centers 2013 Literal Constants Decimal (base 10) integer constant 1345 1345L Octal (base 8) constant 0173 Hexadecimal (base 16) constant 0x12F4C Floating Point constant 7.23 723e-2 String constant “Hello World” Character constant ‘z’ ‘\n’ www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 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
@UMBC Training Centers 2013 Exercises Questions Text pg 40 - #2, #3 Just write down your answers They do not involve coding www.umbctraining.com @UMBC Training Centers 2013
Variable Declarations int height = 72; long maxNumberOfPassengers; char middleInitial = ‘L’, newLine = ‘\n’; double averageScore = 0.0; unsigned int nrRows = 5, nrColumns = 10; short average; www.umbctraining.com @UMBC Training Centers 2013
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
@UMBC Training Centers 2013 In-Class Example VarBasicOutput project Look at floatingVar 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
@UMBC Training Centers 2013 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
@UMBC Training Centers 2013 In-Class Example VarDataDemo project www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 Arithmetic Addition x = 3 + 2.5; y = z + w; Subtraction x = 3.7 – 2; d = 5 – z; Multiplication x = 3.6 * 2.5; x = 5 * y; Division x = 3 / 2; x = 3 / 2.0; Modulus x = 3 % 2; y = z % 6; www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 Exploring Division Integer Division int / int int 3 / 2 = 1 Floating point division float / float float 3.0 / 2.0 = 1.5 Mixed Division int / float float 3 / 2.0 = 1.5 float / int float 3.0 / 2 = 1.5 www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 Type Conversion int i1, i2 = -150; float f1 = 123.125, f2; i1 = f1; f1 = i2; f1 = i2 / 100; f2 = i2 / 100.0; f2 = (float)i2 / 100; www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 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
@UMBC Training Centers 2013 Pre/post-increment x++ (post-increment) int z, x = 5; x++; z = 3 * x++; ++x (pre-increment) ++x; z = 3 * ++x; www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 Pre/post-decrement x-- (post-decrement) int z, x = 5; x-- z = 3 * x-- --x (pre-decrement) --x; z = 3 * --x; www.umbctraining.com @UMBC Training Centers 2013
Precedence and Associativity Level Operators Associativity Operation 1 () expr++,expr -- L R Parentheses 2 ++expr, --expr R L Unary Operators 3 +,- 4 *, /, % Binary Operators 5 +, - 6 =, -=, +=, -=, *=, /=, %= Assignment operators www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 In-Class Exercises Solve these expressions http://www.csee.umbc.edu/courses/undergraduate/201/spring09/misc/arithmetic.shtml www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 Lab Exercises Text Questions pg 40 #1 not all do first one, then another of your choice #2-8 Ex1-ArithmeticPractice.docx—from CMSC 201 Ex2-Coins.docx – quarters, nickels, dime, pennies to dollars and cents www.umbctraining.com @UMBC Training Centers 2013
@UMBC Training Centers 2013 www.umbctraining.com @UMBC Training Centers 2013