Assistant Professor Rabia Koser Dept. of Computer Applications

Slides:



Advertisements
Similar presentations
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Advertisements

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.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Types and Variables. Computer Programming 2 C++ in one page!
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
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)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
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.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Simple Data Type Representation and conversion of numbers
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
CPS120: Introduction to Computer Science
Lecture #5 Introduction to C++
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Copyright © – Curt Hill Types What they do.
1 Information Representation in Computer Lecture Nine.
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)
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Simple Data Types in C Alan L. Cox Objectives Be able to explain to others what a data type is Be able to use basic data types in C programs.
CS 232: Computer Architecture II Prof. Laxmikant (Sanjay) Kale Floating point arithmetic.
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.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Simple Data Types in C Alan L. Cox Objectives Be able to explain to others what a data type is Be able to use basic data types in C programs.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
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
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
Floating Point Arithmetic – Part I
William Stallings Computer Organization and Architecture 8th Edition
David Kauchak CS 52 – Spring 2017
Basic Data Types & Memory & Representation
C++ Lesson 1.
Programming and Data Structure
The Machine Model Memory
Number Representation
Topics IEEE Floating Point Standard Rounding Floating Point Operations
More important details More fun Part 3
Bits, Bytes, and Integers CSE 238/2038/2138: Systems Programming
ITEC113 Algorithms and Programming Techniques
Instructor: David Ferry
Floating Point Number system corresponding to the decimal notation
CS 232: Computer Architecture II
Chapter 6 Floating Point
Data Structures Mohammed Thajeel To the second year students
Chapter 2 Bits, Data Types & Operations Integer Representation
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to Abstract Data Types
Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei
Bits, Bytes, and Integers January 16, 2008
Introduction to Java, and DrJava part 1
Bits and Bytes Boolean algebra Expressing in C
Bits, Bytes, and Integers 2nd Lectures
Bits and Bytes Topics Representing information as bits
Integers II CSE 351 Summer 2018 Instructor: Justin Hsia
Introduction to Java, and DrJava
C++ Programming Lecture 3 C++ Basics – Part I
Comp Org & Assembly Lang
Operations and Arithmetic
CS 105 “Tour of the Black Holes of Computing!”
Introduction to Java, and DrJava
Module 2 Variables, Data Types and Arithmetic
Rayat Shikshan Sanstha’s S. M. Joshi College, Hadapsar
Introduction to Java, and DrJava part 1
Presentation transcript:

Assistant Professor Rabia Koser Dept. of Computer Applications Simple Data Types in C Prepared By:- Assistant Professor Rabia Koser Dept. of Computer Applications

Everything is Just a Bunch of Bits Bits can represent many different things Depends on interpretation You and your program must keep track of what kind of data is at each location in the computer’s memory E.g., program data types

Data types in C Only really four basic types: Size of these types on char int (short, long, long long, unsigned) float double Size of these types on CLEAR machines: Sizes of these types vary from one machine to another! Type Size (bytes) char 1 int 4 short 2 long 8 long long float double

Characters (char) Roman alphabet, punctuation, digits, and other symbols: Encoded within one byte (256 possible symbols) ASCII encoding (man ascii for details) In C: char a_char = ’a’; char newline_char = ’\n’; char tab_char = ’\t’; char backslash_char = ’\\’;

Characters are just numbers What does this function do? argument type and name return type char fun(char c) { char new_c; if ((c >= ’A’) && (c <= ’Z’)) new_c = c - ’A’ + ’a’; else new_c = c; return (new_c); } procedure name local variable type and name comparisons with characters! Math on characters!

Bit Shifting as Multiplication 1 = 3 = 6 = -3 = -6 Shift left (x << 1) multiplies by 2: Works for unsigned, 2’s complement Can overflow In decimal, same idea multiplies by 10: e.g., 42  420

Bit Shifting as Division 1 = 7 = 3 = 9 = 4 Always rounds down! Logical shift right (x >> 1) divides by 2 for unsigned: 1 = 7 = 3 = -7 = -4 Always rounds down! Arithmetic shift right (x >> 1) divides by 2 for 2’s complement:

Bit Shifting for Multiplication/Division Why useful? Simpler, thus faster, than general multiplication & division Standard compiler optimization Can shift multiple positions at once: Multiplies or divides by corresponding power-of-2 a << 5 a >> 5

A Sampling of Integer Properties For both unsigned & 2’s complement: Mostly as usual, e.g.: 0 is identity for +, - 1 is identity for ×, ÷ +, -, × are associative +, × are commutative × distributes over +, - Some surprises, e.g.: ÷ doesn’t distribute over +, -  (a,b > 0  a + b > a) Why should you care? Programmer should be aware of behavior of their programs Compiler uses such properties in optimizations

FP Representation: IEEE 754 Current standard version of floating-point Single-precision (float) One word: 1 sign bit, 23 bit fraction, 8 bit exponent Positive range: 1.17549435 × 10-38 … 3.40282347 × 10+38 Double-precision (double) Two words: 1 sign bit, 52 bit fraction, 11 bit exponent Positive range: 2.2250738585072014 × 10-308 … 1.7976931348623157 × 10+308

Compiler needs this or it Booleans in C bool added to C in 1999 Many programmers had already defined their own Boolean type To avoid conflict bool is disabled by default #include <stdbool.h> bool bool1 = true; bool bool2 = false; Important! Compiler needs this or it won't know about "bool"!

Enumerated Types in C Alternative style: Pre-C99 Boolean definition: enum Color { RED, WHITE, BLACK, YELLOW }; enum Color my_color = RED; The new type name is “enum Color” Alternative style: enum AColor { COLOR_RED, COLOR_WHITE, COLOR_BLACK, COLOR_YELLOW }; typedef enum AColor color_t; color_t my_color = COLOR_RED; Pre-C99 Boolean definition: enum Bool { false = 0, true = 1 }; typedef enum Bool bool; bool my_bool = true;

Thanks