3/13/2012. ESc101: Variable Types. Types of Variables. Variables

Similar documents
Number Representation

Sources: On the Web: Slides will be available on:

Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

Numbering Systems. InThisAppendix...

Chapter 4: Computer Codes

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

ECE 0142 Computer Organization. Lecture 3 Floating Point Representations

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

The programming language C. sws1 1

Pemrograman Dasar. Basic Elements Of Java

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

Lecture 2. Binary and Hexadecimal Numbers

LSN 2 Number Systems. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

CSI 333 Lecture 1 Number Systems

Informatica e Sistemi in Tempo Reale

DNA Data and Program Representation. Alexandre David

Computers. Hardware. The Central Processing Unit (CPU) CMPT 125: Lecture 1: Understanding the Computer

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

3/13/2012. Writing Simple C Programs. ESc101: Decision making using if-else and switch statements. Writing Simple C Programs

The string of digits in the binary number system represents the quantity

This 3-digit ASCII string could also be calculated as n = (Data[2]-0x30) +10*((Data[1]-0x30)+10*(Data[0]-0x30));

The New IoT Standard: Any App for Any Device Using Any Data Format. Mike Weiner Product Manager, Omega DevCloud KORE Telematics

Computer Science 281 Binary and Hexadecimal Review

CDA 3200 Digital Systems. Instructor: Dr. Janusz Zalewski Developed by: Dr. Dahai Guo Spring 2012

How To Write Portable Programs In C

Levent EREN A-306 Office Phone: INTRODUCTION TO DIGITAL LOGIC

2 ASCII TABLE (DOS) 3 ASCII TABLE (Window)

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Systems I: Computer Organization and Architecture

Solution for Homework 2

So far we have considered only numeric processing, i.e. processing of numeric data represented

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

Binary Division. Decimal Division. Hardware for Binary Division. Simple 16-bit Divider Circuit

Arithmetic Coding: Introduction

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Data Storage. Chapter 3. Objectives. 3-1 Data Types. Data Inside the Computer. After studying this chapter, students should be able to:

Useful Number Systems

Introduction to Java

2011, The McGraw-Hill Companies, Inc. Chapter 3

Data Storage 3.1. Foundations of Computer Science Cengage Learning

Measures of Error: for exact x and approximation x Absolute error e = x x. Relative error r = (x x )/x.

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

CS321. Introduction to Numerical Methods

Binary Numbers. Binary Octal Hexadecimal

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

HOMEWORK # 2 SOLUTIO

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

This Unit: Floating Point Arithmetic. CIS 371 Computer Organization and Design. Readings. Floating Point (FP) Numbers

Section 1.4 Place Value Systems of Numeration in Other Bases

Base Conversion written by Cathy Saxton

Organization of Records in Blocks

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

C++ Programming Language

Divide: Paper & Pencil. Computer Architecture ALU Design : Division and Floating Point. Divide algorithm. DIVIDE HARDWARE Version 1

ASCII Characters. 146 CHAPTER 3 Information Representation. The sign bit is 1, so the number is negative. Converting to decimal gives

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CHAPTER 5 Round-off errors

The C Programming Language course syllabus associate level

Numerical Matrix Analysis

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:

Chapter 7D The Java Virtual Machine

Java Interview Questions and Answers

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Arithmetic in MIPS. Objectives. Instruction. Integer arithmetic. After completing this lab you will:

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Variables, Constants, and Data Types

C PROGRAMMING FOR MATHEMATICAL COMPUTING

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

The Answer to the 14 Most Frequently Asked Modbus Questions

Overview. java.math.biginteger, java.math.bigdecimal. Definition: objects are everything but primitives The eight primitive data type in Java

Introduction to Java. CS 3: Computer Programming in Java

Chapter Binary, Octal, Decimal, and Hexadecimal Calculations

arrays C Programming Language - Arrays

Binary Representation

Data Storage: Each time you create a variable in memory, a certain amount of memory is allocated for that variable based on its data type (or class).

Chapter 2 Elementary Programming

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

MS ACCESS DATABASE DATA TYPES

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011

Today s topics. Digital Computers. More on binary. Binary Digits (Bits)

Introduction to Xilinx System Generator Part II. Evan Everett and Michael Wu ELEC Spring 2013

Chapter 1. Binary, octal and hexadecimal numbers

Principles of Database Management Systems. Overview. Principles of Data Layout. Topic for today. "Executive Summary": here.

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

7th Marathon of Parallel Programming WSCAD-SSC/SBAC-PAD-2012

Chapter 5. Binary, octal and hexadecimal numbers

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Chapter One Introduction to Programming

Tokens and Python s Lexical Structure

1 Abstract Data Types Information Hiding

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

C++ Language Tutorial

Unsigned Conversions from Decimal or to Decimal and other Number Systems

Transcription:

ESc101: Variable Types Instructor: Krithika Venkataramani Semester 2, 2011-2012 The content of these slides are taken from the lecture slides of Prof. Arnab Bhattacharya, Prof. R.K. Ghosh, Prof. Dheeraj Sanghi and Prof. Manindra Agrawal 1 2 Variables Types of Variables Variables signify data that may be modified Name of a variable can contain letters, digits and underscore _ Example: i, y2k, big_name, bigger_name_2 Case-sensitive: camel, CAMEL and CaMeL are different Name cannot start with a digit Example: 1d is not valid Name can start with an underscore, but do not do so Example: avoid valid names such as _bad Certain keywords are special They are reserved and cannot be used Example: main, if Each variable type represents the domain of values Integer: int or char Character: char Boolean: int or char real: double or float However, they can store only a subset of the domain int can store numbers from -2 31 to 2 31-1 Initial values of variables are specified as constants of the same type int i = 0; double d = 1.4; char c = 'A' 3 4 1

