Simple Data Types Built-In and User Defined Chapter 10.

Slides:



Advertisements
Similar presentations
True or false A variable of type char can hold the value 301. ( F )
Advertisements

Types and Variables. Computer Programming 2 C++ in one page!
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Data types and variables
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Chapter 2 Data Types, Declarations, and Displays
Basic Elements of C++ Chapter 2.
1 Simple Data Types: Built-in and Use-Defined. 2 Chapter 10 Topics  Built-In Simple Types  Integral and Floating Point Data Types  Using Combined Assignment.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
1 Chapter 10 Simple Data Types: Built- In and User- Defined Dale/Weems.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Simple Data Types: Built-In and User-Defined
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Data Types Declarations Expressions Data storage C++ Basics.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
7. BASIC TYPES. Systems of numeration Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Arithmetic Expressions Function Calls Output
Chapter 7: Expressions and Assignment Statements
Chap. 2. Types, Operators, and Expressions
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Chapter 7: Expressions and Assignment Statements
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of C++
C++ Simple Data Types Simple types Integral Floating
Basic Elements of C++ Chapter 2.
Operators and Expressions
Data Types Chapter 8.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Basics of ‘C’.
Chapter 7 Additional Control Structures
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
Module 2 Variables, Data Types and Arithmetic
Presentation transcript:

Simple Data Types Built-In and User Defined Chapter 10

2 Built-In Simple Types Simple (or "atomic") Each value is indivisible, a single entity Examples –int, char, float NOT simple types –strings, arrays, structs

3 Characteristics of Simple Types An object of a given type uses a specific number of bytes –sizeof( ) function will give how many –int => 2 bytes –float => 4 bytes (on our Borland C++) An object of a simple type has a range of values –low to high –range is limited by size allocated to the type

4 Integral Types Include –char, short, int, long –can be signed or unsigned –signed integer stored in 2 bytes –range is … Constants can be specified in –decimal, octal, hex –normally in decimal

5 Floating Point Types Stored in scientific notation Include –float, double, long double Float stored in 8 bytes –5 - 6 significant digits –range  3.4 E  38

6 Assignment Operators & Expressions Assignment operator = –Syntax : variable = expression; Statement has –a value of the expression –value stored in variable location Value previously stored in variable is now wiped out

7 Combined Assignment Operators Consider a += 5; // same as a = a + 5; Similar results with -= *= /= %= The symbol += is considered a binary operator –syntax requires a variable on the left –an expression on the right

8 Increment & Decrement Operators a++; // a unary operator which increments a by 1 same as a = a + 1; or a += 1; Similar results with and -- work only on variables … not constants Consider cout << a++; and cout << ++a; –post increment prints, then increments –pre-increment increments first, then prints

9 Bitwise Operators Operators >, &, and | Used for manipulating individual bits within memory > for shifting bits Most of what we do will use && and || for logical AND and OR

10 The Cast Operator Wen mixing data types in expressions, implicit type casting occurs Example int x; float f; Consider x = f; Versus f = x; Text suggests explicit type casting x = (int)f; // use the type in ( ) f = float(x); // use the type as a function

11 The sizeof Operator A unary operator –yields size (in bytes) of some variable or data type –almost acts as a function cout << sizeof (part_struct); Often used for arrays or structs where you need to specify number of bytes to be read in or sent to a file

12 The Selection Operator ? : A trinary operator -- requires three operands Example amt = (x < 5) ? y : z; If x < 5 then amt gets value stored in y otherwise gets value stored in z

13 Operator Precedence Similar to algebraic precedence –Parentheses, unary, mult, div, addn, subt –Note page A1 Appendix B Note also that some are L-> R, others are R-> L:

14 Character Data Char is actual considered a subset of int –uses 1 byte of memory Since it is subset of int, it can store numbers or characters char c1, c2; c1 = 12; c2 = 'A' // Both are legal

15 Character Sets External representation => what you see printed on screen or printer Internal representation => bit form for storing the data in the computer memory Most machines we encounter will use ASCII character set Other machines use EBCDIC Appendix D, pg A9 has examples of both

16 C++ char Constants Single printable character enclosed by single quotes 'A' '7' '$' –can be letters, numerals, or symbols Control characters (non printable characters) used to control output '\n' for newline (same as endl) '\f' for form feed '\t' for tab '\a' for beep

17 Comparing Characters Use comparison operators = etc. Consider if (ch >= 'a' && ch <= 'z')... This checks to see if ch is in the lower case characters Equivalent is if (islower (ch)) … –found in ctype.h Check out character-testing library functions in Appendix C, pgs A2-A4

18 Convert Digit Characters to Integers Possible to do arithmetic with characters –they are basically, integers Example char ch; cin >> ch; num = ch - '0';

19 Converting Lowercase to Uppercase From ctype.h header file, use functions provided ch = toupper (ch); ch = tolower(ch): Change only made if ch "needs" to be changed –if it is already uppercase, toupper( ) does nothing

20 Representing Floating Point Numbers Precision of 5 or 6 significant digits Represented internally in scientific notation Four bytes will store –sign bit for the number –5 or 6 significant digits –sign bit for the power –  38 range for the power

21 Arithmetic with Floating Point Numbers May lose accuracy due to round off error –especially when combining very large with very small numbers Warnings –don't use floats to control loops –don't compare floats for equality, rather compare for closeness if ( abs (a - b) < ) …

22 Implementation of Float on a Computer Example: E-4 Significant digits: from 1st nonzero digit on left to last non zero digit on right Precision: max number of significant digits Representational Error: The arithmetic error when precision of true results greater than precision for machine

23 Implementation of Float on a Computer Underflow –results of calculation too small to be represented 1.3E E-10 Overflow –value of calculation too large to be stored 5.6E20 * 7.8E30 –C++ does not define results when this occurs, usually garbage values

24 The Typedef Statement Syntax: typedef existing_type_name new_type_name; Example: typedef int Boolean; Does not really create a new type –is a valuable tool for writing self-documenting programs

25 Enumerated Types Possible to create a new type by simply listing (enumerating) the constants which make up the type Example: enum daysOfWeek (SUN, MON, TUE, WED, THU, FRI, SAT); daysOfWeek today, tomorrow, work_day; work_day = MON; C++ represents these internally as numbers (0.. 6 for our example)

26 Enumerated Types Incrementing variables of an enumerated type Do NOT use workaday += 1; NOR today++; Instead, use explicit type conversion today = daysOfWeek (today + 1);

27 Enumerated Types Comparison –normal, OK –in order of the enumeration definition I/O –generally not possible to do directly –can be sort of done, indirectly Used primarily for program control, branching, looping Possible to have functions return an enumerated type

28 Named and Anonymous Data Types Named Type –user defined type –declaration includes typedef –As with daysOfWeek or Boolean Anonymous Type does not have an associated type enum (MILD, MEDIUM, HOT) salsa_sizzle; variable declared without typedef

29 User-Written Header Files For your collection of handy identifiers –type such as our Boolean type definition use #include "bool.h" –note use of " " rather than Tells the computer to go looking at the logged directory for bool.h file and include/insert it into the source code.

30 Type Coercion in Expressions If two operands are of different types –one is temporarily "promoted" or "widened" to match the data type of the other int x = 5; float f = 1.234, amt = + f; Another example: –char or short operands promoted to int int x = 5 + 'Q'; Expression result is of type int

31 Type Coercion in Assignments Can result in "demotion" or "narrowing" Assigning a float to an integer variable int x = 3.456; –decimal portion of float is lost This loss of data can be considered a problem or a feature! x | 3 Memory