Data Types. Data Types. Integer Types. Signed Integers

Similar documents
Chapter 7. Basic Types

These are reserved words of the C language. For example int, float, if, else, for, while etc.

THE FUNDAMENTAL DATA TYPES

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Chapter 3 Basic Data Types. Lecture 3 1

Computer System and programming in C

COP 3275: Chapter 07. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

Computer Systems Programming. Practice Midterm. Name:

COMP2611: Computer Organization. Data Representation

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

1.3b Type Conversion

Computers Programming Course 5. Iulian Năstac

Reserved Words and Identifiers

The type of all data used in a C++ program must be specified

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

UNIT 3 OPERATORS. [Marks- 12]

A Fast Review of C Essentials Part I

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Data Types and Variables in C language

1/31/2018. Overview. The C Programming Language Part 2. Basic I/O. Basic I/O. Basic I/O. Conversion Characters. Input/Output Structures and Arrays

C-LANGUAGE CURRICULAM

Data Type Fall 2014 Jinkyu Jeong

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

COMP Overview of Tutorial #2

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Final Labs and Tutors

Basic Types and Formatted I/O

Lecture 02 C FUNDAMENTALS

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Chapter 3. Fundamental Data Types

Module 2: Computer Arithmetic

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

Programming for Engineers Iteration

Fundamentals of Programming Session 7

Data Representation 1

Operators and Expressions:

UNIT- 3 Introduction to C++

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

ESc101 : Fundamental of Computing

printf( Please enter another number: ); scanf( %d, &num2);

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Arithmetic Operators. Portability: Printing Numbers

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Basics of Programming

Fundamentals of Programming Session 8

First of all, it is a variable, just like other variables you studied

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Programming in C++ 6. Floating point data types

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Expressions and Casting

Input/Output Week 5:Lesson 16.1

Unit 4. Input/Output Functions

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

3. Types of Algorithmic and Program Instructions

Department of Computer Applications

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

The type of all data used in a C (or C++) program must be specified

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Inf2C - Computer Systems Lecture 2 Data Representation

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

SIGNED AND UNSIGNED SYSTEMS

Programming for Engineers Introduction to C

Fundamental of Programming (C)

UNIT 7A Data Representation: Numbers and Text. Digital Data

Basic Definition INTEGER DATA. Unsigned Binary and Binary-Coded Decimal. BCD: Binary-Coded Decimal

Declaration and Memory

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

More about Binary 9/6/2016

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

ECE2049: Embedded Computing in Engineering Design C Term Spring Lecture #3: Of Integers and Endians (pt. 2)

Fundamentals of Programming

BSM540 Basics of C Language

Number System. Introduction. Decimal Numbers

FLOATING POINT NUMBERS

Number Systems, Scalar Types, and Input and Output

C Programming

Chapter 2. Data Representation in Computer Systems

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

CS Programming In C

CS101 Introduction to computing Floating Point Numbers

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Integer Encoding and Manipulation

Programming in C++ 5. Integral data types

Advanced C Programming Topics

Structures, Operators

2. MACHINE REPRESENTATION OF TYPICAL ARITHMETIC DATA FORMATS (NATURAL AND INTEGER NUMBERS).

COMP2121: Microprocessors and Interfacing. Number Systems

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Transcription:

Data Types Data Types Dr. TGI Fernando 1 2 The fundamental building blocks of any programming language. What is a data type? A data type is a set of values and a set of operations define on these values. E.g. 1: Set of integers with operations +, -, *, /, etc. The built-in C types come in two groups: Fundamental types - integer (whole numbers) and floating-point (can have a fractional part). Derived types - derived from basic types. 1 Email: gishantha@dscs.sjp.ac.lk 2 URL: http://tgifernando.wordpress.com/ Integer Types Signed Integers Integer Set - infinite Computer memory can represent only a subset of integers. C offers several integer types - int, short int, long int Each comes in both signed and unsigned versions. The following six combinations make different integer data types: short int unsigned short int int unsigned int long int unsigned long int The leftmost bit of a signed integer (sign bit) is 0 if the number is +ve or 0, 1 if it is -ve. Thus, the largest 16-bit integer has the binary representation 0111111111111111 which has the value 32,767 (2 15 1). Can store -ve numbers, zero and +ve numbers. Default - signed integers

