MELJUN CORTES C++ Chap4 Data Types Variables Expressions

Embed Size (px)

Citation preview

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    1/38

    A Closer Look at Data Types

    Variables and Expressions

    C Programming Language

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    2/38

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 2

    Objectives

    Well learn Data type modifiers

    Global/local Variables

    Constants Type conversions

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    3/38

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 3

    1. Use Cs Data-Type Modifiers

    Type Modifiers char, int, float, double can be modified using Cs

    type modifiers to more precisely fit your specific

    need.

    signed : apply to charand int, default.

    unsigned : apply to charand int.

    shot : apply to int.

    long : apply to intand double.

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    4/38

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 4

    1. Use Cs Data-Type Modifiers

    (4 byte)

    (8 byte)

    (10 byte)(4 byte)

    (2 or 4 byte)

    (2 byte)

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    5/38

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 5

    1. Use Cs Data-Type Modifiers

    All Data Types

    char 8 -128 ~ 127

    unsigned char 8 0 ~ 255

    signed char 8 -128 ~ 127

    int 16 -32,768 ~ 32,767

    unsigned int 16 0 ~ 65,535

    short int 16 -32,768 ~ 32,767

    unsigned short int 16 0 ~ 65,535

    long int 32 -2,147,483,648 ~ 2,147,483,647

    unsigned long int 32 0 ~ 4,294,967,295

    float 32 3.4E-38 ~ 3.4E+38

    double 64 1.7E-308 ~ 1.7E+308

    long double 80 3.4E-4932 ~ 1.1E+4932

    Data type Bits Range

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    6/38

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 6

    1. Use Cs Data-Type Modifiers

    Shorthand notation (implied int) unsigned int unsigned

    short int short

    long int long

    Format specifiers of printf() & scanf()

    %hd - short int

    %ld - long int

    %u unsigned

    %lu - unsigned long int

    %Lf - long double

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    7/38MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 7

    1. Use Cs Data-Type Modifiers

    Example 1. How to input and output shot, long, and unsigned values.

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    8/38MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 8

    1. Use Cs Data-Type Modifiers

    Example 2. The difference between the way that signed and unsigned

    integers are interpreted by C.

    **** Result ****

    -32536 33000

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    9/38MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 9

    1. Use Cs Data-Type Modifiers

    Example 3. Using a char variable for number arithmetic.

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    10/38MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 10

    1. Use Cs Data-Type Modifiers

    Exercises

    Write a program that prompts the user for a distance and

    computers how long it takes light to travel that distance.

    Use an unsigned long int to hold the distance.

    (Light travels at approximately 186,000 miles/sec)

    ====== Light travel time computation program ======

    How long distance (miles) : 500000

    500000 miles => 2.69 sec.

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    11/38MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 11

    2. Learn where variables are declared

    Location of variables declared

    Inside a function local variables

    Outside all functions global variables

    Local variable

    Is created upon entry into its function

    Is destroyed upon exit

    Can use the same name in different functions

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    12/38MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 12

    2. Learn where variables are declared

    #include

    void f1(void), f2(void);

    int main(void) {

    f1();

    return 0;

    }

    void f1(void) {

    int count;

    for (count=0; count

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    13/38MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 13

    2. Learn where variables are declared

    The Scope of local variable

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    14/38MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 14

    2. Learn where variables are declared

    The Scope of global variable#include

    void f1();

    int max; /* global variable : entire program validate */

    int main(void) {

    max= 10;

    f1();

    return 0;

    }

    void f1(void) {

    int i;

    for(i=0; i

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    15/38MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 15

    Example 1.

    A local variable and a global variable may have the same

    name.

    2. Learn where variables are declared

    **** Result ****

    count in f1() : 100

    count in main() : 10

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    16/38MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 16

    Example 2. - Appropriateness using of global variable.

    2. Learn where variables are declared

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    17/38MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 17

    Example 3. - Local variables dont maintain their valuesbetween functions calls.

    2. Learn where variables are declared

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    18/38MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 18

    2. Learn where variables are declared

    Exercises

    Write a program that contains a function called soundspeed(),

    which computes the number of seconds it will take sound to

    travel a specified distance.

    Write the program two ways: first, with soundspeed()as a non-general function and second, with soundspeed()parameterized.

    (For the speed of sound, use 1129 feet/sec)

    ====== Sound travel time computation program ======How long distance (feet) : 5000

    5000 feet => 4.43 sec.

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    19/38MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 19

    3. Take a closer look at constants

    Constants Fixed values

    Integer constants

    100, -65, 0

    Floating-point constants

    11.123, .501, 123.456E1

    Character constants

    Z, =, \n

    Numeric constants Make decision the data type

    10 int64000 unsigned

    100001long

    123.25 double

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    20/38MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 20

    3. Take a closer look at constants

    Constants Make decision of the exact type by using suffix

    Floating-point types

    F float,

    L long double

    Integer types

    U unsigned,

    L long

    A Number system based on 8 :octal

    9 011,

    21 025

    A Number system based on 16 : hexadecimal

    26 0x1A, 13 0xD

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    21/38MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 21

    3. Take a closer look at constants

    Constants String

    A set of characters enclosed by double quotes.

    Format descriptor : %s

    #include

    int main(void) {

    printf ("%s %s %s", "Once ", "upon ", "a time");

    return 0;}

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    22/38

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 22

    3. Take a closer look at constants

    Example.

    You may need to explicitly tell the compiler what type of constant

    you are using.

    It does not output the correct value.

    You need change type of constant

    2309 2309.0

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    23/38

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 23

    4. Initialize variables

    Variable initialization

    Initialization of global variable

    Using Only constants

    Initialized only once at the start of program execution.

    Initialization of local variable

    Using constants, variables, or function calls

    Initialized each time a function is entered

    type var-name = constant ;

    int count = 100;

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    24/38

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    25/38

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 25

    4. Initialize variables

    Example 2.

    Local variables are initialized each time the function is

    entered.

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    26/38

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 26

    4. Initialize variables

    Example 3. - Initialization by any expression

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    27/38

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 27

    4. Initialize variables

    Exercises

    Write a program that gives an integer variable called ian initial

    value of 100and then uses ito control a forloop that displays

    the numbers 100down to 1.

    int i = 100;for( ; i > 0 ; i--)

    printf(%d , i);

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    28/38

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 28

    5. Understand type conversions in expressions

    C lets you mix different types of data together inone expression.

    char ch;int i;float f;

    double outcome;

    ch = '0';i = 10;f = 10.2;

    outcome = ch * i / f ;

    int type

    float type

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    29/38

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 29

    5. Understand type conversions in expressions

    Examples of implicit conversion in expressionsExpression Intermediate Type

    char + float

    int long

    int * double

    float / long double

    (short+ long)/float

    float

    long

    double

    long double

    long then float

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    30/38

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 30

    5. Understand type conversions in expressions

    Type promotion

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    31/38

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 31

    5. Understand type conversions in expressions

    Example 1. - implicit type conversion

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    32/38

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 32

    5. Understand type conversions in expressions

    Example 2. - implicit type conversion

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    33/38

    6 Understand type conversions in

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    34/38

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 34

    6. Understand type conversions in

    assignments

    Example 1. - implicit type conversion

    1234.009800 1234

    6 Understand type conversions in

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    35/38

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 35

    6. Understand type conversions inassignments

    Exercises - What does this program display?

    Result 3.000000

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    36/38

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 36

    7. Program with type casts

    Type conversion using type casts Transform the type of a variable temporarily

    Can use only right side of assignment statement

    (type) value

    float f;

    f = 100.2;

    /* print f as an integer */

    printf ("%d", (int)f);

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    37/38

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 37

    7. Program with type casts

    Example 1.

    Use sqrt(), for loop

    Print the square roots of the numbers between 1 and 100.

    Print the whole number portion and the fractional part of

    each result separately.

  • 8/10/2019 MELJUN CORTES C++ Chap4 Data Types Variables Expressions

    38/38