Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. More on Handling Basic Data Types Mixed types of data Data.

Slides:



Advertisements
Similar presentations
Fundamentals of Computer and programming in C
Advertisements

C Programming Lecture 23 Enumeration Types Structures.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
ENUMERATED, typedef. ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name.
Numeric Types, Expressions, and Output ROBERT REAVES.
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
CS-212 C++ I/O Dick Steflik. C++ I/O Modeled after UNIX’s concept of a “stream” –conceptionally a stream is a continuous flow of characters/bytes from.
Lab 10 rRepresentation And Conversion of Numeric Types l Difference between Numeric Types l Automatic conversion of Data types l Explicit Conversion of.
1 Lab Session-III CSIT121 Fall 2000 Setting Your Own Paths Setting Project Information Browse Information Generation Data types revisited Enumerated data.
計算機概論實習 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Primitive Data Types: Numbers Strings Ordinal Types Pointers
Saturday May 02 PST 4 PM. Saturday May 02 PST 10:00 PM.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Basic Data Type and calculations Data types in C++ Program.
C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.
Lecture 19: Simple Data Types. 2 Lecture Contents: t Representation and conversion of numeric types t Representation and conversion of type char t Enumerated.
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Bit Manipulation when every bit counts. Questions on Bit Manipulation l what is the motivation for bit manipulation l what is the binary, hexadecimal,
Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.
Instructor - C. BoyleFall Semester
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Ivor Horton, Beginning ANSI C++: The Complete Language, 3 rd.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Definition Various stream manipulators can be used to specify the kinds of formatting to be performed during stream-I/O operations. Stream manipulators.
C++ Workshop Sujana Jyothi Dept. of Computer Science.
CS212: Object Oriented Analysis and Design Lecture 2: Introduction to C++
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
Simple Data Types Built-In and User Defined Chapter 10.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Loops: Repeating One or More Statements for loop while loop.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Student Book Input / Output in C++
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with output formatting.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Program Files and Preprocessing Directives Program files and.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
Last Time…. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Pointers Declare a pointer Address of a variable Pointers and.
Operator Kinds of Operator Precedence of Operator Type Casting.
Today… Continue Building Expressions: –Literals –Constants –Casting –Array Declaration –Operators and Operator Precedence –Keywords Winter 2016CMPE212.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
CSE 220 – C Programming Expressions.
Chap. 2. Types, Operators, and Expressions
Programming Fundamentals
trigraph ??= is for # ??( is for [ ??< is for { ??! is for |
Precedence and Associativity
Instructor: Ioannis A. Vetsikas
DATA HANDLING.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Programming with ANSI C ++
Module 2 Variables, Data Types and Arithmetic
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
Chapter 3 The New Math.
INTRODUCTION TO C.
Presentation transcript:

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. More on Handling Basic Data Types Mixed types of data Data type conversion Bitwise operators Define a new type Define alternative names for existing data types Variables duration and scope

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Mixed expressions Mixed expression rule –Two floating operation  higher precision –Floating and Integer operation  floating See details in p.92

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Explicit Casts Static cast –Old-style casts (C) (object_type) (expression) int a = (int) 3.5; or int a = 3.5; –New-style casts (C++) static_cast (expression) int a = static_cast 3.5*23; Program 3.1

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. sizeof() Count the size of a type or a variable Program 3.2

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Numeric Limitation numeric_limits ::min() numeric_limits ::max() numeric_limits ::digits Program 3.3 Header file –limits.h

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Boolean Operators T: true; F: false PQP && QP || Q!P TTTTF TFFTF FTFTT FFFFT

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Bitwise operators

Bitwise operations bit1bit2&|^~bit : true, 0: false

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Bitwise Operators Bitwise AND  & –0&0  0 0&1  0 1&1  1 Bitwise OR  | –0|0  0 0|1  1 1|1  1 Bitwise XOR  ^ –0^0  0 0^1  1 1^1  0 Left shift  << –( )<<3  ( ) Right shift  >> –( )>>2  ( ) Bitwise complement  ~ –~( )  ( )

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Bitwise exclusive OR operations Program 3.4

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Bitwise exclusive OR operations Program 3.4

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Number System 0x1AF = 1* * = 431 0xCAD = 12* * = 3245 o123 = 1*8 2 +2* = 83 o10101 = 1*8 4 +0*8 3 +1*8 2 +0* = x1AF = ( ) = o717 o123 = ( ) = 0x53

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Output Manipulators Header file –iostream.h Behind cout << –dec, hex, oct, left, right, fixed, scientific, showpoint, noshowpoint, showbase, noshowbase, showpos, noshowpos, uppercase, nouppercase, boolalpha, noboolalpha

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC.

Output Manipulators Header file –iomanip.h Behind cout << –setprecision, setw, setbase, setfill

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Enumerated Data Types Limited range with name –enum Weekday { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; –Weekday today = Tuesday;// 1 –enum Weekday { Monday=1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; –enum Weekday {Monday=1, Mon=1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Un-unique Data Enumerations –enum Weekday { Monday, Mon = Monday, Tuesday = Monday+2, Tues = Tuesday, Wednesday = Tuesday+2, Wed = Wednesday, Thursday = Wednesday+2, Thurs = Thursday, Friday = Thursday +2, Fri = Friday, Saturday = Friday +2, Sat = Saturday, Sunday = Saturday+2, Sun = Sunday}; // 0~12 –enum Punctuation {Comma = ‘,’, Exclamation=‘!’, Question = ‘?’}; // 33~63

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Anonymous Enumerations –enum {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday} yesterday, today, tommorrow; –enum {feetPerYard = 3, inchesPerFoot = 12, yardsPerMile = 1760}; –cout << “Feet in 5 miles= “ << 5*feetPerYard*yeardPerMile;

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Casting between Integer and Enumeration Types The enumerators are constants –Weekday today = Tuesday; –int day_value = today+1; –today = day_value; //error –today = static_cast (day_value); –enum Height {Bottom, Top = 20} position; –position = static_cast (10); Program 3.5

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Lifetime of a Variable A variable can have three different kinds of storage duration –Automatic storage duration –Static storage duration –Dynamic storage duration

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Variable Scopes Automatic variables or local variables –Variables declared in block –Program 3.6 Global variables –Variables declared outside of blocks –::  scope resolution operator –Program 3.7 Static variables (local access, global exist) External variables

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Association for the operators OperatorAssociationOperatorAssociation Postfix ++LeftBinary + -Left Postfix --Left >Left Unary +Right& | ^ ~Left Unary -Right* / %Left Prefix ++Right= Prefix --Rightstatic_cast()Right

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Operator Precedence Appendix D

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Alterative types typedef long BigOnes; –BigOnes mynum = 0L; –long mynum = 0L;// same as previous typedef int EventCount; typedef long EventCount; // redefine EventCoutn the larger range of long integer