Anda di halaman 1dari 39

C Language Revise

O.S. Laboratory

Agenda
C Programming Language Overview. Main Library Methods. Type Modi iers. !e"#arations $ !e initions. Memory. Arrays vs Pointers.
2

C Programming Language
C is a #anguage& ' (mperative. ' Stru"tured. ' )ea*#y typed. ' Minima#ist. ' )ith e+p#i"it memory management. C standard #ibrary& ' Many un"tiona#ities. ' ,o data stru"tures.
%

C Programming Language
Libraries

Sour"es

Compi#er

Ob0e"ts

Lin*er

1+e"utab#e or Library

.CC #ags&
' I<includePath> ' Wall& a## warnings ' c& "ompi#e w/o #in*ing

L!/.CC #ags&
' L<libPath> ' l<lib>
-

C Programming Language
/* File: manipulation.h */ /* Inclusion guard. */ #ifndef !"IP#L!$I%"&' #define !"IP#L!$I%"&' /* !ll the methods( e.g.:*/ int do&)ob* const int i +, #endif /* File: manipulation.c */ /* Inclusion: */ #include -manipulation.h. /* Implementation of all the * methods( e.g.: */ int do&)ob* const int i +/ return 00i, 1

/* File: main.c */ #include -manipulation.h. int main*+ / return do&)ob* 23 +, 1

