Definition: Data Type A data type is a collection of values and the definition of one or more operations on those values.

Similar documents
Data Types. 9. Types. a collection of values and the definition of one or more operations that can be performed on those values

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

Chapter 7. Additional Control Structures

Types, Operators and Expressions

Types, Operators and Expressions

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

A flow chart is a graphical or symbolic representation of a process.

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CS2141 Software Development using C/C++ C++ Basics

File Handling in C. EECS 2031 Fall October 27, 2014

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Information Science 1

CHAPTER 3 Expressions, Functions, Output

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Chapter 7. Expressions and Assignment Statements ISBN

Data Types H&K Chapter 7. Instructor - Andrew S. O Fallon CptS 121 (October 17, 2018) Washington State University

Operators in C. Staff Incharge: S.Sasirekha

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Arithmetic Operators. Portability: Printing Numbers

Fundamentals of Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

UNIT- 3 Introduction to C++

Expressions & Assignment Statements

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

EEE145 Computer Programming

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

More Programming Constructs -- Introduction

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Introduction. Structures, Unions, Bit Manipulations, and Enumerations. Structure. Structure Definitions

OBJECT ORIENTED PROGRAMMING USING C++

Propedéutico de Programación

C Programming Language (Chapter 2 of K&R) Variables and Constants

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Java enum, casts, and others (Select portions of Chapters 4 & 5)

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

A First Program - Greeting.cpp

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Part I Part 1 Expressions

Programming for Engineers Iteration

Chapter 2. Elementary Programming

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

Full file at

Software II: Principles of Programming Languages. Why Expressions?

Chapter 7. Expressions and Assignment Statements

Chapter 7. Expressions and Assignment Statements ISBN

1.3b Type Conversion

Chapter 7. Expressions and Assignment Statements

C Structures, Unions, Bit Manipulations, and Enumerations

Operator overloading: extra examples

Syntax and Variables

Fundamental of Programming (C)

Computer System and programming in C

Introduction to Programming

Expressions and Assignment Statements

Review of the C Programming Language for Principles of Operating Systems

Chapter 3: Operators, Expressions and Type Conversion

Programming for Engineers Structures, Unions

Operators and Expressions:

CSC 1214: Object-Oriented Programming

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

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

Chapter 9 Technicalities: Classes, etc. Walter C. Daugherity Lawrence Pete Petersen Bjarne Stroustrup Fall 2007

JAVA OPERATORS GENERAL

Informatics Ingeniería en Electrónica y Automática Industrial

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

OBJECT ORIENTED PROGRAMMING

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Functions and Recursion

Objectives. In this chapter, you will:

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Operators and Expressions

EP241 Computer Programming

Type Definition. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

3. Java - Language Constructs I

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

Chapter 3. Numeric Types, Expressions, and Output

Chapter 9 Technicalities: Classes, etc.

Flashback Technicalities: Classes, etc. Lecture 11 Hartmut Kaiser

C++ Programming: From Problem Analysis to Program Design, Third Edition

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

1. In C++, reserved words are the same as predefined identifiers. a. True

Chapter 9 Technicalities: Classes, etc.

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

Type Conversion. and. Statements

Chapter 7 Arithmetic

Lecture 02 C FUNDAMENTALS

A Fast Review of C Essentials Part I

Infix to Postfix Conversion

High Performance Computing

Operators. Java operators are classified into three categories:

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

Information Science 1

Transcription:

Data Types 1 Definition: Data Type A data type is a collection of values and the definition of one or more operations on those values. Base Data Types All the values of the type are ordered and atomic. C++: short int, int, long int, float, double, long double, char User-defined Types All the values of the type are ordered and atomic. C++: determined by the user Aggregate Data Types Complex combinations of base data types and user-defined types. C++: arrays, structures

