Programming in C++ 5. Integral data types

Similar documents
Operators and Expressions:

Operators & Expressions

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

Operators in C. Staff Incharge: S.Sasirekha

DEPARTMENT OF MATHS, MJ COLLEGE

Computer System and programming in C

SECTION II: LANGUAGE BASICS

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

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

Chapter 4. Operations on Data

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

Operators. Java operators are classified into three categories:

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Chapter 2: Using Data

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

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

Programming in C++ 6. Floating point data types

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

JAVA OPERATORS GENERAL

Fundamentals of Programming CS-110. Lecture 3

C/C++ Programming for Engineers: Working with Integer Variables

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

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

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

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

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

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

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Basics of Programming

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

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Chapter 4: Basic C Operators

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

Lecture 3. More About C

Computer Programming CS F111

Data Types and Variables in C language

UNIT- 3 Introduction to C++

Arithmetic Operators. Portability: Printing Numbers

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Fundamentals of Programming

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Lecture 3 Tao Wang 1

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

LECTURE 3 C++ Basics Part 2

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Week 4 Lecture 1. Expressions and Functions

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

More Programming Constructs -- Introduction

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

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Differentiate Between Keywords and Identifiers

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

Expressions and Precedence. Last updated 12/10/18

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

ANSI C Programming Simple Programs

Visual C# Instructor s Manual Table of Contents

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

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

Information Science 1

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Computers Programming Course 6. Iulian Năstac

Chapter 3 Structure of a C Program

3. Java - Language Constructs I

Expressions and Casting

ECE 122 Engineering Problem Solving with Java

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Fundamental of Programming (C)

CS102: Variables and Expressions

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

C Programming Language. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

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

The Arithmetic Operators

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

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

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

A Java program contains at least one class definition.

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

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

UNIT 3 OPERATORS. [Marks- 12]

Variables and Operators 2/20/01 Lecture #

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

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

Operators in java Operator operands.

1.3b Type Conversion

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 3: Operators, Expressions and Type Conversion

BASIC ELEMENTS OF A COMPUTER PROGRAM

CMPT 125: Lecture 3 Data and Expressions

Transcription:

Programming in C++ 5. Integral data types! Introduction! Type int! Integer multiplication & division! Increment & decrement operators! Associativity & precedence of operators! Some common operators! Long & short integers! Unsigned integers! Character types! Summary 1

Introduction C++ is a typed language - the variables and the constants that occur in a program each have a type which controls how they may be used and what storage is required. The language-defined types which are defined to represent integers, characters and floating point numbers are known as fundamental types. It is also possible to define what are known as derived types in order to manipulate objects such as matrices, complex numbers etc. The motivation for using a typed language is that the compiler can catch many programming errors by performing type-checking. For example if an object is declared to be constant (by using const keyword) than an attempt to change its value is a compile-time error. The integral data types consist of two groups: the integer and character types. 2

Type int The int is the most important of the integral data types and can hold both positive and negative integers. If we wish to use variables k, l, m they can be defined by: int k, l, m; Defining an object not only specifies the type but also reserves the appropriate amount of memory. We can also make an declaration e.g. extern int k; without reserving any memory. In a complete program there must be one and only one definition of an object even though there can be many declarations. An object must be defined or declared before it can be used in a program, otherwise a compile-time error occurs. The operator = is used for assignment. This is rather assignment than equality operator! 3

Type int k = m + n; // sum of m and n is assigned to k k + 3 = 7; // WRONG!!! even looks mathematically OK k = k + l; // in mathematics OK only if l=0 k := 10; // := does not exist in C++ Since the int type occupies a fixed amount of memory only a limited amount of integers can be stored. Some operations can give results above the maximum amount or below the minimum value. Such invalid operations are called integer overflow or integer underflow respectively. INT_MAX and INT_MIN are the maximum and minimum values that can be stored by the int type. They are defined in the header file <limits.h>. These values depend on the particular compiler. 4

Integer multiplication & division Integer multiplication is denoted by the token *. Integer division is denoted by forward slash /. Integer division with both numbers positive always truncates! The same is true when both numbers are negative. If only one number is negative the result depends on compiler. Integer modulus or remainder is denoted by % token. If k and l are both positive then k % lgives the remainder obtained on dividing k by l. If both k and l are negative the result is negative. If only one value is negative the result is dependent on the C++ compiler. k = (k / l) * l + k % l 5

Increment & decrement operators The value of a variable can be changed without using = operator. k = k + 1; ++k; k++; // prefix increment operator // postfix increment operator The prefix operator increments k by one before it is used. The postfix operator increments after the value of k has been used. In many cases the result depends on pre or postfix character of ++. int i, j, k, l, m, n; i =2; j=2; k=3; m = i++ / k; // assigns 0 to m n = ++j / k; // assigns 1 to n There are also prefix and postfix decrement operators. 10++; // WRONG!!! constant cannot be incremented --3.14 // WRONG!!! and incremented too 6

Associativity & precedence of operators We have learnt how to use quite a few operators: = + - * / % ++ -- The =, *, /, % tokens are binary operators. It means that they are defined for an operand on each side of the operator. The ++, -- are both unary operators. They require a single operand. The - operator can be both a binary or an unary operator: k = i - j; k = - j; Operator precedence controls which operator in an expression is applied first. The associativity determines the order in which operators with the same precedence are applied. The compiler first uses operator precedence to determine the order of evaluation in an expression and any remaining ambiguities are removed by using operator associativity. 7

Some common operators Operator Name Associativity ++ increment right to left -- decrement right to left - unary minus right to left * multiplication left to right / division left to right % modulus left to right + addition left to right - subtraction left to right = assignment right to left += -= *= /= %= 8

Long & short integers Long integers have the same properties as integers - typically four bytes (the minimum acceptable for the ANSI and ISO C standard). It is rarely common for a compiler to define both int and long data types to have the same four byte representation. long int m; long n; The suffix L or l is used to distinguish a long constant. long k = 7L; Short integer type has the same properties as int except that a particular C++ compiler should not use for it more bytes than are used for int. This type can be useful for saving memory, but if the representation is less than the natural size suggested by the hardware there may be a performance penalty. Keyword short is used to define short integers. short int m; short n; 9

Unsigned integers The short, int and long types all have corresponding unsigned types. They occupy the same amount of memory as the signed type and they obey the laws of arithmetic modulo 2 n. unsigned addition does not overflow and subtraction does not underflow, they simply wrap around. unsigned int k; // suppose represented by 2 bytes k = 65535; // max. unsigned number for 2 bytes k = k + 1; // the result is 0!!! k = 1-2; // the result is 65535 It is not usually worth using an unsigned rather than signed integral type just to gain a higher upper limit on the positive integers that can be stored. A U or u is used in any combination with L or l to denote an unsigned constant. unsigned int k = 1U; unsigned long m = 2UL; 10

Character types The type char is represented by a sufficient number of bytes to hold any character belonging to the character set for the particular compiler. This representation is usually one byte (the minimum allowed by ANSI C standard). C++ distinguishes three character types: char unsigned char signed char The signed char and unsigned char data types obey the same rules of arithmetic as their integer counterparts. If the type is simply specified as char then it is compiler dependent as to whether or not the higher bit is treated as a sign bit. The char types are usually used to hold characters. char c1, c2; c1 = A ; c2 = 44; 11

Summary! The integral types are short, int, long! The character types are char, signed char, unsigned char! Do not forget about basic integral operators together with their associativity and precedence. 12