Integer Data Types. Data Type. Data Types. int, short int, long int

Similar documents
! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

LECTURE 02 INTRODUCTION TO C++

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

The C++ Language. Arizona State University 1

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

Chapter 2: Introduction to C++

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

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

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

CSc Introduction to Computing

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

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

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

Objectives. In this chapter, you will:

Variables and Constants

Chapter 2: Basic Elements of C++

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++ Objectives. Objectives (cont d.) A C++ Program. Introduction

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

Exercise: Using Numbers

Lecture 2 Tao Wang 1

Your first C++ program

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

Lab # 02. Basic Elements of C++ _ Part1

6.096 Introduction to C++ January (IAP) 2009

CHAPTER 3 BASIC INSTRUCTION OF C++

CS242 COMPUTER PROGRAMMING

Exercise: Inventing Language

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

REVIEW. The C++ Programming Language. CS 151 Review #2

Types, Values, Variables & Assignment. EECS 211 Winter 2018

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

IT 1033: Fundamentals of Programming Data types & variables

UNIT- 3 Introduction to C++

Computer Programming : C++

Fundamentals of Programming CS-110. Lecture 2

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Exercise 1.1 Hello world

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

Syntax and Variables

Variables. Data Types.

Getting started with C++ (Part 2)

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

BITG 1233: Introduction to C++

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

CS1500 Algorithms and Data Structures for Engineering, FALL Virgil Pavlu, Jose Annunziato,

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

A First Program - Greeting.cpp

Chapter Two MULTIPLE CHOICE

Introduction to Programming EC-105. Lecture 2

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

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Unit 3. Constants and Expressions

Programming. C++ Basics

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

THE FUNDAMENTAL DATA TYPES

Using C++, design an Abstract Data Type class named MyGrades. The class must have the following private members :

car object properties data red travels at 50 mph actions functions containing programming statements Program Program variables functions

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

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Integer Representation Floating point Representation Other data types

Datatypes, Variables, and Operations

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

Review of Important Topics in CS1600. Functions Arrays C-strings

Work relative to other classes

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

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

CMSC 202 Midterm Exam 1 Fall 2015

Chapter 2: Overview of C++

Chapter 2 Basic Elements of C++

BASIC ELEMENTS OF A COMPUTER PROGRAM

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Types, Variables, and Constants

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Introduction to C++ (Extensions to C)

Reserved Words and Identifiers

Main Memory Organization

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

Outline. Data and Operations. Data Types. Integral Types

VARIABLES AND CONSTANTS

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Functions, Arrays & Structs

Relational Operators and if. Class 10

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions.

Fundamental Data Types

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Transcription:

Data Types Variables are classified according to their data type. The data type determines the kind of information that may be stored in the variable. A data type is a set of values. Generally two main (types of) data types: a. Numeric ( integers such as 3, 157, -47 and floating points such 23.7, 0.94 ) b. Character Primary Consideration for selecting a numeric data type are :- a. The largest and the smallest numbers that may be stored in the variable. b. How much memory the variable uses. c. Whether the variable stores signed or unsigned numbers d. The number of decimal places of precision the variable has. Data Types Data Type Represents int, short, long whole numbers ( integers ) float, double real numbers ( fractional, decimal ) bool logical values : true, false char a single character string sequence of chars. Integer Data Types int, short int, long int Whole numbers 2, 1000, -900 May be signed or unsigned Typical sizes and ranges (may vary depending on the system) Literals ( are int by default) Fall 2017 CS 1428 Husain Gholoom Page 1

Integer Data Types Data Type Size Range Short int 2 bytes -32,768 to 32,767 unsigned short int 2 bytes 0 to 65,535 int 4 bytes -2,147,483,648 to 2,147,483,647 unsigned int 4 bytes 0 to 4,294,967,295 Long int 4 bytes -2,147,483,648 to 2,147,483,647 unsigned long int 4 bytes 0 to 4,294,967,295 Long long int 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,808 unsigned long long int 8 bytes 0 to 18,446,744,073,709,551,615 Example of Variable Definitions: short dayofweek; unsigned long distance; int xcoordinate = 10; long deficit = 1500; Fall 2017 CS 1428 Husain Gholoom Page 2