Unsigned Integers Abbreviations An integer with no sign bit (the leftmost bit is considered part of the number s magnitude) is said to be unsigned. The largest 16-bit unsigned integer is 65,535 (2 16 1). Can store only zero and +ve numbers. Can represent +ve numbers twice as big as the signed types. C allows us to drop the word int from the names of integer data types: short int and long int. E.g. 2: unsigned short int may be abbreviated to unsigned short. long int may be abbreviated to just long. Ranges of Integer Data Types Ranges - 16 bit machines The range of values represented by each of the six integer types varies from one machine to another. However, there are a couple of rules that all compilers must obey: 1. Each must provide the three integer data types: int, short and long. 2. int must not be shorter than short, and long must not be shorter than int. Note 1: However, it is possible to have the range of short may be the same as int, and the range of int same as long. Note 2: short int and int have identical ranges.

Ranges - 32 bit machines Ranges - 64 bit machines Note 3: int and long int have identical ranges. Some Integer Declarations Integer Constants E.g. 3: i n t myint ; // int unsigned s h o r t i n t age ; // unsigned short int unsi gned s h o r t numofchildren ; // unsigned short int unsi gned l o n g i n t worldpopulation ; // unsigned long int l o n g likecounter ; // long int Numbers that appear in a program (not numbers that are read, written, or computed). 12 3568l 589L 100U By default, integer constants are stored as int. To force the number stored as other formats: Put l or L at the end of the number to treat it as long (E.g. 15L). Put u or U to indicate the number is unsigned (E.g. 15u).

Integer Overflow Which Integer Type? With the richness of C integer types, which integer type should we use? When arithmatic operations are performed on integers, it is possible that the result will be too large to represent. E.g. 4: The result of the sum of two ints may exceed the maximum of ints. Such a situation is called as an integer overflow. Generally, int variables work more efficiently. If there is no compelling reason to choose another type, use int. If a variable never holds a negative number, use an unsigned type. E.g. No of words in a document Variable can store higher values. If we know that the variable may have to represent larger integer values, use long. If a number is too large to be represented as type long, go for a floating-point type. Reading and Writing Integers 1. When reading or writing an unsigned integer, use the letter u instead of d in the conversion specification. E.g. 5: unsigned i n t i; scanf ( %u, &i); printf ( %u, i); 2. When reading or writing an short integer, put the letter h in front of d. E.g. 6: s h o r t s; scanf ( %hd, &s); printf ( %hd, s); 3. When reading or writing an long integer, put the letter l in front of d. E.g. 7: l o n g l; scanf ( %l d, &l); printf ( %l d, l); Floating Types Use to represent numbers with a fractional part, or numbers that are exceedingly large or small. E.g. 8: 12.34, 0.23, 8.0, 3.45E6, 2.52e+8, 8.33E-4 C provides three floating types: float - Single-precision floating-point double - Double-precision floating-point long double - Extended-precision floating-point float is suitable when the precision is not critical. E.g. 9: Calculating the average rainfall to two decimal point double - gives a greater precision long double - gives the ultimate in precision

Ranges and Precision - Floating Types float vs. double On computers that follow IEEE standards, the following ranges and precisions are applied on float and double. Arithmetic operations slow on double and long double variables. Need more bytes to store double variables (see Bytes of memory column in the previous slide). Calculations are more accurate in double variables (see digits of precision column in the previous slide). Floating Constants Reading and Writing Floating-Point Numbers Floating constants can be written in a variety of ways. 57.0 57. 57.0e0 57E0 5.7e1 5.7e+1.57e+2 570e-1 By default, floating constants are stored as a double number. On occasion, it may necessary to store a floating number in float or long double format: To indicate single precision is required, put f or F at the end of the constant (E.g. 57.0f or 57.0F) To indicate extended precision is required, put l or L at the end of the constant (E.g. 57.0l or 57.0L) 1. When reading a value of double, put the letter l in front of e,f or g: double d; scanf ( %l f, &d); Note 4: Use l only in a scanf format string, not a printf string. In a printf format string, the e, f, and g conversions can be used to write either float or double values. 2. When reading or writing a value of type long double, put the letter L in front of e, f or g: l o n g double ld; scanf ( %Lf, &ld); printf ( %Lf, ld);

