Data types - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Data types

Description:

int auks, bats, coots; // the following statement adds the values as double, ... coots = int (19.99) int (11.99); // new C syntax ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 17
Provided by: roeh
Category:
Tags: coots | data | types

less

Transcript and Presenter's Notes

Title: Data types


1
Data types
  • Fundamental data types
  • Integer, floating point, character
  • Derived data types
  • Arrays
  • Strings
  • Structures

2
Fundamental data types
  • Integer types
  • short, int, long
  • Character type
  • char
  • Boolian type
  • bool
  • Floating-point types
  • float, double, long double

3
Integer limits
4
Integer limits
  • // limits.cpp -- some integer limits
  • include ltiostreamgt
  • using namespace std
  • include ltclimitsgt // use limits.h for older
    systems
  • int main()
  • int n_int INT_MAX // initialize
    n_int to max int value
  • short n_short SHRT_MAX // symbols
    defined in limits.h file
  • long n_long LONG_MAX
  • // sizeof operator yields size of type or of
    variable
  • cout ltlt "int is " ltlt sizeof (int) ltlt "
    bytes.\n"
  • cout ltlt "short is " ltlt sizeof n_short ltlt "
    bytes.\n"
  • cout ltlt "long is " ltlt sizeof n_long ltlt "
    bytes.\n\n"
  • cout ltlt "Maximum values\n"
  • cout ltlt "int " ltlt n_int ltlt "\n"
  • cout ltlt "short " ltlt n_short ltlt "\n"

5
Exceeding integer limits
  • // exceed.cpp -- exceeding some integer limits
  • include ltiostreamgt
  • using namespace std
  • define ZERO 0 // makes ZERO symbol for 0
    value
  • include ltclimitsgt // defines INT_MAX as largest
    int value
  • int main()
  • short sam SHRT_MAX // initialize a
    variable to max value
  • unsigned short sue sam// okay if variable
    sam already defined
  • cout ltlt "Sam has " ltlt sam ltlt " dollars and
    Sue has " ltlt sue
  • cout ltlt " dollars deposited.\nAdd 1 to each
    account.\nNow "
  • sam sam 1
  • sue sue 1
  • cout ltlt "Sam has " ltlt sam ltlt " dollars and
    Sue has " ltlt sue
  • cout ltlt " dollars deposited.\nPoor Sam!\n"
  • sam ZERO
  • sue ZERO
  • cout ltlt "Sam has " ltlt sam ltlt " dollars and
    Sue has " ltlt sue

6
Arithmetic
  • // arith.cpp -- some C arithmetic
  • include ltiostreamgt
  • using namespace std
  • int main()
  • cout.setf(ios_basefixed, ios_basefloatfiel
    d) // fixed-point
  • float hats, heads
  • cout ltlt "Enter a number "
  • cin gtgt hats
  • cout ltlt "Enter another number "
  • cin gtgt heads
  • cout ltlt "hats " ltlt hats ltlt " heads " ltlt
    heads ltlt "\n"
  • cout ltlt "hats heads " ltlt hats heads ltlt
    "\n"
  • cout ltlt "hats - heads " ltlt hats - heads ltlt
    "\n"
  • cout ltlt "hats heads " ltlt hats heads ltlt
    "\n"
  • cout ltlt "hats / heads " ltlt hats / heads ltlt
    "\n"
  • return 0

7
Character variables
  • // chartype.cpp -- the char type
  • include ltiostreamgt
  • using namespace std
  • int main( )
  • char ch // declare a char variable
  • cout ltlt "Enter a character\n"
  • cin gtgt ch
  • cout ltlt "Holla! "
  • cout ltlt "Thank you for the " ltlt ch ltlt "
    character.\n"
  • return 0

8
Type cast
  • // typecast.cpp -- forcing type changes
  • include ltiostreamgt
  • using namespace std
  • int main()
  • int auks, bats, coots
  • // the following statement adds the values as
    double,
  • // then converts the result to int
  • auks 19.99 11.99
  • // these statements add values as int
  • bats (int) 19.99 (int) 11.99 // old C
    syntax
  • coots int (19.99) int (11.99) // new
    C syntax
  • cout ltlt "auks " ltlt auks ltlt ", bats " ltlt
    bats
  • cout ltlt ", coots " ltlt coots ltlt '\n'
  • char ch 'Z'
  • cout ltlt "The code for " ltlt ch ltlt " is "
    // print as char

9
Derived data types
  • Arrays
  • static, dynamic
  • Strings
  • Structures

10
Arrays
  • // arrayone.cpp _ small arrays of integers
  • include ltiostreamgt
  • using namespace std
  • int main()
  • int yams3 // creates array with three
    elements
  • yams0 7 // assign value to first
    element
  • yams1 8
  • yams2 6
  • int yamcosts3 20, 30, 5 // create,
    initialize array
  • // NOTE If your C compiler or translator can't
    initialize
  • // this array, use static int yamcosts3 instead
    of
  • // int yamcosts3
  • cout ltlt "Total yams "
  • cout ltlt yams0 yams1 yams2 ltlt "\n"
  • cout ltlt "The package with " ltlt yams1 ltlt "
    yams costs "

11
Strings
  • // strings.cpp _ storing strings in an array
  • include ltiostreamgt
  • include ltcstringgt // for the strlen() function
  • using namespace std
  • int main()
  • const int Size 15
  • char name1Size // empty
    array
  • char name2Size "Cowboy" //
    initialized array
  • // NOTE some implementations may require the
    static keyword
  • // to initialize the array name2
  • cout ltlt "Howdy! I'm " ltlt name2
  • cout ltlt "! What's your name?\n"
  • cin gtgt name1
  • cout ltlt "Well, " ltlt name1 ltlt ", your name has
    "
  • cout ltlt strlen(name1) ltlt " letters and is
    stored\n"

12
Strings
13
Structures
14
Structures
  • // structur.cpp -- a simple structure
  • include ltiostreamgt
  • using namespace std
  • struct inflatable // structure template
  • char name20
  • float volume
  • double price
  • int main()
  • inflatable guest
  • "Glorious Gloria", // name value
  • 1.88, // volume value
  • 29.99 // price value
  • // guest is a structure variable of type
    inflatable
  • // It's initialized to the indicated values
  • inflatable pal
  • "Audacious Arthur",
  • 3.12,
  • 32.99
  • // pal is a second variable of type
    inflatable
  • // NOTE some implementations require using
  • // static inflatable guest
  • cout ltlt "Expand your guest list with " ltlt
    guest.name
  • cout ltlt " and " ltlt pal.name ltlt "!\n"
  • // pal.name is the name member of the pal
    variable
  • cout ltlt "You can have both for "
  • cout ltlt guest.price pal.price ltlt "!\n"
  • return 0

15
Assigning structures
  • // assgn_st.cpp -- assigning structures
  • include ltiostreamgt
  • using namespace std
  • struct inflatable
  • char name20
  • float volume
  • double price
  • int main()
  • inflatable bouquet
  • "sunflowers",
  • 0.20,
  • 12.49
  • inflatable choice

16
Local and external declarations
Write a Comment
User Comments (0)
About PowerShow.com