MELJUN CORTES C++ chapter 4 data types variables expressions

Preview:

Citation preview

A Closer Look at Data A Closer Look at Data Types, Variables, and Types, Variables, and

ExpressionsExpressionsC Programming C Programming

LanguageLanguage

MELJUN CORTESMELJUN CORTES

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 2

ObjectivesObjectives

WeWe’’ll learnll learn Data type modifiersData type modifiers Global/local VariablesGlobal/local Variables ConstantsConstants Type conversionsType conversions

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 3

1.1. Use CUse C’’s Data-Type Modifierss Data-Type Modifiers

Type ModifiersType Modifiers char, int, float, double can be modified using Cchar, int, float, double can be modified using C’’s type s type

modifiers to more precisely fit your specific need.modifiers to more precisely fit your specific need.

signed signed : apply to : apply to charchar and and intint, default. , default. unsigned unsigned : apply to : apply to charchar and and intint. . shot shot : apply to : apply to intint. . long long : apply to : apply to intint and and doubledouble. .

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 4

1.1. Use CUse C’’s Data-Type Modifierss Data-Type Modifiers

(4 byte)(4 byte)

(8 byte)(8 byte)

(10 byte)(10 byte)(4 byte)(4 byte)

(2 or 4 byte)(2 or 4 byte)

(2 byte)(2 byte)

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 5

1.1. Use CUse C’’s Data-Type Modifierss Data-Type Modifiers

All Data TypesAll 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

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

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 6

1.1. Use CUse C’’s Data-Type Modifierss Data-Type Modifiers

Shorthand notation Shorthand notation (implied int)(implied int) unsigned int unsigned int unsigned unsigned short int short int short short long int long int long long

Format specifiers of printf() & scanf()Format specifiers of printf() & scanf() %hd - short int%hd - short int %ld - long int%ld - long int %u %u –– unsigned unsigned %lu - unsigned long int%lu - unsigned long int %Lf - long double%Lf - long double

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 7

1.1. Use CUse C’’s Data-Type Modifierss Data-Type Modifiers

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

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 8

1.1. Use CUse C’’s Data-Type Modifierss Data-Type Modifiers

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

integers are interpreted by C.integers are interpreted by C.

**** Result ****

-32536 33000

**** Result ****

-32536 33000

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 9

1.1. Use CUse C’’s Data-Type Modifierss Data-Type Modifiers

Example 3.Example 3. Using a char variable for number arithmetic. Using a char variable for number arithmetic.

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 10

1.1. Use CUse C’’s Data-Type Modifierss Data-Type Modifiers

ExercisesExercises

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)

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.

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

How long distance (miles) : 500000

500000 miles => 2.69 sec.

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 11

2. Learn where variables are declared2. Learn where variables are declared

Location of variables declaredLocation of variables declared Inside a function Inside a function local variableslocal variables

Outside all functions Outside all functions global variablesglobal variables

Local variableLocal variable Is created upon entry into its functionIs created upon entry into its function

Is destroyed upon exitIs destroyed upon exit

Can use the same name in different functionsCan use the same name in different functions

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 12

2. Learn where variables are declared2. Learn where variables are declared

#include <stdio.h>         void f1(void), f2(void);         int main(void)  {           f1();           return 0;         }

        void f1(void) {           int count;           for (count=0; count<10; count++) f2();         }

        void f2(void) {           int count;           for (count=0; count<10; count++) printf (“%d “, count);         }

#include <stdio.h>         void f1(void), f2(void);         int main(void)  {           f1();           return 0;         }

        void f1(void) {           int count;           for (count=0; count<10; count++) f2();         }

        void f2(void) {           int count;           for (count=0; count<10; count++) printf (“%d “, count);         }

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 13

2. Learn where variables are declared2. Learn where variables are declared

The Scope of local variableThe Scope of local variable

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 14

2. Learn where variables are declared2. Learn where variables are declared

The Scope of global variableThe Scope of global variable

#include <stdio.h>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<max; i++) printf(“%d “, i);}

#include <stdio.h>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<max; i++) printf(“%d “, i);}

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 15

Example 1.Example 1. A local variable and a global variable may have the same name.A local variable and a global variable may have the same name.

2. Learn where variables are declared2. Learn where variables are declared

**** Result ****

count in f1() : 100count in main() : 10

**** Result ****

count in f1() : 100count in main() : 10

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 16

Example 2. - Example 2. - Appropriateness using of global variable.Appropriateness using of global variable.

2. Learn where variables are declared2. Learn where variables are declared

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 17

Example 3. - Example 3. - Local variables donLocal variables don’’t maintain their values between t maintain their values between functions calls.functions calls.

2. Learn where variables are declared2. Learn where variables are declared

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 18

2. Learn where variables are declared2. Learn where variables are declared

ExercisesExercises

Write a program that contains a function called soundspeed(),which computes the number of seconds it will take sound totravel 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)

Write a program that contains a function called soundspeed(),which computes the number of seconds it will take sound totravel 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.

====== Sound travel time computation program ======

How long distance (feet) : 5000

5000 feet => 4.43 sec.

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 19

3. Take a closer look at constants3. Take a closer look at constants

ConstantsConstants Fixed valuesFixed values Integer constantsInteger constants

100, -65, 0100, -65, 0 Floating-point constantsFloating-point constants

11.123, .501, 123.456E111.123, .501, 123.456E1 Character constantsCharacter constants

‘‘ZZ’’, , ‘‘==‘‘, , ‘‘\n\n’’ Numeric constants Numeric constants Make decision the data type Make decision the data type

