Simple Data Types - PowerPoint PPT Presentation


PPT – Simple Data Types PowerPoint presentation | free to download - id: a818c-NGRjZ


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation

Simple Data Types


Single printable character enclosed by single quotes 'A' '7' ... Control characters (non printable characters) used to control output ' ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 32
Provided by: arms9
Learn more at:
Tags: data | printable | simple | types


Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Simple Data Types

Simple Data Types
  • Built-In
  • and
  • User Defined

Chapter 10
Built-In Simple Types
  • Simple (or "atomic")
  • Each value is indivisible, a single entity
  • Examples
  • int, char, float
  • NOT simple types
  • strings, arrays, structs

Characteristics of Simple Types
  • An object of a given type uses a specific number
    of bytes
  • sizeof( ) function will give how many
  • int gt 2 bytes
  • float gt 4 bytes (on our Borland C)
  • An object of a simple type has a range of values
  • low to high
  • range is limited by size allocated to the type

Integral Types
  • Include
  • char, short, int, long
  • can be signed or unsigned
  • signed integer stored in 2 bytes
  • range is -32768 32767
  • Constants can be specified in
  • decimal, octal, hex
  • normally in decimal

Floating Point Types
  • Stored in scientific notation
  • Include
  • float, double, long double
  • Float stored in 8 bytes
  • 5 - 6 significant digits
  • range ? 3.4 E ? 38

Assignment Operators Expressions
  • Assignment operator
  • Syntax variable expression
  • Statement has
  • a value of the expression
  • value stored in variable location
  • Value previously stored in variable is now wiped

Combined Assignment Operators
  • Considera 5 // same as a a 5
  • Similar results with- /
  • The symbol is considered a binary
  • syntax requires a variable on the left
  • an expression on the right

Increment Decrement Operators
  • a // a unary operator which increments
    a by
    1same as a a 1 or a 1
  • Similar results with --
  • and -- work only on variables not constants
  • Consider cout ltlt a and cout ltlt a
  • post increment prints, then increments
  • pre-increment increments first, then prints

Bitwise Operators
  • Operators ltlt, gtgt , , and
  • Used for manipulating individual bits within
  • ltlt and gtgt for shifting bits
  • Most of what we do will use and for logical
    AND and OR

The Cast Operator
  • Wen mixing data types in expressions, implicit
    type casting occurs
  • Example int x float fConsider x
    fVersus f x
  • Text suggests explicit type castingx (int)f
    // use the type in ( ) f float(x)
    // use the type as a function

The sizeof Operator
  • A unary operator
  • yields size (in bytes) of some variable or data
  • almost acts as a function
  • cout ltlt sizeof (part_struct)
  • Often used for arrays or structs where you need
    to specify number of bytes to be read in or sent
    to a file

The Selection Operator ?
  • A trinary operator -- requires three operands
  • Exampleamt (x lt 5) ? y z
  • If x lt 5 then amt gets value stored in
    yotherwise gets value stored in z

Operator Precedence
  • Similar to algebraic precedence
  • Parentheses, unary, mult, div, addn, subt
  • Note page A1 Appendix B
  • Note also that some are L-gt R, others are R-gt L

Character Data
  • Char is actual considered a subset of int
  • uses 1 byte of memory
  • Since it is subset of int, it can store numbers
    or characterschar c1, c2c1 12c2 'A'
    // Both are legal

Character Sets
  • External representation gt what you see printed
    on screen or printer
  • Internal representation gt bit form for storing
    the data in the computer memory
  • Most machines we encounter will use ASCII
    character set
  • Other machines use EBCDIC
  • Appendix D, pg A9 has examples of both

C char Constants
  • Single printable character enclosed by single
    quotes'A' '7' ''
  • can be letters, numerals, or symbols
  • Control characters (non printable characters)
    used to control output'\n' for newline
    (same as endl)'\f' for form feed'\t'
    for tab'\a' for beep

Comparing Characters
  • Use comparison operatorslt gt etc.
  • Consider if (ch gt 'a' ch lt 'z') ...This
    checks to see if ch is in the lower case
  • Equivalent is if (islower (ch))
  • found in ctype.h
  • Check out character-testing library functions in
    Appendix C, pgs A2-A4

Convert Digit Characters to Integers
  • Possible to do arithmetic with characters
  • they are basically, integers
  • Examplechar chcin gtgt chnum ch - '0'

Converting Lowercase to Uppercase
  • From ctype.h header file, use functions
    providedch toupper (ch)ch tolower(ch)
  • Change only made if ch "needs" to be changed
  • if it is already uppercase, toupper( ) does

Representing Floating Point Numbers
  • Precision of 5 or 6 significant digits
  • Represented internally in scientific
    notationFour bytes will store
  • sign bit for the number
  • 5 or 6 significant digits
  • sign bit for the power
  • ? 38 range for the power

Arithmetic with Floating Point Numbers
  • May lose accuracy due to round off error
  • especially when combining very large with very
    small numbers
  • Warnings
  • don't use floats to control loops
  • don't compare floats for equality, rather compare
    for closenessif ( abs (a - b) lt 0.0001)

Implementation of Float on a Computer
  • Example

Representational ErrorThe arithmetic error when
precision of true results greater than precision
for machine
Implementation of Float on a Computer
  • Underflow
  • results of calculation too small to be
    represented1.3E10 4.5E-10
  • Overflow
  • value of calculation too large to be
    stored5.6E20 7.8E30
  • C does not define results when this occurs,
    usually garbage values

The Typedef Statement
  • Syntaxtypedef existing_type_name
  • Exampletypedef int Boolean
  • Does not really create a new type
  • is a valuable tool for writing self-documenting

Enumerated Types
  • Possible to create a new type by simply listing
    (enumerating) the constants which make up the
  • Exampleenum daysOfWeek (SUN, MON, TUE, WED,
    THU, FRI, SAT)daysOfWeek today, tomorrow,
    work_daywork_day MON
  • C represents these internally as numbers (0 ..
    6 for our example)

Enumerated Types
  • Incrementing variables of an enumerated type
  • Do NOT use workaday 1 NOR
  • Instead, use explicit type conversiontoday
    daysOfWeek (today 1)

Enumerated Types
  • Comparison
  • normal, OK
  • in order of the enumeration definition
  • I/O
  • generally not possible to do directly
  • can be sort of done, indirectly
  • Used primarily for program control, branching,
  • Possible to have functions return an enumerated

Named and Anonymous Data Types
  • Named Type
  • user defined type
  • declaration includes typedef
  • As with daysOfWeek or Boolean
  • Anonymous Type
  • does not have an associated typeenum (MILD,
    MEDIUM, HOT) salsa_sizzle
  • variable declared without typedef

User-Written Header Files
  • For your collection of handy identifiers
  • type such as our Boolean type definition
  • use include "bool.h"
  • note use of " " rather than lt gt
  • Tells the computer to go looking at the logged
    directory for bool.h file and include/insert it
    into the source code.

Type Coercion in Expressions
  • If two operands are of different types
  • one is temporarily "promoted" or "widened" to
    match the data type of the otherint x 5float
    f 1.234, amt f
  • Another example
  • char or short operands promoted to intint x 5

Type Coercion in Assignments
  • Can result in "demotion" or "narrowing"
  • Assigning a float to an integer variable
    int x 3.456
  • decimal portion of float is lost
  • This loss of data can be considered a problem or
    a feature!