0% found this document useful (0 votes)
4 views

CS111 - Lecture 9 - Fall 2023 2024

This document is a lecture on data types in C programming, covering the five basic data types: char, int, float, double, and void, along with their memory sizes and ranges. It also discusses signed and unsigned integers, type modifiers, and provides examples of using these data types in programming. Additionally, it touches on recursion, counting systems, binary and hexadecimal conversions, and includes assignments for practical understanding.

Uploaded by

Ahmedoueiss
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CS111 - Lecture 9 - Fall 2023 2024

This document is a lecture on data types in C programming, covering the five basic data types: char, int, float, double, and void, along with their memory sizes and ranges. It also discusses signed and unsigned integers, type modifiers, and provides examples of using these data types in programming. Additionally, it touches on recursion, counting systems, binary and hexadecimal conversions, and includes assignments for practical understanding.

Uploaded by

Ahmedoueiss
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

CS 111

INTRODUCTION TO CS

1 Presented by Assoc. Prof. Hala Abdel-Galil


Head of computer science department
LECTURE 9
2
DATA TYPES

 There are 5 data types in C:


char, int, float, double and void
 Each type reserves a number of bytes in memory.
 The size (number of bytes) of these types depends
on the architecture of the particular machine.
 See <limits.h> to determine your platform’s
values for these types.

3
 Unsigned integer means that the number may be positive
or zero only
 Signed integer means that the number may be positive, or
negative, or zero also
 If we don’t write the type it means that it’s signed
 If we define the variable x as:
 unsigned char x ; then the range for the variable x is
 0 < x < 255 ( it contains numbers equal to 28 -1= 255)
 main()
 {
 char x;
 for(x=-127 ; x<127; x++)
 printf(" %d ",x);
 }
 it will prints the numbers from -127 to 126 but if we write
 for(x=-127 ; x<=127; x++)
 We will have an infinite loop; to terminate the loop it must reach the
value 128 and it can’t be done
4
 Note that the compiler don’t send an error message
 main()
{

 unsigned char x;

 for(x=0;x<=255;x++)

 printf(" %d ",x);

}

 This will prints numbers from 0 to 255

 And if we write %c instead of %d it will prints the


symbol for this number in ASCII table
 Ex. x= 65 is equvalent to x = ‘A’
5
WRITE A PROGRAM TO PRINT OUT ALL SYMBOLS
IN ASCII TABLE ( 10 SYBOLS IN EACH LINE )
AND WRITE THE SYMBOLE BESIDE IT’S
EQUIVALENT NUMBER.
 main()
 { unsigned char k;
 printf("\n ");
 for(k=32; k < 255 ; k++)
 { if(k%10==0)
 { printf("\n\n");
 getch();
 }
 printf("%3d %c ",k,k);
 }
 printf("%3d %c",255,255);
 } 6
TURBO C’S BASIC DATA TYPES
ASCII character
See Appendix B

Data Type size Range Example


char 8 0 to 255 ‘a’ , ‘7’ ,’&’
int 16 -32768 to 32768 0,3444, -566
2bytes
float 32 3.4 e-38 to 3.4 e+38 73.6f, 0.736e2f
4bytes
double 64 1.7 e-308 to 1.7 e+308 73.6, -738.31,
8bytes 678 e-3
void
 On many0machines:
Valueless ---

 int occupies2 or4 bytes


 float4 bytes with6 or7 significant digits
 double about twice of float and greater accuracy so we
7
can represent the fraction parts in a wide range
 Long double occupies ten bytes
ASCII CHARACTER SET ASCII of ‘A’ = 65
(AMERICAN STANDARD CODE ASCII of ‘a’ = 97
FOR INFORMATION INTERCHANGE)

Appendix B
5
printf( "The character (%c) has the value %d\n", 'a', 'a' );
The character (a) has the value 97
TYPE MODIFIERS

 Excepting type void, the basic data type may


have various modifiers preceding them.

 The modifiers are: Longer than /


Signed, unsigned, long, short shorter than
regular
8 bits 8 bits

It remains All 8 bits are used


