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

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

LECTURE 02 INTRODUCTION TO C++

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

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

Chapter 2: Introduction to C++

Variables and Constants

CSc Introduction to Computing

Lecture 2 Tao Wang 1

The C++ Language. Arizona State University 1

THE FUNDAMENTAL DATA TYPES

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

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

Week 3 Lecture 2. Types Constants and Variables

Exercise: Using Numbers

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

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

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

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 3. Fundamental Data Types

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

BITG 1233: Introduction to C++

CS102: Variables and Expressions

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

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

Fundamentals of Programming

file://j:\macmillancomputerpublishing\chapters\in100.html 3/22/01

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

INTRODUCTION TO COMPUTER SCIENCE - LAB

Introduction to C. Systems Programming Concepts

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

CHAPTER 3 BASIC INSTRUCTION OF C++

BASIC ELEMENTS OF A COMPUTER PROGRAM

6.096 Introduction to C++ January (IAP) 2009

IT 1033: Fundamentals of Programming Data types & variables

VARIABLES AND CONSTANTS

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Computer System and programming in C

CS149: Elements of Computer Science. Fundamental C++ objects

BSM540 Basics of C Language

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

Fundamentals of Programming CS-110. Lecture 2

Creating a C++ Program

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

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

DEPARTMENT OF MATHS, MJ COLLEGE

Basic Types and Formatted I/O

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

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

Fundamental of Programming (C)

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

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

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

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

Princeton University COS 217: Introduction to Programming Systems C Primitive Data Types

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

Programming and Data Structures

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

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

Objectives. In this chapter, you will:

UNIT- 3 Introduction to C++

Programming. Data Structure

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Getting started with Java

Work relative to other classes

C Language, Token, Keywords, Constant, variable

CS242 COMPUTER PROGRAMMING

Programming Fundamentals and Methodology. COMP104: C++ Basics

Week 1 / Lecture 2 8 March 2017 NWEN 241 C Fundamentals. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

Fundamentals of C Programming

Syntax and Variables

BTE2313. Chapter 2: Introduction to C++ Programming

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

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Getting started with C++ (Part 2)

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso

Outline. Data and Operations. Data Types. Integral Types

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

Introduction to Programming EC-105. Lecture 2

Basic Elements of C. Staff Incharge: S.Sasirekha

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Number Systems, Scalar Types, and Input and Output

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

Variables and Literals

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

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

Expressions and Variables

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

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Datatypes, Variables, and Operations

Basic data types. Building blocks of computation

ECET 264 C Programming Language with Applications

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Data Types and Variables in C language

Transcription:

CS 150 Introduction to Computer Science I Data Types Today Last we covered o main function o cout object o How data that is used by a program can be declared and stored Today we will o Investigate the various types of data that C++ can handle 9/6/06 CS150 Introduction to Computer Science 1 1 9/6/06 CS150 Introduction to Computer Science 1 2 Declaration Statements Examples of declaration statements const double PI = 3.14; const double RADIUS = 5.4; double circ; With the above statements we are declaring four things o PI to store the value of Pi that never changes o RADIUS to store the value of radius that never changes o area to store the area of the circle o circ to store the circumference of the circle Declaration Statements Variable declarations o Allocate space for data to be used in the program o The data can be changed double circ; Constant declaration o Allocate space for data that cannot be changed const double PI = 3.14; const double RADIUS = 5.4; 9/6/06 CS150 Introduction to Computer Science 1 3 9/6/06 CS150 Introduction to Computer Science 1 4 Variable Declaration Variables are declared by stating o Type of data (data type) o Name to identify the variable (identifier) o Semicolon (;) data-type identifier; Variable Declaration If there is more than one variable of a single data type then you o State the data type o List the variable identifiers (names) separated by commas o Semicolon data-type identifier1, identifier2; double area, circ; 9/6/06 CS150 Introduction to Computer Science 1 5 9/6/06 CS150 Introduction to Computer Science 1 6 1

