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)

Slides:



Advertisements
Similar presentations
Types and Variables. Computer Programming 2 C++ in one page!
Advertisements

Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Lab 10 rRepresentation And Conversion of Numeric Types l Difference between Numeric Types l Automatic conversion of Data types l Explicit Conversion of.
1 Fundamental Data Types. 2 Declaration All variables must be declared before being used. –Tells the compiler to set aside an appropriate amount of space.
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.
1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
CS1061 C Programming Lecture 7: Floating Point A. O’Riordan, 2004.
Instructor: Sanchita Mal-Sarkar Course: CIS 265
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
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.
21 March, 2000 CS1001 Lecture 4 Data Types + Algorithms = Programs Data Types & Arithmetic Errors Arithmetic Operators and Functions Program Testing Tips.
Chapter 7 Simple Data Types and Function Calls Alkar / Demirer.
Number Systems Lecture 02.
Introduction to Computing Systems from bits & gates to C & beyond Chapter 2 Bits, Data Types & Operations Integer Representation Floating-point Representation.
Simple Data Type Representation and conversion of numbers
Chapter 7 Simple Date Types Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Binary Real Numbers. Introduction Computers must be able to represent real numbers (numbers w/ fractions) Two different ways:  Fixed-point  Floating-point.
Computer Arithmetic Nizamettin AYDIN
1 Digital Technology and Computer Fundamentals Chapter 1 Data Representation and Numbering Systems.
Computer Science 111 Fundamentals of Programming I Number Systems.
IT253: Computer Organization
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Floating Point. Agenda  History  Basic Terms  General representation of floating point  Constructing a simple floating point representation  Floating.
Data Representation in Computer Systems
Lecture 5. Topics Sec 1.4 Representing Information as Bit Patterns Representing Text Representing Text Representing Numeric Values Representing Numeric.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Unit 3 Lesson 4 How Data Types Affect Calculations Dave Clausen La Cañada High School.
Simple Data Types Built-In and User Defined Chapter 10.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Copyright © – Curt Hill Types What they do.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
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.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
CS 125 Lecture 3 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
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.
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
1 Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
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.
CSE 220 – C Programming Bitwise Operators.
Chap. 2. Types, Operators, and Expressions
Tokens in C Keywords Identifiers Constants
Data Structures Mohammed Thajeel To the second year students
Fundamental Data Types
Chapter 2 Bits, Data Types & Operations Integer Representation
Introduction to Abstract Data Types
Simple Data Types and Function Calls
Lectures on Numerical Methods
Storing Negative Integers
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng.
Fundamental Data Types
Presentation transcript:

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) = 4 In one machine In another machine

The following statements are true for all machines 1 = sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) 1 <= sizeof(bool) <= sizeof(long) sizeof(float) <= sizeof(double) <= sizeof(long double) A char is at least 8 bits A short is at least 16 bits. A long is at least 32 bits.

Different systems Binary system: 0 or 1 => two digits Decimal system: 0,1,2,3,4,5,6,7,8,9=>ten digits. Octal system: 0,1,2,3,4,5,6,7 => 8 digits Hexadecimal system: 0,1,2,3,4,5,6,7,8,9,A, B, C, D, E, and F => 16 digits

