Вы находитесь на странице: 1из 7

ICS 103: Computer Programming in C

Lab #02: Data Types


Objectie:
Learn about Basic data types in C and how to use them in Problem Solving
!asic Data types in C
The following table shows the basic data types in C:
Data Type C "ey#or$ !ytes %ange P&ace'o&$er
(print)*
P&ace'o&$er
(scan)*
Character char 1 -1! to 1" # c # c
$nteger int % -&1%"&%!'&(%! to
&1%"&%!'&(%"
#d #d
)loating Point float % -'*%+'! to
'*% +'!
#f #f
,ouble precession
floating point*
double ! -1*"e'-! to
1*"e.'-!
#f #lf
+otes:
1* C also defines the following modifiers that can be used to modify the memory allocated to the
types above as well as the range of possible values:
short
long
signed
unsigned
)or e/ample& short int 0or simply s'ort1 reduces the si2e allocated to integers to bytes with a
rage of 0-'&"(! to '&"("1*
The default for char and int is signed 3 meaning the values are distributed between negative and
positive values* unsigne$ c'ar will allocate all the possible range to positive values 0- to 441*

* The standard C specification 056S$ C1 does not actually specify a fi/ed range or memory for these
types* 5ll it says is something li7e this:
short int 89 int 89 long int
float 89 double 89 long double
:hat this means is that a s'ort int should assign less than or the same amount of
storage as an int and the int should be less or the same bytes as a &ong int*
Thus& the above ranges are actually machine and implementation dependent*
The following e/ample will print the actual si2e allocated based on your computer:
;< ,isplays the number of bytes used to store each basic type <;
=include 8stdio*h>

int main0void1 ?
printf0@The si2e of char is #d bytesAn@& si2eof0char11B
printf0@The si2e of short is #d bytesAn@& si2eof0short11B
printf0@The si2e of int is #d bytesAn@& si2eof0int11B
printf0@The si2e of long is #d bytesAn@& si2eof0long11B
printf0@The si2e of float is #d bytesAn@& si2eof0float11B
printf0@The si2e of double is #d bytesAn@& si2eof0double11B
printf0@The si2e of long double is #d bytesAn@& si2eof0long double11B

printf0@An+nter any character to terminate * * *An@1B
getch01B

return -B
C
Copy and paste the above program in the ,ev C.. editor& save it with e/tension *c& compile& and
then e/ecute the program
'* Characters are actually represented in C as integer values* +ach character is represented by its
5SC$$ code 0e*g 5 9 (4* B 9 ((& etc1* The table after the program below shows the printable
5SC$$ characters and their corresponding 5SC$$ codes*
Printing a char variable using D#cE will print the character but printing it with D#dE will print the
5SC$$ code* Similarly& printing an integer variable with D#cE will also print the character
provided the value is within the range of character values* The following e/ample demonstrates
this:
;< Shows the relationship between char and int types <;
=include 8stdio*h>

int main0void1 ?
char c 9 F5FB
int code 9 (4B
printf0@The 5SC$$ value of #c is #dAn@& c& c1B
printf0@Printing #c using its 5SC$$ value #dAn@& code& code1B

printf0@An+nter any character to terminate * * *An@1B
getch01B

return -B

C
Copy and paste the above program in the ,ev C.. editor& save it with e/tension *c& compile& and then
e/ecute the program
Printab&e ,SCII C'aracters
Char
Decimal
code
Description
SP 32 Space
! 33 Exclamation mark
" 34 Quotation mark (&quot; in HTM!
" 3# $ro%% &atc& (num'er %i(n!
) 3* +ollar %i(n
, 3- Percent %i(n
& 3. /mper%an0
1 32 $lo%in( %in(le quote (apo%trop&e!
( 43 4penin( parent&e%e%
! 45 $lo%in( parent&e%e%
6 42 /%teri%k (%tar7 multipl8!
9 43 Plu%
7 44 $omma
: 4# H8p&en7 0a%&7 minu%
; 4* Perio0
< 4- Sla%& (=or>ar0 or 0i?i0e!
3 4. @ero
5 42 4ne
2 #3 T>o
3 #5 T&ree
4 #2 Aour
# #3 Ai?e
* #4 Six
'
- ## Se?en
. #* Ei(&t
2 #- Bine
C #. $olon
; #2 Semicolon
D *3 e%% t&an %i(n (&lt; in HTM!
E *5 Equal% %i(n
F *2 Greater t&an %i(n (&(t; in HTM!
H *3 Que%tion mark
I *4 /t:%i(n
/ *# Jpper ca%e /
K ** Jpper ca%e K
$ *- Jpper ca%e $
+ *. Jpper ca%e +
E *2 Jpper ca%e E
A -3 Jpper ca%e A
G -5 Jpper ca%e G
H -2 Jpper ca%e H
L -3 Jpper ca%e L
M -4 Jpper ca%e M
N -# Jpper ca%e N
-* Jpper ca%e
M -- Jpper ca%e M
B -. Jpper ca%e B
4 -2 Jpper ca%e 4
P .3 Jpper ca%e P
Q .5 Jpper ca%e Q
O .2 Jpper ca%e O
S .3 Jpper ca%e S
T .4 Jpper ca%e T
J .# Jpper ca%e J
%
P .* Jpper ca%e P
Q .- Jpper ca%e Q
R .. Jpper ca%e R
S .2 Jpper ca%e S
@ 23 Jpper ca%e @
T 25 4penin( %quare 'racket
U 22 Kack%la%& (Oe?er%e %lant!
V 23 $lo%in( %quare 'racket
W 24 $aret ($ircum=lex!
X 2# Jn0er%core
1 2* 4penin( %in(le quote
a 2- o>er ca%e a
' 2. o>er ca%e '
c 22 o>er ca%e c
0 533 o>er ca%e 0
e 535 o>er ca%e e
= 532 o>er ca%e =
( 533 o>er ca%e (
& 534 o>er ca%e &
i 53# o>er ca%e i
Y 53* o>er ca%e Y
k 53- o>er ca%e k
l 53. o>er ca%e l
m 532 o>er ca%e m
n 553 o>er ca%e n
o 555 o>er ca%e o
p 552 o>er ca%e p
q 553 o>er ca%e q
r 554 o>er ca%e r
% 55# o>er ca%e %
t 55* o>er ca%e t
4
u 55- o>er ca%e u
? 55. o>er ca%e ?
> 552 o>er ca%e >
x 523 o>er ca%e x
8 525 o>er ca%e 8
Z 522 o>er ca%e Z
[ 523 4penin( curl8 'race
\ 524 Pertical line
] 52# $lo%in( curl8 'race
^ 52* Til0e (approximate!
+E 52- +elete (ru'out!7 cro%%:&atc& 'ox
Laboratory Tas-s:
1* :rite an interactive C program that reads the radius of a circle inscribed inside a
sGuare and based on that it prints the values of red and yellow areas respectively*
5ssume that the radius is in cm*
Print each output value with two digits after the decimal point* ,efine the value of 0'*1%14H1 as a
constant*
5 sample run of your program gives the following:
2* :rite an interactive C program that computes the salary in Saudi Iiyals of a
part-time employee based on :
- number of days wor7ed*
- number of hours per day assuming that he wor7ed the same hours every day*
- the hourly wage*
Choose the right type for each variable* 5 sample run of the program is:
(

3* :rite an interactive C program that reads the two initials of a person 0i*e*& the
starting letters of his first and second names1* $t then prints a message informing
him his age after 4 years* 5 sample run of the program is:

"

Вам также может понравиться