You are on page 1of 38

A Closer Look at Data Types,

Variables, and Expressions

C Programming Language

MELJUN CORTES

Objectives
Well learn
Data type modifiers

Global/local Variables
Constants
Type conversions

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

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 char and int, default.

unsigned : apply to char and int.


shot : apply to int.
long : apply to int and double.

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

1. Use Cs Data-Type Modifiers

(2 byte)

(4 byte)

(2 or 4 byte)

(8 byte)

(4 byte)

(10 byte)

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

1. Use Cs Data-Type Modifiers


All Data Types
Data type

Bits

Range

char

-128 ~ 127

unsigned char

0 ~ 255

signed char

-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

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

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

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

1. Use Cs Data-Type Modifiers


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

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

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
MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

1. Use Cs Data-Type Modifiers


Example 3.
Using a char variable for number arithmetic.

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

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.

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

10

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

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

11

2. 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);
}
MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

12

2. Learn where variables are declared


The Scope of local variable

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

13

2. Learn where variables are declared


The 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);
}
MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

14

2. Learn where variables are declared


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

**** Result ****


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

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

15

2. Learn where variables are declared


Example 2. - Appropriateness using of global variable.

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

16

2. Learn where variables are declared


Example 3. - Local variables dont maintain their values
between functions calls.

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

17

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 nongeneral 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.

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

18

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

64000
100001
123.25
MELJUN CORTES, MBA,MPA,BSCS

int
unsigned
long
double
School of Computer Information

19

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

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

20

3. Take a closer look at constants


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

#include <stdio.h>
int main(void) {

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


return 0;
}

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

21

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

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

22

4. Initialize variables
Variable initialization
type var-name = constant ;
int count = 100;

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
MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

23

4. Initialize variables
Example 1.

Multiple initialization.
int min = 0, count, max = 100;

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

24

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

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

25

4. Initialize variables
Example 3. - Initialization by any expression

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

26

4. Initialize variables
Exercises

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);

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

27

5. Understand type conversions in expressions


C lets you mix different types of data together in
one expression.
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, MBA,MPA,BSCS

School of Computer Information

28

5. Understand type conversions in expressions


Examples of implicit conversion in expressions
Expression

Intermediate Type

char + float

float

int long

long

int * double

double

float / long double

long double

(short+ long)/float

long then float

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

29

5. Understand type conversions in expressions


Type promotion

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

30

5. Understand type conversions in expressions


Example 1. - implicit type conversion

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

31

5. Understand type conversions in expressions


Example 2. - implicit type conversion

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

32

6. Understand type conversions in


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

side differs from that of the left


The type of the right side is converted into that of the left
int num = 1000;
char ch ;
ch = num ;
when the type of the left side is smaller than the type of
the right Data loss may occur

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

33

6. Understand type conversions in


assignments
Example 1. - implicit type conversion

1234.009800 1234

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

34

6. Understand type conversions in


assignments
Exercises - What does this program display?

Result 3.000000
MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

35

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);

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

36

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.

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

37

7. Program with type casts


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

MELJUN CORTES, MBA,MPA,BSCS

School of Computer Information

38