Bitwise operators << Left shift >> Right shift & Bitwise AND | Bitwise OR ^ Bitwise EXCLUSIVE OR(XOR) ` Complement (invert all bits)

Operators The main operators are as follows:

AND operator Bitwise operator AND returns a 1 if both operand bits are 1. 1 & 1 = 1 0 & 1 = 0 1 & 0 = 0 0 & 0 = 0 Example:

OR operator Bitwise operator OR returns a 1 if at least one operand is a 1. 1 | 1 = 1 0 | 1 = 1 1 | 0 = 1 0 | 0 = 0 Example:

XOR Bitwise XOR (exclusive OR) returns a 1 if only one of the operands is a 1. 1 ^ 1 = 0 0 ^ 1 = 1 1 ^ 0 = 1 0 ^ 0 = 0 Example:

Left Bit Shift Left Bit Shift moves the bit values left to a specified position. Bits to the left are moved out of focus and the new bits on the right are set to zero.

Right Bit Shift Right Bit Shift moves the bit values right to a specified position. Bits to the right are moved out of focus and the new bits on the left are set to zero.

Representation and conversion of Numeric Types Why we need more than one numeric type ? We could use data type double for all numbers. However, they are represented in the computer’s memory in different ways. Here are some advantages of integer data type: –On many computers, operation will be faster if we use integer instead of type double. –We need less space to store integer number. –Operations with integer are always precise. But in operations with double numbers can result in some loss of accuracy or round-off error.

Advantages of Type double Format A real number has an integral part and a fractional part. But an integer number can not have a fractional part. So real number can not be represented by an integer, it can be represented by double data type. Another advantage of the data type double is that much larger range of numbers can be represented as compared to type int. Actual ranges vary from one implementation to another. ANSI standard for C min range of positive values of type int: 1- 32, 767 min range of positive values of type double: to 10 37

Internal formats of Type int & Type double All data are represented in memory as binary strings (strings of 0s and 1s). However, the binary string stored for type int value 100 is not the same as the binary string stored for the type double number The actual internal representation is computer dependent. Positive integers are represented by standard binary numbers. Integer 100 is represented by binary number The format of type double is analogous to scientific notation. mantissaexponent type double format binary number type int format

Scientific Notation A real number has an integral part and a fractional part which are separated by a decimal point. Very large or very small values can be represented by scientific notation. Example: In C++ scientific notation: 1.23e5 or 1.23E5 Read the letter e or E as "times 10 to the power". If the exponent has a minus sign, the decimal point is moved to the left. Example: 0.34e-4 ==

Floating-point notation Usual method to represent real numbers Real number is represented by a number, called a mantissa, times a base raised to an integer power, called an exponent. Example: = x Other possibilities x 10 3, x10 0 (we choose mantissa is an integer with no tailing 0s)

Floating-point notation(cont.) A real number is represented by a 32-bit string. 24-bit for mantissa and 8-bit for exponent. Base is fixed to 10. Examples 100 = = = bit binary representation of is bit twos complement binary representation of -2 is

Representation & Conversion of Type char We can declare variable of type char to represent a single character. A character can be a letter, digit, or punctuation mark. A character is enclosed in apostrophes.(e.g. ‘A’, ‘3’). We can compare character values using equality operators (== and !=) and relational operators (, >=). In ASCII (American Standard Code for Information Interchange): digit character ‘0’ = 48 (decimal code value) ‘9’ = 57 (‘0’<‘1’<‘2’<‘3’<‘4’…….<‘9’) ‘A’ = 65 ‘Z’ = 90 (‘A’< ‘B’<‘C’……..<‘X’<‘Y’<‘Z’) ‘a’ = 97 ‘z’ = 122 (‘a’< ‘b’<‘c’……..<‘x’<‘y’<‘z’) C++ allows conversion of type char to type int and vice versa.

Converting digit characters to integers ‘0’ – ‘0’ = 0 ‘1’ – ‘0’ = 1 ‘2’ – ‘0’ = 2 ‘3 – ‘0’ = 3 In ASCII, ‘0’ has internal representation 48 and ‘2’ has internal representation 50.

Numerical Inaccuracies Sometimes an error occurs in representing real numbers. Analogy: in decimal system, 1/3 is ….. Some fractions canot be represented exactly as binary numbers in the mantissa of the type double format. This inaccuracy is called representational error. It depends on the number of binary digits used in the mantissa. The more bits, the smaller the error. An equality comparison of two type double values can lead to unexpected results. Example for (count = 0.0; count != 10.0; count = count + 0.1) { } Infinite loop

Cancellation Error When you add a very large and a very small real numbers, the larger number may cancel out the smaller number. Results in cancellation error. Example: = (On some computers)

Arithmetic Underflow If we multiply two very small numbers, the result may be too small to be represented accurately. The computational result will be represented by zero. This phenomenon is called arithmetic underflow.

Arithmetic Overflow If we multiply two very large numbers, the result may be too large to be represented. This phenomenon is called arithmetic overflow. Different C compilers handle it in different ways.

Automatic Conversion of Data Types In several cases, data of one numeric type are automatically converted to another numeric type. Some examples: int k = 5, m = 4, n; double x = 1.5, y = 2.1, z; AExpression of different numeric types: k + x Value of int variable k is converted Value is 6.5 to type double format before operation is performed. BRHS is of type int and LHS is of type double: z = k / m; Expression is evaluated first. Then, the result is converted to type double format for assignment. z = 1.0 Expression value = 1

Automatic Conversion of Data Types Example: int k = 5, m = 4, n; double x = 1.5, y = 2.1, z; CRHS is of type double and LHS is of type int: n = x * y; Expression is evaluated first. Then, the result is converted to type int format for assignment, and fractional part is lost. Expression value = 3.15; n = 3

Explicit Conversion of Data Types C also provides an explicit type conversion operation called a cast. Examples: –Using cast operation int n1, n2; double frac; frac = (double)n1 / (double)n2; 2.0 / –Without cast operation frac = n1 / n2; 2 / 4 integer division 0 Result: frac = 0.5 Result: frac = 0.0

Explicit Conversion of Data Types A. frac = (double)(n1 / n2); and B. frac = (double)n1 / (double)n2; C. frac = (double)n1 /n2; AThe quotient n1/n2 is computed first, results in loss of fractional part. The cast to double convertsthe whole number quotient tp type dpouble. Bfrac = (double)n1 / (double)n2; 2.0 / Cfrac = (double)n1 / n2; n2 will automatically be converted to double. Difference

Explicit Conversion of Data Types Sometimes we include casts that do not affect the result but simply make clear to the reader the conversions that would occur automatically. Example: int x, sqrt_x; sqrt_x = (int)sqrt((double)x); The formal parameter of sqrt function is of type double. The actual argument x is of type int. x will automatically be converted to type double. sqrt function returns a value of type double. This value will automatically be converted to type int since sqrt_x is of type int. Note: When a cast operation is applied to a variable, the conversion changes the value of the expression, but it does not change what is stored in the variable.

Enumerated Types ANSI C allows us to associate a numeric code with each category by creating an enumerated type that has its own list of meaningful values. typedef can be used to name user-defined types. Example: typedef enum { sunday, monday, tuesday, wednesday, thursday, friday, saturday} day_t; enumerated type day_t has seven possible values (enumeration constants). sunday will be represented as the integer 0, monday as integer 1, and so on. day_t today; // declaration of variable today.

Examples of Enumerated Types class_t is an enumerated data type: typedef enum {fresh, soph, jr, sr} class_t; What is the value of each of the following? (int) sr, (class_t) 0, (class_t)((int)soph + 1) What is displayed by this code fragment? for (class = fresh; class <= sr; ++class) cout << class << “ “; 3 fresh jr

Example of Enumerated Type Example: typedef enum { sunday, monday, tuesday, wednesday, thursday, friday, saturday} day_t; day_t next(day_t today) { day_t next; if (today == saturday) next = sunday; else next = (day_t)((int)today + 1); return (next); }

Another Example of Enumerated Type typedef enum { sunday, monday, tuesday, wednesday, thursday, friday, saturday} day_t; void print_day (day_t day) { switch (day) { case sunday: cout << “sunday”; break; case monday: cout << “monday”; break; default: cout << “Invalid Code”; }