C Programming Language
The irst 4uestion&
' )hi"h C standard5
C89 (ANSI) C67 8(SO/(1C9 C99 (ISO/IEC) C:: 8(SO/(1C9

C Programming Language
Pros $ "ons&
' A,S(& portab#e. ' C66& some use u# eatures.
Type modi iers 8in#ine< restri"t9. ,ew types 8#ong #ong9. (nterming#ed de"#arations and "ode. One #ine "omments 8 // 9. !esignated initia#i=ers. >ariadi" ma"ros.
;

Agenda
C Programming Language Overview. Main Library Methods. Type Modi iers. !e"#arations $ !e initions. Memory. Arrays vs Pointers.
?

(nput / Output
@eader stdio.h
' int print 8 "onst "har A ormat< B 9
Prints a ormatted string< with optiona# parameters. Returns the number o written "hara"ters< or a negative integer on error.

' int s"an 8 "onst "har A ormat< B 9


Reads a ormatted string< re4uiring pointers to optiona# arguments. Returns the number o read arguments.
6

(nput / Output
@eader stdio.h
' int print 8C(L1A stream< "onst "harA ormat<B9
As print < but on a stream.

' int s"an 8C(L1A stream< "onst "harA ormat<B9


As s"an < but on a stream.

' !e au#t streams&


stdin& standard input. stdout& standard output. stderr& standard error.
:7

(nput / Output
' C(L1A open8"onst "harA n< "onst "harA mode9
Opens a i#e< or returns ,DLL on error< with mode&
' ' ' ' ' ' EwF& write mode with "reation o the i#e i it does not e+ist. ErF& reading mode. EaF& append mode with "reation o the i#e i it does not e+ist. ErGF& reading/writing. EwGF& reading/writing with "reation o the i#e i it does not e+ist. EaGF& reading/append mode with "reation o the i#e i it does not e+ist.

' int "#ose8 C(L1 A stream 9


C#oses a i#e< returning =ero on su""ess.
::

(nput / Output
@eader string.h
' si=eHt str#en8 "onst "har A s 9
Returns the #ength o a string.

' "harA str"py8"harA dst< "onst "harA sr"9


Copies string sr" in dst< returning dst.

' int str"mp8"onst "har A s:< "onst "har A s29


Compares two strings< returning&
' ,egative integer i s: is #ess than s2. ' Iero i s: is e4ua#s to s2. ' Positive integer i s: greater than s2.
:2

(nput / Output
@eader string.h
' voidA mem"py8voidAdst< "onst voidA sr"< si=eHt s9
As str"py< but on raw bytes. Returns dst. Re4uires the number o bytes to "opy.

' int mem"mp8"onst voidA m:< "onst voidA m2< si=eHt s9


As str"mp< but on raw bytes. Re4uires the number o bytes to "ompare.
:%

Agenda
C Programming Language Overview. Main Library Methods. Type Modi iers. !e"#arations $ !e initions. Memory. Arrays vs Pointers.
:-

C& Type Modi iers


Const&
' The va#ue "annot be "hanged 8it is a "onstant9.
"onst int a J 2K /A a is the "onstant 2 A/ int "onst a J 2K /A idem A/ "onst int A b J ,DLLK /A b points to a "onstant int A/ int "onst A b J ,DLLK /A idem A/ int A "onst b J "K /A b is "onstant< and points to non constant integers A/

:2

C& Type Modi iers


MemoryLre#ated modi iers&
' Register& suggest to imp#ement the variab#e into registers< or speed.
register unsigned int iK or 8 i J 7K i M :77K ++i 9 N B O

' >o#ati#e& forces to imp#ement the variab#e in memory. Dse u# with threads.
vo#ati#e int a J 7K Thread :& whi#e 8 a M :77 9K Thread 2& a J %-2K
:3

C& Type Modi iers


S"ope modi iers&
' 1+tern& de"#aration has e+terna# #in*age.
/A Ci#e in".h A/ e+tern int aK /A Ci#e p:." A/ int a J 7K /A Ci#e p2." A/ a J -2K /A de"#aration A/ /A de inition A/ /A usage A/

' Stati"& de"#aration has interna# #in*age< and it is a##o"ated in the stati" memory.
/A Ci#e p:." A/ stati" int a J 7K /A Ci#e p2." A/ stati" int a J 7K /A #o"a# de"#arationA/ /A #o"a# de"#aration A/
:;

C& Type Modi iers


Restri"t&
' Assume no over#apping memories< to per orm more aggressive optimi=ations.
void oo8 "har A restri"t< "har A restri"t 9K "har aP :7 QK "har A b J a G %K /A This may not wor* "orre"t#y& A/ oo8 a< b 9K
:?

C& Type Modi iers


(n#ine
' Suggest to e+pand a un"tion in the "a##ing point< in order to improve per orman"es. ' The "ompi#er must EseeF the body to per orm the e+pansion.
in#ine int oo8void9 Nreturn 22KO int a J oo89K /A ( in#ine is app#ied A/

int a J 22K
:6

Agenda
C Programming Language Overview. Main Library Methods. Type Modi iers. !e"#arations $ !e initions. Memory. Arrays vs Pointers.
27

C& !e"#arations $ !e initions


!e"#aration&
' )arns the "ompi#er o the e+isten"e o something.
1.g. the e+isten"e o a variab#e or method.

!e inition&
' Te##s to the "ompi#er a## the detai#s about something.
1.g. a##o"ates the memory or a variab#e< imp#ements the body o a method.

Therefore each symbol can be declared multiple times, but must be defined only once.
' Useful to split a program on multiple source files.

2:

C& !e"#arations $ !e initions


Declaration Variable Method Compound type (Struct / Union !e" nati#e type !e" compound type e+tern int aK void oo8void9K Definition int aK int b J %K stati" int " J -2K void oo8void9 N O

/A Corward de"#aration A/ /A!e"#aration $ de initionA/ stru"t SK stru"t S N OK /A !e"#aration $ de inition A/ typede int tabHsi=eHtK /A )hen S is a orward de"#aration A/ typede stru"t S SK /A)hen S is de"#ared $ de ined A/ typede stru"t S SK

22

C& !e"#arations $ !e initions


Corward de"#aration&
' @ides a## the detai#s o a type. ' A orwarded type "an be used on#y as a pointer. ' Dse u# to avoid i##ega# type re"ursion. ' Dsed a#so as design pattern.

2%

C& !e"#arations $ !e initions


1+amp#e o how to avoid type re"ursion&
/A (##ega# A/ /A Lega# A/ /A Corward de"#aration& A/ struct $% stru"t A N stru"t R bK OK stru"t R N stru"t A aK OK stru"t A N struct $ & b% OK stru"t R N stru"t A aK OK 2-

Agenda
C Programming Language Overview. Main Library Methods. Type Modi iers. !e"#arations $ !e initions. Memory. Arrays vs Pointers.
22

C& Memory
Memory !ata

(nstru"tions a J b G "K Stati"

!ynami"

Read On#y
E@e##o )or#dSF

Read/)rite
stati" int a J %%K

Sta"*
void oo89 N int a J %%K O

@eap
int A a J 8intA9 ma##o"8?9K

23

C& Memory
Variables on the stac'.
Stac' a J %% b J -void oo89 N int a J %%K int b J --K O (eap

2;

C& Memory
Variables in the heap.
void oo89 N int Aa J 8intA9 ma##o"8si=eo 8int99K int Ab J 8intA9 ma##o"8si=eo 8int99K Aa J %%K Ab J --K O Aa J %% Stac' a b Ab J -(eap

2?

C& Memory
)rray on the stac'.
Stac' aP % Q J -void oo89 N int aP - QK aP 7 Q J %%K aP % Q J --K O aP 2 Q aP : Q aP 7 Q J %% (eap

26

C& Memory
)rray in the heap.
Stac' a void oo89 N int Aa J 8intA9 ma##o"8-Asi=eo 8int99K aP 7 Q J %%K aP % Q J --K O (eap

aP % Q J -aP 2 Q aP : Q aP 7 Q J %%

%7

C& Memory
Struct "ith int si*e e+uals to , bytes, bloc's of byte, in the "orst case.
stru"t S N int aK "har ":K int bK "har "2K OK void oo89N stru"t S sK O .)/!0!12 in general si*eof(S 34 ,&si*eof(int 5,&si*eof(char Stac' padding s."2 s.b s.b padding s.": s.a s.a (eap

%:

C& Memory
Union "ith int si*e e+uals to 6 bytes, and bloc's of - byte.
union D N int aK "har "K short bK OK void oo89N union D uK O Stac' u.a u.a u.a< u.b u.a<u.b<u." (eap

%2

Agenda
C Programming Language Overview. Main Library Methods. Type Modi iers. !e"#arations $ !e initions. Memory. Arrays vs Pointers.
%%

C& Arrays vs Pointers


7ointers are not arrays 33
Look back to previous slides!

' Pointers are references to memory #o"ations. ' Arrays are #o"ations o se+uential memory.
/A ( a shou#d be an array< A avoid thisSS A/ void oo8 int A a< unsigned #en 9K /A Pre er thisSS A/ void oo8 int aPQ< unsigned #en 9K

%-

C& Arrays vs Pointers


Matri8.
Stac' aP:QP:QJ-void oo89 N int aP 2 QP 2 QK aP 7 QP 7 Q J %%K aP : QP : Q J --K O aP:QP7Q aP7QP:Q aP7QP7QJ%% (eap

%2

C& Arrays vs Pointers


)rray of pointers.
void oo89 N int AaP 2 QK aP 7 Q J 8intA9ma##o"82Asi=eo 8int99K aP : Q J 8intA9ma##o"82Asi=eo 8int99K aP 7 QP 7 Q J %%K aP : QP : Q J --K O Stac' aP:Q aP7Q aP:QP:QJ-aP:QP7Q (eap

aP7QP:Q aP7QP7QJ%%

%3

C& Arrays vs Pointers


7ointer to pointers.
void oo89 N int AAaK a J 8intAA9ma##o"82Asi=eo 8intA99K aP 7 Q J 8intA9ma##o"82Asi=eo 8int99K aP : Q J 8intA9ma##o"82Asi=eo 8int99K aP 7 QP 7 Q J %%K aP : QP : Q J --K O aP7QP:Q aP7QP7QJ%% %; Stac' a (eap aP:Q aP7Q

aP:QP:QJ-aP:QP7Q

C& Arrays vs Pointers


C String&
' An array o "hara"ters< terminated by TU7T ' "har aPQ J E@e##oFK
A string on the sta"* ' 3 bytes.

' "har A a J E@e##oFK


A string in readLon#y data memory ' 3 bytes ' and a pointer on the sta"*.

' "onst "har A a J E@e##oFK


(dem< but better sin"e a##ows "ompi#e time "he"*s.
%?

C& Arrays vs Pointers


Three string e8amples.
Stac' a void oo89 N "onst "har A a J EAAAFK "har b P Q J ERRRFK "har A " J 8"harA9 ma##o"8-Asi=eo 8"har99K str"py8"< ECCCF9K O bP%QJTU7T bP2QJTRT bP:QJTRT bP7QJTRT " "P%QJTU7T "P2QJTCT "P:QJTCT "P7QJTCT aP%QJTU7T aP2QJTAT aP:QJTAT aP7QJTAT %6
(eap /ead9nly

TU7T TCT TCT TCT

Anda mungkin juga menyukai