Sign bit 7 bits Unsigned
0 → positive
6
1 → negative
Signed Approximately twice the signed
Data type Printf conversion scanf conversion
specification specification
Floating-point types
long double %Lf %Lf
double %f %lf
float %f %f
Integer types
unsigned long long int %llu %llu
long long int %lld %lld
unsigned long int %lu %lu
long int %ld %ld

Fig 5.5 section 5.6


unsigned int %u %u
int %d %d
unsigned short %hu %hu
7
short %hd %hd
char %c %c
1- Write a program to print out the letters from a to z. by using
the equivalent number in ASCII table.

2- what’s the output of the following programe


main( ) ;
{ char k ;
for (k = 1 ; k < 200 ,k++)
printf( " %d " , k ) ;
}
main()
{
int k=6, m=5;
float x;
double y=1/3 ;
x=k/m;
printf("\n %f", x);
printf("\n %f",x+y);
printf("\n %f", (float)k/m);
}
1.000000
1.333333
1.200000
If we have the following variables:
int i
long int n
float x
double y what’s the output type of the following statements:

(a) i + n
(b) i / n
(c) x + n
(d) x* y
(e) n *y
(f) (float) y
(g) (double) y
(r) (float) i / n
COMPOUND INTEREST CALCULATIONS

Consider:
 A person invests $1000.00 in a savings account
yielding 5% interest. Assuming that all interest is
left on deposit in the account, calculate and print
the amount of money in the account at the end of
each year for 10 years. Use the following formula
for determining these amounts:
a = p(1 + r)n

Section 4.6
where
p is the original amount invested (i.e., the principal)
r is the annual interest rate
n is the number of years
a is the amount on deposit at the end of the nth year.
14
COMPOUND INTEREST CALCULATIONS
(CONT.)

function prototype:
unsigned int year;
double pow (double, double)

Section 4.6
Implicitly
converted to double

15
RECURSION
16
RECURSION
 In C, a function that calls itself directly or
indirectly is called recursive function.
 Recursion is useful for problems that can be
represented by a simpler version of the same
problem.
 Example: the factorial function
6! = 6 * 5 * 4 * 3 * 2 * 1
We could write:
6! = 6 * 5!

In general, the factorial


17
n>=1
RECURSIVE FUNCTION
Recursive problem-solving have in common:
➔ One or more simple cases of the problem have
a direct and easy answer – base case.
Example: 0! =1.
➔ The other cases must resemble the original
problem but simpler or smaller. “Recursive
cases” or “Recursive call”.
Example: n! = n (n-1)!

 The idea is to keep reducing the problem size


until it reduces to the simple case which has an
obvious solution. 18
19
20
21
22
23
 Begins with the terms 0 and 1 and has the
property that each succeeding term is the sum of
the two preceding terms. Write a recursive
function fibonacci( n ) that calculates the nth
Fibonacci number.

24
THANKS
25
COUNTING SYSTEMS
COUNTING SYSTEM
 There are three kinds of people in the world:
those who can count, and those who can not.
- Unknown Wisdom
BASE 10 COUNTING SYSTEM
 We happened to use the
current counting system,
because we happened to have
ten fingers.
 If dinosaurs had ruled the
earth, they would be happy
to use a 8-based counting
system.
NUMBERS

 Ancient Africa
 Notches on a bone.
 Egyptians/Roman
 Each magnitude is represented by a symbol.
 Indian/Arabian (Modern numbering system)

1,475,268
BASE 10 (DECIMAL NUMBERS)

 What does 157 mean?


 157 = 1 x 100 + 5 x 10 + 7 x 1
= 1 x 102 + 5 x 101 + 7 x 100
BINARY CODE

 Imagine a specie that only has two fingers. how can


they count?
 A computer is such kind of two-finger specie. 0 and 1

 Each place is the exponential of 2


BASE 10 VS BASE 2
Base 10
157
157 = 1 x 100 + 5 x 10 + 7 x 1
= 1 x 102 + 5 x 101 + 7 x 100

Base 2
1011 = 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20
1011 = 1 x 8 + 0 x 4 + 1 x 2 + 1 x 1
= 11 in decimal system
WHAT ARE BINARY NUMBERS?
 Decimal system uses ten symbols to
