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

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

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

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

C++ Language Tutorial

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

Number Representation

Chapter One Introduction to Programming

Answers to Review Questions Chapter 7

Comp151. Definitions & Declarations

Passing 1D arrays to functions.

An Incomplete C++ Primer. University of Wyoming MA 5310

The programming language C. sws1 1

Informatica e Sistemi in Tempo Reale

Pemrograman Dasar. Basic Elements Of Java

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

MS ACCESS DATABASE DATA TYPES

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

How To Write Portable Programs In C

Sequential Program Execution

Negative Exponents and Scientific Notation

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

DNA Data and Program Representation. Alexandre David

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).

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

Basics of I/O Streams and File I/O

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

Chapter 5. Selection 5-1

C++ INTERVIEW QUESTIONS

- Hour 1 - Introducing Visual C++ 5

Illustration 1: Diagram of program function and data flow

Computer Science 281 Binary and Hexadecimal Review

Recognizing PL/SQL Lexical Units. Copyright 2007, Oracle. All rights reserved.

VB.NET Programming Fundamentals

The C Programming Language course syllabus associate level

Appendix K Introduction to Microsoft Visual C++ 6.0

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

Oracle Internal & Oracle Academy

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Member Functions of the istream Class

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

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

1. The First Visual C++ Program

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

C++ Programming Language

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Formatting Numbers with C++ Output Streams

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Numbering Systems. InThisAppendix...

Introduction to Python

Java Interview Questions and Answers

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

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Ch 7-1. Object-Oriented Programming and Classes

Chapter 2 Introduction to Java programming

Java Basics: Data Types, Variables, and Loops

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

CS 106 Introduction to Computer Science I

Chapter 2 Elementary Programming

The Answer to the 14 Most Frequently Asked Modbus Questions

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Conditions & Boolean Expressions

Programming languages C

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

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

Programming Languages CIS 443

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Chapter 4: Computer Codes

Module 816. File Management in C. M. Campbell 1993 Deakin University

Variables, Constants, and Data Types

Instruction Set Architecture (ISA)

Lecture 5: Java Fundamentals III

Introduction to Java. CS 3: Computer Programming in Java

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Introduction to Java

Utility Software II lab 1 Jacek Wiślicki, jacenty@kis.p.lodz.pl original material by Hubert Kołodziejski

Binary storage of graphs and related data

Field Properties Quick Reference

Stacks. Linear data structures

C++ Essentials. Sharam Hekmat PragSoft Corporation

Chapter 7D The Java Virtual Machine

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

PIC 10A. Lecture 7: Graphics II and intro to the if statement

Computer Science 217

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

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

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

The use of binary codes to represent characters

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

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

Chapter 3 Review Math 1030

Object Oriented Software Design II

C PROGRAMMING FOR MATHEMATICAL COMPUTING

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

Java Crash Course Part I

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

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 2016 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 2016 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 2016 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 2016 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 2016 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 2016 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 2016 CS 1428 Husain Gholoom Page 7