Character Types Reading and Writing Characters Designed to store characters, such as letters and digits. In C, characters are stored in char variables. The char type is really another integer type. A char type variable, stores the corresponding ASCII value of a character. char ch = M ; // Stores the ASCII value 77 of M in ch The %c conversion specification allows scanf and printf to read and write single characters: char ch; scanf ( %c, &ch); // reads a single character printf ( %c, ch); // prints the character stored in ch Note 5: scanf doesn t skip white-space characters before reading a character. To force scanf to skip white-space before reading a character, put a space before %c. scanf ( %c, &ch); // skips white - space, then reads ch Quizs getchar and putchar [1] Quiz 1: Write the output of the following program segment: char ch; i n t i = 97; ch = i; printf ( %d\n, i); printf ( %c \n, ch); ch = i + 1; printf ( %d\n, i); printf ( %c \n, ch); ch = ch + 1; i = ch; printf ( %d\n, i); printf ( %c \n, ch); Quiz 2: Write the output of the following for loop: f o r ( char ch = A ; ch <= Z ; ch ++) printf ( %c, ch); C provides getchar and putchar functions to read and write characters respectively. putchar writes a single character: char ch = G ; putchar (ch); getchar function reads a character and returns it. In order to save this character, we must assign the return character to a char variable. char ch; ch = getchar (); // reads a character and stores it in ch

getchar and putchar [2] Exercise Note 6: getchar returns an int value rather than a char value. Quiz 3: Write the output of the following code: char ch; i n t i; i = getchar (); ch = i; printf ( %d\n, i); printf ( %c \n, ch); Ex. 1: Write a C program to calculate the length of a message that is entered by the user. Use getchar function to read characters. Note 7: getchar and putchar save time when the program is executed. They are much simpler than scanf and printf. They are implemented as macros for additional speed. Type Conversion [1] Type Conversion [2] For a computer to perform an arithmetic operation, 1. the operands must usually be of the same size (the same number of bits), and 2. they must be stored in the same way. But in C, allows the basic types to be mixed in expressions. Thus, the C compiler should generate instructions to convert some operands to different types so that the hardware will be able to evaluate the expression. For example, if the program adds a 16-bit short and a 32-bit int, the compiler will arrange a temporary 32-bit int to store the value stored in 16-bit short and then adds the 32-bit int and the temporary 32-bit int. This is called automatic type conversion (or implicit conversion).

Real Story - Automatic Type Conversion When... Automatic Type Conversions? i n t count = 7; f l o a t avgweight = 155.5; double ttlweight = count * avgweight ; Automatic type conversions are performed in the following situations: 1. When the operands in an arithmetic or logical expression don t have the same type. 2. When the type of the expression on the right side of an assignment does not match the type of variable on the left side. 3. When the type of an argument in a function call does not match the type of the corresponding parameter. 4. When the type of the expression in a return statement does not match the function s return type. Order - Automatic Type Conversions When the compiler confronts mixed-typed expressions, types are considered as higher or lower, based roughly on the below table. Data Type long double double float unsigned long int long int unsigned int int short, char Order Highest Lowest The lower-type variable is converted to the type of the higher-type variable. Casting (Explicit Conversion) Sometimes a programmer needs to convert a value from one type to another in a situation where the compiler will not do it automatically. E.g. 10: f l o a t x = 10/3; // x = 3.0 To get the correct result, need to cast one of the operand. f l o a t x = 10.0/3; E.g. 11: i n t ttlmarks = 556, noofstds = 10; f l o a t avgmark = ttlmarks / noofstds ; // avgmark = 55.0 To get the correct result, need to cast one of the operand. f l o a t avgmark = ( f l o a t ) ttlmarks / noofstds ;

Avoiding Overflows with Casting The sizeof Operator Consider the following code: l o n g i; i n t j = 1000; i = j* j; // overflow may occur The value of j*j is 1,000,000, and i is a long, and it seems that the above statement is correct. The problem of that statement is when multiply two int values, the result will have int type. But j*j is too large to represent as an int on some machines, causing an overflow. We can use casting to avoid this overflow problem: i = ( l o n g )j * j; Quiz 4: Will the following statement avoid the above overflow problem? The sizeof operator allows a program to determine how much memory is required to store values of a particular type. The value of the expression sizeof (type name) is an unsigned integer representing the number of bytes required to store a value belonging to type name. For example, on a 32-bit machine, is normally 4. sizeof (int) i = ( l o n g )(j * j);