lecture 3 data types 1
TRANSCRIPT
-
8/10/2019 Lecture 3 Data Types 1
1/33
1
Fundamental
data types
-
8/10/2019 Lecture 3 Data Types 1
2/33
2
#include
int main(void)
{
int a, b, c;
float x = 0.0f, y = 3.3f, = !"."f;
$intf( %&nut t'o intee$s * );
scanf( %+d+d*, b, c );
a = b - c;
x = y - ;
$etu$n 0;
/ecla$ation/ecla$ation 'ith initialiation
always initialize!!!
unction calls
1ssinment
statements
-
8/10/2019 Lecture 3 Data Types 1
3/33
3
undamental data tyes
2 &nte$al (sined o$ unsined) cha$
sho$t
int lon
2 loatin oint tyes float
double
-
8/10/2019 Lecture 3 Data Types 1
4/33
4
Long formLon
g form
char signed char unsigned char
sined shortint sined int sined longint
unsigned shortint unsignedint unsigned longint
float double long double
Common (short) formCommon
(short) form
cha$ sined cha$ unsined cha$
sho$t int lonunsined sho$t unsined unsined lon
float double lon double
undamental /ata yes
-
8/10/2019 Lecture 3 Data Types 1
5/33
5
unctionality 4$ous
Integral Types:Inte
gral Types:
cha$ sined cha$ unsined cha$
sho$t int lon
unsined sho$t unsined unsined lon
Floating Types:Floatin
g Types:
float double lon double
rithmetic Types:rithmetic T
ypes:2 inte$al tyes
2 floatin tyes
-
8/10/2019 Lecture 3 Data Types 1
6/33
6
5onstants
char a, x, 0, &, \n
int 1, 0, -54, 4234567
long 0l, 123l, 7766L
unsigne 0u, 23u, 3000000000!
"loat 1#2", #2", 1#", 3#1415$"
ou%le 1#0, -3#1412long ou%le 1#0l, 2#4433l
-
8/10/2019 Lecture 3 Data Types 1
7/33
7
cha$acte$ 6a6 6b6 6c6 ... 66
intee$ value 7" 78 77 ... 99:
cha$acte$ 616 66 656 ... 66
intee$ value " ... 70
cha$acte$ 606 696 6:6 ... 676
intee$ value ?8 ?7 0 ... "
cha$acte$ 66 6@6 6-6
intee$ value 38 ?: ?3
Aome 5ha$acte$ 5onstants
and thei$ &ntee$ Balues
he cha$acte$ C0C
has a value of ?8
-
8/10/2019 Lecture 3 Data Types 1
8/33
8
Aome 5ha$acte$ 5onstants
and thei$ &ntee$ Balues
ame of Character "ritten in C Integer #alue
alert CDaE $
bac%slash &'' *
horizontal tab &'t
newline &'n +,
null character &', ,
-uote &'. /
he cha$acte$
CD0C has a
value of 0
-
8/10/2019 Lecture 3 Data Types 1
9/33
9
Aome 5ha$acte$ 5onstants
and thei$ &ntee$ Balues
cha$ c = 6a6;
$intf(F+cF, c);
$intf(F+dF, c);
$intf(F+c+c+cF, c, c-9, c-:);
-
8/10/2019 Lecture 3 Data Types 1
10/33
10
Aome 5ha$acte$ 5onstants
and thei$ &ntee$ Balues
cha$ c = 6a6;
$intf(F+cF, c);
$intf(F+dF, c);
$intf(F+c+c+cF, c, c-9, c-:);
ais $inted
$7is $inted
a%cis $inted
-
8/10/2019 Lecture 3 Data Types 1
11/33
11
Aome 5ha$acte$ 5onstants
and thei$ &ntee$ Baluescha$acte$ 616 66 656 ... 66
intee$ value " ... 70
cha$ c = 0;
int i = 0;
fo$ ( i = 6a6; i
-
8/10/2019 Lecture 3 Data Types 1
12/33
12
Aome 5ha$acte$ 5onstants
and thei$ &ntee$ Baluescha$acte$ 616 66 656 ... 66
intee$ value " ... 70
cha$ c = 0;
int i = 0;
fo$ ( i = 6a6; i
-
8/10/2019 Lecture 3 Data Types 1
13/33
13
Gse$ /efined 5onstants
#include
int main(void)
{
int c = 0;
'hile ( ( c = etcha$() ) H= 23F)
{
utcha$( c );
utcha$( c );
$etu$n 0;
Ioo until the 2nd 3f the File
in stdio1haea$ the line 4define
23F (5+)
-
8/10/2019 Lecture 3 Data Types 1
14/33
14
5onstants
Type 6escription 27ample
char single character &g
special character &'n
he7adecimal code &'7+a
int decimal 5+8 *8 ,8 +*/9;
octal ,+,8 5,*,8 ,=85?e7p +*/e5*8 +*1/e5*
-
8/10/2019 Lecture 3 Data Types 1
15/33
15
/ecimal, Jexadecimal, Kctal
conve$sions#include
int main(void)
{
$intf( %+d +x +oDn*, 97, 97, 97 );
$intf( %+d +x +oDn*, 0x9c, 0x9c, 0x9c);
$intf( %+d +x +oDn*, 09", 09", 09");
$intf( %+dDn*, 99 - 0x99 - 099);
$intf( %+xDn*, :07"99);$intf( %+dDn*, 0x9ff);
$etu$n 0;
-
8/10/2019 Lecture 3 Data Types 1
16/33
16
/ecimal, Jexadecimal, Kctal
conve$sions#include
int main(void)
{
$intf( %+d +x +oDn*, 97, 97, 97 );
$intf( %+d +x +oDn*, 0x9c, 0x9c, 0x9c);
$intf( %+d +x +oDn*, 09", 09", 09");
$intf( %+dDn*, 99 - 0x99 - 099);
$intf( %+xDn*, :07"99);$intf( %+dDn*, 0x9ff);
$etu$n 0;
97 93 :3
:8 9c 3?
9 f 9"
3"
9fffff
:07"99
-
8/10/2019 Lecture 3 Data Types 1
17/33
17
comute the sie of some
fundamental tyesinclue stio#h.int /ainoi
rint"he sie o" so/e "una/ental tes isco/ute#\n\n8
rint"char9 :3 %te \n, sieo"char 8
rint"short9 :3 %tes\n, sieo"short 8
rint"int9 :3 %tes\n, sieo"int 8
rint"long9 :3 %tes\n, sieo"long 8
rint"unsigne9 :3 %tes\n, sieo"unsigne 8
rint""loat9 :3 %tes\n, sieo""loat 8
rint"ou%le9 :3 %tes\n, sieo"ou%le 8 rint"long ou%le9:3 %tes\n,sieo"long ou%le 8
return 08
;
sieof $etu$ns the
numbe$ of bytes
$ese$ved fo$ a
va$iable tye.
-
8/10/2019 Lecture 3 Data Types 1
18/33
18
comute the sie of some
fundamental tyes$un on %L5 Lentium 3F
he sie of some fundamental tyes is comuted.
cha$ 9 byte
sho$t : bytes
int ? bytes
lon ? bytes
unsined ? bytesfloat ? bytes
double 8 bytes
lon double 8 bytes
-
8/10/2019 Lecture 3 Data Types 1
19/33
19
comute the sie of some
fundamental tyes
sieof(cha$) == 9
sieof(sho$t)
-
8/10/2019 Lecture 3 Data Types 1
20/33
20
4oin ove$ the Iimit
inclue stio#h.
inclue li/its#h.
int /ain oi
int i < 08
unsigne u < !=>?@'A8
"or i < 08 i 58 BBi
rint" :u B : < :u\n, u, i, u B i 8
"or i < 08 i 58 BBi
rint" :u C : < :u\n, u, i, u C i 8
return 08
;
yically eMual to 0x == :3:!9 ==
9*9;$*
-
8/10/2019 Lecture 3 Data Types 1
21/33
21
4oin ove$ the Iimit
$un on Fsco$ioF
?:7?7":7 - 0 = ?:7?7":7
?:7?7":7 - 9 = 0
?:7?7":7 - : = 9?:7?7":7 - 3 = :
?:7?7":7 - ? = 3
?:7?7":7 @ 0 = 0
?:7?7":7 @ 9 = ?:7?7":7
?:7?7":7 @ : = ?:7?7":7?
?:7?7":7 @ 3 = ?:7?7":73
?:7?7":7 @ ? = ?:7?7":7:
he va$iable $esta$ts itself
-
8/10/2019 Lecture 3 Data Types 1
22/33
22
&ntee$s6 Ne$esentation
Type @alue Ainary @alue
unsigne char unsigne char 0 0000 0000unsigne char 255 1111 1111unsigne char 127 0111 1111
unsigne char 12+ 1000 0000signe char s signe char 0 0000 0000signe char -1 1111 1111 (*Bs complement)signe char 127 0111 1111signe char -12+ 1000 0000 (*Bs complement)
signe short s signe int s
@ means @alues means sign
negati@e integers 5in
*Bs complement
-
8/10/2019 Lecture 3 Data Types 1
23/33
23
&ntee$s6 Ne$esentation (:)
sined cha$ sc = !9:8;
unsined cha$ uc = :;
sc = sc ! 9;
uc = uc - 9;
uc = :;
uc = uc@:;
+,,, ,,,,
++++ ++++
,+++ ++++ D +*$ Enderflow
,,,, ,,,, D , 3@erflow
++++ ++++
++++ +++, D *9
-
8/10/2019 Lecture 3 Data Types 1
24/33
loatin yes
foat single-precision foating-pointdouble double-precision foating-point
long double extended-precision foating-point
Type SmallestPositiveValue
LargestValue
Precision
foat 1.17*10-38 3.40*1038 6 digits
double 2.22*10-308
1.79*10308
15 digits
double x longdouble x
scan!"#$l!%& 'x(# %
-
8/10/2019 Lecture 3 Data Types 1
25/33
26
loat $e$esentation ! limited
accu$acyinclue stio#h.
int /ain
int i8
"loat " < 08
"or i < 08 i 1008 BBi
" B< 0#018
rint" :"\n, " 8
return 08
; 3utput: ,1
-
8/10/2019 Lecture 3 Data Types 1
26/33
27
Aecial float values
2 a ot a umber1 Ne$esents an illeal value
rint":"\n, sDrt-18'ill $int-1#=>E00or nan
2 IF infinity
rint":"\n, 1F0#08'ill $int1#=>G00or in"
-
8/10/2019 Lecture 3 Data Types 1
27/33
28
Oathematical unctions
s-rt(7)
pow(78y) D 7y
e7p(7) DeG
log(7)sin(7)
cos(7)
tan(7)
he functions a$e decla$ed in
1ll the functions use doubles
(floats sM$tf, o'f,exf,...)
NeMui$es linPin 'ith math lib$a$y
gcc -l/
-
8/10/2019 Lecture 3 Data Types 1
28/33
29
inclue stio#h.
inclue /ath#h.
int /ainoi
ou%le x < 08rint" \nhe sDuare root o" x an x raise
\nto the x oHer Hill %e co/ute#\n---\n\n 8
Hhile 1
rint"=nut x9 8
scan":l", &x 8
i" x .< 0#0
rint"\n:15s:22#15e\n:15s:22#15e\n:15s:22#15e\n\n
,x < , x, sDrtx < , sDrtx,
oHx, x
-
8/10/2019 Lecture 3 Data Types 1
29/33
30
he Nesult of the L$o$am
he sDuare root o" x an x raisehe sDuare root o" x an x raiseto the x oHer Hill %e co/ute#to the x oHer Hill %e co/ute#
------
=nut x9 2=nut x9 2
x < 2#000000000000000eB00x < 2#000000000000000eB00
sDrtx < 1#4142135623730$5eB00sDrtx < 1#4142135623730$5eB00
oHx, x < 4#000000000000000eB00oHx, x < 4#000000000000000eB00
=nut x9=nut x9
-
8/10/2019 Lecture 3 Data Types 1
30/33
31
he usual a$ithmetic conve$sion ($omotion)
2 If eitheroperand is of type long double8 double8 floator unsigned long8 the
other operand is converted to long double8 double8 floator unsigned longappropriately1
2 3therwise8 the integral promotions are performed on both operands8
and the following rules are applied:
2 If one operand has type longand the other operand has type unsignedthen
one of two possibilities occurs:Q If a long can representall the @alues of an unsigned8 then the
operand of type unsignedis convertedto long1
Q If a longcannot representall the @alues of an unsigned8 then
bothoperands are convertedto unsigned long1
2 3therwise8 if either operand is of type long8 the other operand is converted
to long1
2 3therwise8 if either operand is of type unsigned8 the other operand is
convertedto unsigned1
2 3therwise8 both operands ha@e type int1
-
8/10/2019 Lecture 3 Data Types 1
31/33
32
Rx$essions and yes
char c8 short s8 int i8 unsigne u8 long l8unsigne long ul8"loat "8 ou%le 8 long ou%le l8
27pression Type 27pression Type
c s H i int u $ i unsigned
u *1, i double f $ i float
c = / int $ s ul unsigned long
c = 1, double ld = c long double
d = s double u ul unsigned long
* i H l Long u l systemdependent
-
8/10/2019 Lecture 3 Data Types 1
32/33
33
5ast
ou%le < 3#38int i < 08
unsigne ui < 08
char c8
i < int8
< ou%lei F 28
ui < unsignei8
c < ++28
i DD trunc(d) D /
d DD +1
-
8/10/2019 Lecture 3 Data Types 1
33/33
34