Example Program to Add two numbers Different Data Types /* Program to add two numbers */ #include <stdio.h> // Include headers void main() // Main function { int a=2, b=3, c; // Declare variables scanf("%d", &a); // Read 'a' ' from keyboard scanf("%d", &b); // Read 'b' from keyboard c = a + b; printf("%d\n", c); // Write 'c' to screen } Different types are needed because one type is not suitable for representing data of another type. Mixing types may result in precision loss, overflow, underflow Application performance suffers while performing numerically intensive computation if inappropriate data types are used. Exceptions must be handled explicitly or they lead to errors. Use of appropriate type is important both for efficiency and correctness 5 6 Integer Types Integer Representation Two different int types: signed and unsigned Maximum signed int in 16 bit: 011111111111111, i,e., 2 15-1 Maximum unsigned int in 16 bit: 111111111111111, i.e., 2 16-1 Possible types to suit our needs are: short int, unsigned short int, unsigned int, long int, unsigned long int. We represent integers as binary numbers For example: 56 will be stored as 00111000 How do we store negative numbers? 1st bit of memory is usually the sign bit In case of 8 bit memory, only 7 bits are for magnitude. Similarly 32-bit memory would have 31 bits for magnitude Hence the largest positive integer that can be stored in an integer variable on our PCs is: 2 31 1. Smallest number: There are variations on storing magnitude Overflow: Trying to store numbers outside the range 7 8 2

Different Real Number Types Representation of Real Numbers Real numbers of arbitrary precision cannot be represented Different types: float, double, long double double is more accurate than float 1/3 is printed as 0.33333334326.. as a float, but 0.33333333333.. as a double double is used for precision ii critical calculations lti By default floating point constants are stored as a double. To force float constant should be suffixed with f, i.e., 7.5f or 7.5F. Format specifier "%lf", "%Lf" are used for using double and long double using scanf/printf Use the scientific notation: f * b k With this notation, we need to store f and k. We also need to decide the value of b. The most commonly used representation is: Use 1 bit for sign Value of b is taken as 2 Use 8 bits to store k (called exponent) Use 23 bits to store f (called mantissa), in normalized form with integer part of the fraction to be exactly 1 (e.g. 1.0011) Exponent can be from -127 to +126 So the range is from 2-127 to 2 126, or 10-38 to 10 +38 approx. 9 10 Errors in representing real numbers Range of different data types There are three types of errors: Underflow: Trying to store exponent less than -127 Overflow: Trying to store exponent more than 126 Rounding off: Storing the nearest floating point number Floating point arithmetic The hardware has to do a lot more for floating point arithmetic compared to integer arithmetic Do not store numbers as floating point, unless you really need fractions 11 Variables are stored in a predefined space A unit of storage is a Byte A Byte has space to store a sequence of 8 binary digits Different variable types have different storage space assigned Assignment of space is machine dependent Type Space assigned in Bytes Range char 1-2 7 to (2 7-1) unsigned char 1 0 to (2 8-1) short int 2-2 15 to (2 15-1) unsigned short int 2 0 to (2 16-1) int 4-2 31 to (2 31-1) unsigned int 4 0 to (2 32-1) float 4 (approx) [10-38, 10 38 ] double 8 (approx) [10-308, 10 308 ] 12 3

Input and output of variables Character type Correct type specification must be used Type Format Specifier char %c int %d unsigned int %u float %f, %g, %e double %lf long double %Lf scanf is for input Format: scanf( <specification>, &<name>); E.g. c is a char: scanf( %c, &c); printf is for output Format: printf( <specification>, <name>); E.g. c is a char: printf(``%c'', c); 13 Variable type char used for representing characters Characters are special integers of much shorter size Only 256 characters can be represented Digits 0-9 are not represented by 00000000-00001001 0-9 represented by a continuous sequence Similarly A-Z (a-z) also represented by a continuous sequence ASCII character set is most widely used specifies a standard that maps characters to numbers 0-127 Extended ASCII assigns symbols to numbers 128-255 ASCII and Extended ASCII use 1 Byte for storage Unicode includes characters from all languages of the world Unicode uses 2 Bytes 14 The ASCII Table Printing the Code of a Character /*Program to print the code of a character*/ #include <stdio.h> void main() { int code; //Declare variable to store the code code = (int) getchar(); //Asking user to input the character printf("%d", code); //printing the code of the character } 15 16 4

Additional data types Data type Format specifier Size (machine Range dependent) int %d(decimal), %i 4 bytes -2 31-1 to 2 31-1 unsigned int %u 4 bytes 0 to 2 32-1 short int (unsigned) %hd (%hu) 2 bytes -2 15-1 to 2 15-1 long int (unsigned) %ld (%lu) 8 bytes -2 63-1 to 2 63-1 char %c, %d 1 byte -128 to 127 unsigned char %u, %d 1 byte 0 to 255 string %s array of -- characters float %f, %g, %e 4 bytes 3.4 10-38 to 3.4 10 38 double %lf, %lg, %le 8 bytes 1.7 10-308 to 1.7 10 308 long double %Lf, %Lg, %Le 16 bytes? 1 bit: 1 or 0 1 Byte: 8 bits Note: for 32-bit machines long int and int are same 17 Additional formats for octal and hexadecimal Data type Format Display/ Read specifier unsigned int %o unsigned octal integer unsigned int %x, %X unsigned hexadecimal integer unsigned long int %lo unsigned octal integer unsigned long int %lx, %lx unsigned hexadecimal integer unsigned short int %ho unsigned octal integer unsigned short int %hx, %hx unsigned hexadecimal integer 18 5