represent numeric information
0 1 2 3 4 5 6 7 8 9
 Each position within a number represents
10 raised to a power
 Binary system uses two symbols to
represent numeric information
0 1
 Each position within a number represents
2 raised to a power
 Binary numbers can be converted to
decimal numbers and vice versa
USING BINARY NUMBERS
 Binary numbers can be used for mathematical
operations
 101 + 110 = 1011

 Binary numbers can be used to represent


characters
 Morse Code -
 S ... U ..- S ... A .- N -.
BINARY SYMBOL REPRESENTATION
 Two numbers/symbols can be represented in
many ways

 0 1
 On Off
 True False
 Yes No
 Open Closed
BINARY BITS AND BYTES

 1 bit is a single bit of information, a 1 or 0


 Only two possible values
 1 byte is 8 bits, an 8 bit word
 256 possible values from 0-255 base 10 or 00000000 to
11111111 base 2
 10100110 is a single byte
BASE 10 TO BINARY

From down to up from


BINARY MATHEMATICS

0+0=0 
1+0=1 
1+1=10 
BINARY FRACTIONS
 Just as we multiply each place to the left of the
decimal by 2 when we’re working with integers,
we divide by 2 for each place to the right of the
decimal

0 0 0 1 1 1.0 1 1

4 2 1 1/2 1/4 1/8


CONVERTING BINARY FRACTIONS TO
DECIMAL
 Write down the powers of two (both left and right
of decimal).
 For each place which has a 1 in it, add the
corresponding decimal value

0 0 0 1 1 1.0 1 1

4 2 1 1/2 1/4 1/8

= 4 + 2 + 1 + ¼ + 1/8
=7.375
ASSIGNMENT #1
 Convert the following numbers into decimal:
 10001010.1010
 00000011.0110
ANSWERS

 138.625
 3.375
CONVERTING DECIMAL FRACTIONS INTO
BINARY
 Convert the part to the left of the decimal as
before.
 Take the fraction and multiply it by two.
 The whole number part is the value you enter.
 Discard the whole number part
 Repeat the preceding steps until you have the
number of digits you need.
AN EXAMPLE
 13.625 into binary (4 binary decimals)
 13 = 00001101

 0.625 * 2 = 1.25

 0.25 * 2 = 0.5

 0.5 * 2 = 1.0

 0 * 2 = 0.0

 13.625 = 00001101.1010
ASSIGNMENT #2
 Convert the following decimal fractions into
binary (4 decimal places)
 12.5

 15.875
ANSWER
 1100.1000
 1111.1110
HEXADECIMAL (BASE 16)

 Binary code is too long in representation. Hex is much


shorter.
 Converting a binary number to a Hex number is
relatively easy
 Every 4 bit can convert to a Hex
 Problem: we are short of numbers
 A-10 B-11 C-12 D-13 E-14 F-15
0

NUMBER BASE CONVERSION:


BASE-R → DECIMAL

A number expressed in base-r can be converted to its decimal


equivalent by multiplying each coefficient with the corresponding
power of r and adding
LOOKUP TABLE

Binary Hex Binary Hex


0000 0 1000 8
0001 1 1001 9
0010 2 1010 A
0011 3 1011 B
0100 4 1100 C
0101 5 1101 D
0110 6 1110 E
0111 7 1111 F
EXAMPLE
3

DECIMAL TO OCTAL
CONVERSION OF 12510 TO OCTAL

8 125
8 15 - 5
1 -7

12510=1758
4

 OCTAL TO DECIMAL CONVERSION

175

5X80 = 5
7X81 = 56

1X82 = 64

125

1758 = 12510
QUIZ
 No Calculators!!!!
 Convert binary code to Decimal number.
 10100101 (Bin)
 Convert Decimal number to binary code
 176 (Dec)
 Convert Hexadecimal number to binary
 BADDEF
 Add these two binary numbers
 10001101+11011100=?
ANSWER
 10100101 (Binary) = 165 (Decimal)
 176 (Decimal)= 10110000 (Binary)

 BADDEF=1011,1010,1101,1101,1110,1111

 The result of summation


 101101001

You might also like