memory location in c++

Upload: forkenstein

Post on 07-Jul-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/18/2019 Memory Location in C++

    1/25

  • 8/18/2019 Memory Location in C++

    2/25

    Inside every computer is a componentcalled internal memory.

    Computer’s internal memory is composedof memory locations, each with a uniquenumeric address.

    Memory location is similar to collection ofstorage bins.

    Each address can store one item at a time.

    Internal Memory

  • 8/18/2019 Memory Location in C++

    3/25

    Illustration of Storage Bins

  • 8/18/2019 Memory Location in C++

    4/25

  • 8/18/2019 Memory Location in C++

    5/25

    Memory Location

    Data Type ' indicates what type ofinformation the address will store %e.g.,

    numeric or te!tual&. Identifer ' allows the programmer to

    refer to the memory location elsewhere

    in the program.' (ames made up by the programmer.

  • 8/18/2019 Memory Location in C++

    6/25

     "ypes of Identi$er

    Variables - are memory locations whosevalues can change during runtime %when theprogram is running&. Most memory locationsare variables.

    Constants ' are memory locations whosecontent cannot be changed during programe!ecution. )ften named in uppercase letters.

    E!ample* PI %constant&, radius %variable&

  • 8/18/2019 Memory Location in C++

    7/25

    +ules In (aming Identi$ers%variables&

    (ame must begin with a letter and containonly letters, numbers, and the underscorecharacter

    (o punctuation mars, spaces, or other specialcharacters %such as - or & are allowed

    Cannot be a eyword %word that has specialmeaning in C//&

    (ames are case sensitiveE!ample* discount is di0erent from DISCOUNT andfrom Discount

  • 8/18/2019 Memory Location in C++

    8/25

    1alid or Invalid 1ariables

    total2ales

     "otal32al4s

     "otal 2ales total.2ales

    5th6tr2ales total2ale-

     "7taLsales

  • 8/18/2019 Memory Location in C++

    9/25

    C// 8eywords

    8eywords are prede$ned reservedwords or reserved identi$ers that

    have special meanings. "hey cannot be used as identi$ers in

    your program

  • 8/18/2019 Memory Location in C++

    10/25

    C// 8eywords

    auto

    constdouble

    9oat

    brea

    continueelse

    for

    void

    case

    default

    char

  • 8/18/2019 Memory Location in C++

    11/25

    C// 8eywords

    int

    shortstruct

    endl

    long

    signedunsigned

    e!plicit

    do

    while

    bool

    string

  • 8/18/2019 Memory Location in C++

    12/25

    #ata "ypes

    Memory locations come in di0erenttypes and si:es

    memory location will only accept anitem that matches its data type.

    #ata type of a memory location isdetermined by the programmer whendeclaring the location.

  • 8/18/2019 Memory Location in C++

    13/25

     short

     int

     float  double

     bool

     char    string (user-defned data type)

    ;undamental #ata "ypes in C//

  • 8/18/2019 Memory Location in C++

    14/25

    ;undamental #ata "ypes in C//

    bool ' stores

  • 8/18/2019 Memory Location in C++

    15/25

    ;undamental #ata "ypes in C/

    / oat and double - store real numbers

    %numbers with a decimal place&

    #i0erences are range of values, precision,and memory used %double has the greaterof each&

    char ' stores characters %letter, symbol,or number that will not be used in acalculation& )nly one character stored at a time

  • 8/18/2019 Memory Location in C++

    16/25

    ;undamental #ata "ypes in C

    / strin - is a user'de$ned data type,

    can store :ero %blan& or more

    characters

  • 8/18/2019 Memory Location in C++

    17/25

    ;undamental #ata "ypes in C//

  • 8/18/2019 Memory Location in C++

    18/25

    #eclaring Memory Location

    >hen declaring a memory location a data type andan identi$er %variable or constant& must be provided

    2ynta! for declaring a variable in C//

    DataType Identifier ; !"amples#

    string age;

    float length;

    int width;

    string lastname;

  • 8/18/2019 Memory Location in C++

    19/25

    ssigning ;loating'point 1aluesto Integer 1ariables

    If a 9oating'point value is assigned to aninteger variable "he fractional part will be truncated %i.e.,

    ?chopped o0@ and discarded& "he value is not rounded

    int rainfall = 3.88;cout

  • 8/18/2019 Memory Location in C++

    20/25

    2tring Literals

    Can store a series of characters inconsecutive memory locations.

    "Hello"

    Enclosed in a double quotation " " 

    Can de$ne string data type in programs  string nae;

  • 8/18/2019 Memory Location in C++

    21/25

    char $s strin literal constants

    Character literal constants initiali:e char data types Consist of one character in single quotation

    mars

    2tring literal constants initiali:e string datatypes

    Aero or more characters enclosed in doublequotation mars Empty string %“ ”& is a valid string literal

    constant.

  • 8/18/2019 Memory Location in C++

    22/25

    +eading in a 2tring 2tatement

    ' +eads in a string with no blansstring nae; 

    cin !! nae; 

    ' +eads in a string that may contain blansstring nae;

    getlinecin# nae$; 

  • 8/18/2019 Memory Location in C++

    23/25

  • 8/18/2019 Memory Location in C++

    24/25

    8nowledge Chec

    Identify the data type of the following variables*!astname

    PI where PI #$%&%'

    ( where ( %))

    *adius where radius +,

    True

    width where width %)

    -gePhoneNum.er

    )

  • 8/18/2019 Memory Location in C++

    25/25

    repared by* rof. L+6 (atividad

     Than%   & ou'' 

    That in ll Thins od May ,e lori