Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 2 Variables, Data Types and Arithmetic

Similar presentations


Presentation on theme: "Module 2 Variables, Data Types and Arithmetic"— Presentation transcript:

1 Module 2 Variables, Data Types and Arithmetic

2 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 @UMBC Training Centers 2013

3 @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 @UMBC Training Centers 2013

4 @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 @UMBC Training Centers 2013

5 @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 @UMBC Training Centers 2013

6 @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 @UMBC Training Centers 2013

7 @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 to 32767 unsigned short int 0 to 65535 signed int 4 to unsigned int 0 to 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 @UMBC Training Centers 2013 7

8 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 ??? @UMBC Training Centers 2013 8

9 @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 @UMBC Training Centers 2013

10 @UMBC Training Centers 2013
Literal Constants Decimal (base 10) integer constant L Octal (base 8) constant 0173 Hexadecimal (base 16) constant 0x12F4C Floating Point constant e-2 String constant “Hello World” Character constant ‘z’ ‘\n’ @UMBC Training Centers 2013

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

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

13 Variable Declarations
int height = 72; long maxNumberOfPassengers; char middleInitial = ‘L’, newLine = ‘\n’; double averageScore = 0.0; unsigned int nrRows = 5, nrColumns = 10; short average; @UMBC Training Centers 2013

14 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 @UMBC Training Centers 2013

15 @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 @UMBC Training Centers 2013

16 @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 @UMBC Training Centers 2013

17 @UMBC Training Centers 2013
In-Class Example VarDataDemo project @UMBC Training Centers 2013

18 @UMBC Training Centers 2013
Arithmetic Addition x = ; 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; @UMBC Training Centers 2013

19 @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 @UMBC Training Centers 2013

20 @UMBC Training Centers 2013
Type Conversion int i1, i2 = -150; float f1 = , f2; i1 = f1; f1 = i2; f1 = i2 / 100; f2 = i2 / 100.0; f2 = (float)i2 / 100; @UMBC Training Centers 2013

21 @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; @UMBC Training Centers 2013

22 @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; @UMBC Training Centers 2013

23 @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; @UMBC Training Centers 2013

24 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 @UMBC Training Centers 2013

25 @UMBC Training Centers 2013
In-Class Exercises Solve these expressions @UMBC Training Centers 2013

26 @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 @UMBC Training Centers 2013

27 @UMBC Training Centers 2013
@UMBC Training Centers 2013


Download ppt "Module 2 Variables, Data Types and Arithmetic"

Similar presentations


Ads by Google