Floating-Point Data Types Used to hold real numbers such as 2.5, -7.8 Typical sizes and ranges (may vary depending on the system): Single Precision float 4 bytes +/- 3.4e +/- 38 (~7 digits) Double Precision double 8 bytes +/- 1.7e +/- 308 (~15 digits) Long Double Precision long double 8 bytes* +/- 1.7e +/- 308 (~15 digits) *some compiler use 10 bytes for long double : the range is Floating-point literals can be represented in +/- 3.4E-4932 and +/- 1.1E4832 Fixed point (decimal) notation: 31.4159 0.0000625 E (scientific) notation: 3.14159E1 6.25e-5 Note : there are no unsigned floating point data types. On all machines, variables of the float, double, and long double data types can store positive or negative numbers. Literals ( default type is double ) can be expressed in a variety of ways :- 31.415E5 // equivalent to 3141500.0 - E or e will work - but printed as e. -31.415e5 // equivalent to -3141500.0 3.1e-4 // equivalent to 0.00031 Floating-Point Data Types float double distance, time; mass; distance = 1.495979E11; // how far away the sun is (in meters) mass = 1.989E30; // how much the sun weighs (in kilograms) time = 12.816; // hours of daylight in San Marcos today, 8/31 Fall 2017 CS 1428 Husain Gholoom Page 3

Converting between floating-points and integers: int i; float f; f = 8.9; i = 8.9; // stores 8 in i ( truncates, does not round ) i = 8; f = 8; // stores 8.0 in f f = 7.9; i = f; // stores 7 in i The bool Data Type Defined as bool Literals: the values are true or false Output: 1 0 bool boolvalue; boolvalue = true; cout << boolvalue << endl; boolvalue = false; cout << boolvalue << endl; bool is a numeric type: true is 1 and false is 0 The char Data Type char Literals: All the keyboard and printable symbols such as 'A' '3' '!' '\n' 'n'. Numeric value of character from the ASCII character set is stored in memory: Output: char letter; letter = 'A'; // 65 is stored in memory letter = '!'; A! Fall 2017 CS 1428 Husain Gholoom Page 4

char is really a numeric type also! Note: 65 is the ASCII code for 'A' Output: A B char letter; letter = 65; letter = 66; The string Data Type A string is a sequence of characters. Requires the string header file: #include <string> Literals: Hello again Over\nThere Y A string is stored sequentially in memory, with the null character ('\0') at the end. The null character is not displayed. To define string variables in programs: string firstname, lastname; To assign literals to variables : firstname = "George"; lastname = "Washington"; To display via cout : sizeof cout << firstname << " " << lastname; sizeof function returns size of a data type in bytes in any system. The result is system-dependent. The argument may be a data type: sizeof(int) // result is 4 on most systems The argument may be a variable: double salary; cout << sizeof(salary); // result is 8 on most systems Fall 2017 CS 1428 Husain Gholoom Page 5

What is the output of the following?? cout << "The size of a short is " << sizeof(short) cout << "The size of an integer is " << sizeof(int) << " bytes.\n"; << " bytes.\n"; Declaring Variables with the auto Key Word The auto word key tells the compiler to determine the variable s data type from the initialization value. auto amount = 100; auto interestrate = 12.5; auto stockcode = X ; The above statements uses auto instead of a data type Scopes of a Variable A variable's scope is the part of the program in which a variable can be accessed. Rule : A variable cannot be used before it is defined. Example :- #include <iostream> using namespace std; int main () { value = 150; //error, use of value before it is defined int value; cout << value; } Fall 2017 CS 1428 Husain Gholoom Page 6

Named Constants Variable whose value cannot be changed during program execution Literals do not have meaningful names cost = price + (price *.0825); what is the meaning of.0825? Same literal may be used throughout a program, but may want to change it later. Maybe.0825 occurs in dozens of places in the code. Search and replace problem. Literals may be given names to be used in their place. General Form: const data_type VARIABLE = value; For Example o const double SALES_TAX_RATE =.0825; Then the equation will be cost = price + (price * SALES_TAX_RATE); const makes the variable read-only Initialization required All-caps for the name of the constant is just a convention Fall 2017 CS 1428 Husain Gholoom Page 7