Identifiers Programmer-defined names that represent some element of a program C++ does place limits on what names you can call your variables Rules 1. Identifiers must begin with a letter or an underscore 2. Identifiers must consist of letters, numbers and underscore, nothing else 3. Identifiers cannot be a reserved keyword Reserved Keywords These are words that are reserved by C++ to implement various features Examples of keywords that we have seen so far are int, double, const, return A list of C++ keywords can be found on page 45 of your textbook 9/6/06 CS150 Introduction to Computer Science 1 7 9/6/06 CS150 Introduction to Computer Science 1 8 Identifiers Identifiers Identifiers are case sensitive o int num1; o int Num1; o num1 and Num1 are different variables You should always try to use meaningful variable names If you have a variable that represents the width, then call it width not w 9/6/06 CS150 Introduction to Computer Science 1 9 Q 4.1 Which of the following declarations are invalid and why? a. Letter1; b. 1letter; c. double inches, kms; d. double inches*num; e. int joe s; f. Int cent_per_inch; g. double two-dimensional; h. hello; i. int return; j. size int; 9/6/06 CS150 Introduction to Computer Science 1 10 Data types Data types o C++ can store many different types of data o A data type also defines what operations can be performed on data of that type o We will be looking at Integer numbers Characters Strings Floating-point numbers Booleans Integers The main integer data type is int The int data type is used to store integer numbers, both positive and negative ints are finite (why?), i.e. they have a limited range that is implementation dependent Examples of ints are: 123, -23, 0, 2352 An int without a sign (+ or - ) is assumed to be positive 2,353 is not an int, 2353 is an int What operations can be performed on integers? 9/6/06 CS150 Introduction to Computer Science 1 11 9/6/06 CS150 Introduction to Computer Science 1 12 2

Integer Data Types There are five integer data types, each with a different range and a different size The data type is used to store single acters (letters, digits, special acters) s are usually 1-byte long Characters are stored as integers The most common method for encoding acters is ASCII Character constants are enclosed in single quotes Range of data types is listed on page 48 Examples of acter constants are: A, a, *, 2, $ 9/6/06 CS150 Introduction to Computer Science 1 13 9/6/06 CS150 Introduction to Computer Science 1 14 Program 4.1 Program 4.2 letter; letter; letter = 65; letter = 66; letter = 'A'; letter = 'B'; 9/6/06 CS150 Introduction to Computer Science 1 15 9/6/06 CS150 Introduction to Computer Science 1 16 Character constants can only hold a single acter String constants are used to store a series of acters To indicate the end of a string, a null terminator is used Questions Q 4.2 How are the acter A and the string constant A stored in memory? Q 4.3 Is the escape acter \n a acter or a string? Q 4.4 How do we declare a variable and assign it a value? 9/6/06 CS150 Introduction to Computer Science 1 17 9/6/06 CS150 Introduction to Computer Science 1 18 3

string Class string is the data type used to store more than one acter Not built into C++ but provided by standard C++ Need to include the preprocessor directive o #include <string> string Questions Q 4.5 How do we declare a variable of type string? Q 4.6 How do we assign a value to the variable? Q 4.7 How do we output a string constant and a string variable? What is output? 9/6/06 CS150 Introduction to Computer Science 1 19 9/6/06 CS150 Introduction to Computer Science 1 20 Floating-Point Data Types The float, double, and long double data types are used to store floating-point numbers, both positive and negative Floating-point numbers can contain fractional parts Computers store floating-point numbers in a manner similar to scientific notation Computers represent floating-point numbers using E notation Floating-Point Data Types float, double, and long double are finite Examples of floating-point numbers are: 1.0, -2.3, -.3, 12E5, -1E-2, 1.4e+8 2,353.99 is not a double, 2353.99 is a double 9/6/06 CS150 Introduction to Computer Science 1 21 9/6/06 CS150 Introduction to Computer Science 1 22 How to Choose a Data Type Ask yourself the following questions o What are the largest and smallest numbers that may be stored? o How much memory does the variable use? o Is the variable signed (positive and negative)? o How many decimal places of precision does the variable need? Variable Sizes On my machine the sizes are 9/6/06 CS150 Introduction to Computer Science 1 23 9/6/06 CS150 Introduction to Computer Science 1 24 4

Variable Size Program Variable Ranges Type Size Values int 2,147,483,648 to 2,147,483,647 short int 2 bytes 32,768 to 32,767 cout << "The size of an int is:\t\t" << sizeof(int) << " bytes.\n"; cout << "The size of a short int is:\t" << sizeof(short) << " bytes.\n"; cout << "The size of a long int is:\t" << sizeof(long) << " bytes.\n"; cout << "The size of a is:\t\t" << sizeof() << " bytes.\n"; cout << "The size of a float is:\t\t" << sizeof(float) << " bytes.\n"; cout << "The size of a double is:\t" << sizeof(double) << " bytes.\n"; long int unsigned int 1 byte 2,147,483,648 to 2,147,483,647 0 to 4,294,967,295 256 acter values float double 8 bytes 1.2e 38 to 3.4e38 2.2e 308 to 1.8e308 9/6/06 CS150 Introduction to Computer Science 1 25 9/6/06 CS150 Introduction to Computer Science 1 26 Summary In today s lecture we covered o Identifiers o Data types o How data that is used by a program can be declared and stored We have covered p. 45-63 of your textbook 9/6/06 CS150 Introduction to Computer Science 1 27 5