Copyright © 2007-2010 – Curt Hill Types What they do.

Slides:



Advertisements
Similar presentations
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
String Escape Sequences
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Objectives You should be able to describe: Data Types
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
CPS120: Introduction to Computer Science Lecture 8.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Information Representation: Negative and Floating Point.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Numeric Types, Expressions, and Output ROBERT REAVES.
Copyright © Curt Hill BitWise Operators Packing Logicals with Other Bases as a Bonus.
CPS120: Introduction to Computer Science
Lecture #5 Introduction to C++
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Copyright Curt Hill Variables What are they? Why do we need them?
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
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.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
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)
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
Numbers in Computers.
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.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter 2: Introduction to C++
Data Types, Variables & Arithmetic
BASIC ELEMENTS OF A COMPUTER PROGRAM
ITEC113 Algorithms and Programming Techniques
Data Structures Mohammed Thajeel To the second year students
Introduction to Abstract Data Types
Basics of ‘C’.
Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei
Introduction to Java, and DrJava part 1
C++ Data Types Data Type
Introduction to Java, and DrJava
Primitive Types and Expressions
Introduction to Java, and DrJava
Introduction to Java, and DrJava part 1
Variables and Constants
Presentation transcript:

Copyright © – Curt Hill Types What they do

Introduction People are smart, computers are dumb This is obvious when we speak about types The type is usually understood when people work in algebra We do not care if the number is an integer, fraction or real – it is just a number Not so in C++ – we must explicitly state the type Copyright © – Curt Hill

Standard types in C programs A type determines legal values and legal operations Most of the standard types are reserved words In the rest of this presentation the sizes and precisions given are for our machines Others compilers may be different Copyright © – Curt Hill

Integer types int - a standard integer –Integers in range + or billion –Occupies four bytes short - half the size –Range is to long – In our case same as int Copyright © – Curt Hill

Integer Operators The integer types allow the standard mathematical operators: –Addition + –Subtraction - –Multiplication * –Integer division / 5 / 2 = 2 –Integer remainder % 5 % 2 = 1 Copyright © – Curt Hill

Integer division Integer operators always give integer results Remembering division and remainder –Do long division –Do not go past the decimal point –Quotient is obtained with / –Remainder with % Integer constants are strings of digits with or without a sign Copyright © – Curt Hill

Division operators If a is 22 and b is 5 a/b gives 4 a%b gives 2 Copyright © – Curt Hill 5 ) 22 4 r 2

Floating point types Also known as real numbers Consist of exponent and mantissa –Similar to scientific notation Float –Regular floating point numbers –About seven decimal digits Double –Twice as many mantissa digits as float –About 14 decimal digits Copyright © – Curt Hill

Constants There are two forms for a real constant Floating point constants have either a decimal point or an exponent or both Scientific notation is something like this: 3.2 × 10 4 Since the exponent is hard to type we use E notation: 3.2E4 Copyright © – Curt Hill

Double Preferred Our machines have plenty of memory so saving space is not the issue The Pentium only does double arithmetic The standard math library accepts only double parameters Therefore we routinely use double instead of float Copyright © – Curt Hill

Floating Point Operators The real types allow the standard mathematical operators: –Addition + –Subtraction - –Multiplication * –Real division / 5.0 / 2.0 = 2.5 –No remainder operator Copyright © – Curt Hill

Overloaded operators The / operator is overloaded, which means that it has different meanings depending on what operands it has With two integers it means integer division, with two floating point numbers it means real division We will later consider what it means when the types do not match Copyright © – Curt Hill

Character types Represents an ASCII character char One character Constants are enclosed in single quotes –As opposed to string constants which are in double quotes Small integers can be used to specify as well Copyright © – Curt Hill

Characters as numbers? There is some interchangability between characters and small integers Each character is represented on the machine by an integer in the range -128 to +127 The character set of a machine is the convention that connects the number to the character –We will use ASCII on Windows Copyright © – Curt Hill

Booleans Type name is bool Holds a true or false –Both are reserved words Will discuss this one more seriously later Copyright © – Curt Hill

Modifiers Before types we can apply several modifiers that alter some of the characteristics of the type long –Usually means that the item is twice as long as normal for more precision –Not applicable to characters short –Usually means that the item is half the length –Not applicable to characters Copyright © – Curt Hill

More modifiers signed –Explicitly must have a sign unsigned –May not have a sign –Not allowed on floating point types –Gives one more bit of precision for integers or characters Copyright © – Curt Hill

Examples A double is a long float Sometimes there is a long double Characters, may be by default either signed or unsigned –Ours are signed Any particular compiler may ignore some of these Copyright © – Curt Hill

For our purposes we will only consider the following: char int double bool Copyright © – Curt Hill

Mixed type expressions When an arithmetic expression has two types in it, we call this a mixed type expression In general the weaker type is converted to the stronger type and then the operation proceeds Shorter items are weaker than longer Integer items are weaker than floating Copyright © – Curt Hill

Examples: Add a short int to an int –The short int is converted to an int and the operation proceeds Add an int to a float –Convert the int to a float and continue To convert: –The original form and value of the variable are unchanged –For the purpose of the expression change the value from one type to another Copyright © – Curt Hill