Enumerated Types 2 Enumerated types list all the valid values for a type. enum Season { WINTER, SPRING, SUMMER, FALL}; enum Hemisphere { NORTH, SOUTH, EAST, WEST}; Season period; Hemisphere region; period = SUMMER; region = EAST; Enumerated types are, by definition, integer types whose values start with 0, unless otherwise specified, and are incremented by 1. enum Months {JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; enum Coin {PENNY = 1, NICKEL = 5, DIME = 10}; enum Boolean {FALSE = 0, TRUE = 1}; //! enum Boolean {TRUE, FALSE };

Enumerated Types (cont) 3 They provide a mechanism for abstraction of real world entities. Enumerated type variables do not contain the character string of the value. The internal representation uses int values; it is bad form to rely on those values. Values of a given enumerated type must be unique. Identifier names of enumerated types must be unique, cannot be used in other enumerated types. Enumerated types cannot be read or (usefully) written directly. Enumerated data types make the program more readable by providing a way to incorporate meaningful labels (thus easier to debug) and they help in selfdocumenting the program.

Enumerated Types Example 4 enum ParityType { EVEN, ODD }; // file-scoped type definition ParityType FindParity(int N); void main() { int k; ParityType kparity; cout << Please enter an integer: ; // get an integer cin >> k; kparity = FindParity(k); // determine its parity } if (kparity == EVEN) // can t usefully print cout << That was even. << endl; // the value of kparity else // directly cout << Hmmm... that was odd. << endl; ParityType FindParity(int N) { } if (N % 2 == 0) return EVEN; else return ODD;

Type Coercion 5 C++ performs implicit type conversion whenever values of different types are used in: - arithmetic and relational expressions - assignment operators - parameter passage - return of a function value from a value-returning function Promotion (widening): conversion from a "lower" type to a "higher" type, e.g. Demotion (narrowing): conversion from a "higher" type to a "lower" type, e.g.

Compound Operators 6 Compound operators: Allow arithmetic operations and assignment operation in one statement, e.g. y += x; means y -= x; means y *= x; means y /= x; means The compound operators provide a convenient contraction that saves typing, but may reduce code readability.

Binary Shift Operators Shift operators are only for integer operands. 7 The shift operator shifts the bits of the operand in the indicated direction, inserting zeros at the opposite end. The effect of a shift to the left (<<) is multiplication by a power of 2 The effect of a shift to the right (>>) is division by a power of 2. Suppose someint = 32, then result = (someint << 2); // result = 32 * 2^2 = 128 // shifts 0 s from the right result = (someint >> 3); // result = 32 / 2^3 = 4 // shifts 0 s from the left // machine dependent

Bitwise Operators 8 Given: int someint1 = 5, // someint1 == 0000 0000... 0000 0101 someint2 = 3 ; // someint2 == 0000 0000... 0000 0011 Bitwise AND: someint1 & someint2 == Bitwise OR: someint1 someint2 == Bitwise exclusive OR: someint1 ^ someint2 == Complement: ~someint1 ==

Cast Operators 9 Cast operators allow the explicit changing of the data type of expressions, e.g. ratio = float (a) /b; // functional notation ratio = (float) a / b; // prefix notation -use // typecasting expressions Cast operators are available for any built-in data type. It is generally better to use an explicit typecast rather than rely on the invisible coercion mechanisms in C++. For instance, given the declarations int m; double x = 5.0, y = 2.0; write m = int(x / y) rather than m = x / y The former makes the automatic truncation explicit, and indicates the programmer is aware that the truncation will occur.

Other Operators 10 sizeof operator: yields the size in bytes of its operand, e.g. sizeof(someint) returns the size in bytes of the storage allocated for someint, which would be 4 on most contemporary systems. Conditional ternary operator: expr1? expr2 : expr3; is a contracted form of an if..else statement with assignments. It provides brevity at the expense of code readability. Nparity = (N % 2 == 0)? EVEN : ODD; if (N % 2 == 0) Nparity = EVEN; else Nparity = ODD;

Typedef 11 The typedef statement is used to allow the programmer to custom tailor his/her types by using pre-existing types in a way that makes her/his types more meaningful. E.g.: typedef predefined_type newdefinedtype ; const int MaxNameLength = 30; typedef char Name[MaxNameLength]; Name myname = Fred Flintstone ; Name yourname = Mud ; cout << myname << endl; Typedefs allow the programmer to map existing data types to a problem-specific concept. Good use of typedefs can improve data organization and program efficiency. Correct application of typedefs aid in the production of self-documenting code.