10 int64000 unsigned100001 long123.25 double

10 int64000 unsigned100001 long123.25 double

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 20

3. Take a closer look at constants3. Take a closer look at constants

ConstantsConstants Make decision of the exact type by using suffixMake decision of the exact type by using suffix

• Floating-point types

F float,  L long double

• Integer types

   U unsigned,  L long

• 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

• A Number system based on 8 : octal

9 011,  21 025

• A Number system based on 16 : hexadecimal

26 0x1A,  13 0xD

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 21

3. Take a closer look at constants3. Take a closer look at constants

ConstantsConstants StringString

A set of characters enclosed by A set of characters enclosed by double quotesdouble quotes.. Format descriptor : Format descriptor : %s%s

 #include <stdio.h>

        int main(void) {

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

          return 0;

        }

 #include <stdio.h>

        int main(void) {

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

          return 0;

        }

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 22

3. Take a closer look at constants3. Take a closer look at constants

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

are using.are using.

• It does not output the correct value.

• You need change type of constant

2309 2309.0

• It does not output the correct value.

• You need change type of constant

2309 2309.0

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 23

4. Initialize variables4. Initialize variables

Variable initializationVariable initialization

Initialization of global variableInitialization of global variable Using Only constantsUsing Only constants Initialized only once at the start of program execution.Initialized only once at the start of program execution.

Initialization of local variableInitialization of local variable Using constants, variables, or function calls Using constants, variables, or function calls Initialized each time a function is enteredInitialized each time a function is entered

type var-name = constant ;

int count = 100;

type var-name = constant ;

int count = 100;

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 24

4. Initialize variables4. Initialize variables

Example 1.Example 1.

• Multiple initialization.

int min = 0, count, max = 100;

• Multiple initialization.

int min = 0, count, max = 100;

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 25

4. Initialize variables4. Initialize variables

Example 2.Example 2. Local variables are initialized each time the function is entered.Local variables are initialized each time the function is entered.

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 26

4. Initialize variables4. Initialize variables

Example 3. - Example 3. - Initialization by any expressionInitialization by any expression

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 27

4. Initialize variables4. Initialize variables

ExercisesExercises

Write a program that gives an integer variable called i an initial

value of 100 and then uses i to control a for loop that displays

the numbers 100 down to 1.

Write a program that gives an integer variable called i an initial

value of 100 and then uses i to control a for loop that displays

the numbers 100 down to 1.

int i = 100;

for( ; i > 0 ; i--)

printf(“%d ”, i);

int i = 100;

for( ; i > 0 ; i--)

printf(“%d ”, i);

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 28

5. Understand type conversions in expressions5. Understand type conversions in expressions

C lets you mix different types of data together in one C lets you mix different types of data together in one expression.expression.

        char ch;         int i;         float f;         double outcome;

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

        outcome = ch * i / f ;

                          int type 

                         float type

        char ch;         int i;         float f;         double outcome;

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

        outcome = ch * i / f ;

                          int type 

                         float type

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 29

5. Understand type conversions in expressions5. Understand type conversions in expressions

Examples of implicit conversion in expressionsExamples of implicit conversion in expressions

Expression Intermediate Type

char + float

int – long

int * double

float / long double

(short+ long)/float

float

long

double

long double

long then float

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 30

5. Understand type conversions in expressions5. Understand type conversions in expressions

Type promotionType promotion

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 31

5. Understand type conversions in expressions5. Understand type conversions in expressions

Example 1. - Example 1. - implicit type conversion implicit type conversion

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 32

5. Understand type conversions in expressions5. Understand type conversions in expressions

Example 2. - Example 2. - implicit type conversion implicit type conversion

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 33

6. Understand type conversions in assignments6. Understand type conversions in assignments

Type conversionType conversion In an assignment statement in which the type of the right side In an assignment statement in which the type of the right side

differs from that of the leftdiffers from that of the left

The type of the right side is converted into that of the leftThe type of the right side is converted into that of the left

when the type of the left side is smaller than the type of the right when the type of the left side is smaller than the type of the right Data loss may occur Data loss may occur

int num = 1000; char ch ;

ch = num ;

int num = 1000; char ch ;

ch = num ;

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 34

6. Understand type conversions in assignments6. Understand type conversions in assignments

Example 1. - Example 1. - implicit type conversion implicit type conversion

1234.009800 1234 1234.009800 1234

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 35

6. Understand type conversions in assignments6. Understand type conversions in assignments

Exercises - Exercises - What does this program display?What does this program display?

Result 3.000000 Result 3.000000

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 36

7. Program with type casts7. Program with type casts

Type conversion using type castsType conversion using type casts Transform the type of a variable temporarilyTransform the type of a variable temporarily Can use only right side of assignment statementCan use only right side of assignment statement

(type) value (type) value

      float f;         f = 100.2;         /* print f as an integer */         printf ("%d", (int)f);

      float f;         f = 100.2;         /* print f as an integer */         printf ("%d", (int)f);

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 37

7. Program with type casts7. Program with type casts

Example 1.Example 1. Use sqrt(), for loopUse sqrt(), for loop Print the square roots of the numbers between 1 and 100.Print the square roots of the numbers between 1 and 100. Print the whole number portion and the fractional part of each Print the whole number portion and the fractional part of each

result separately.result separately.

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 38

7. Program with type casts7. Program with type casts

Example 2.Example 2. You cannot cast a variable that is on the left side of an You cannot cast a variable that is on the left side of an

assignment statement.assignment statement.