Sei sulla pagina 1di 39

Agenda

C Programming Language Overview. Main Library Methods. Type Modifiers. De !arations " Definitions. Memory. Arrays vs Pointers.
2

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

C Programming Language
Libraries

'our es

Compi!er

Ob/e ts

Lin)er

0*e utab!e or Library

-CC f!ags$
% I<includePath> % Wall$ a!! warnings % c$ ompi!e w.o !in)ing

LD.-CC f!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 first 3uestion$
% (hi h C standard4
C89 (ANSI) C56 7&'O.&0C8 C99 (ISO/IEC) C9: 7+i ) of up oming standard; started in 266<8

C Programming Language
Pros " ons$
% A+'&$ portab!e. % C55$ some usefu! features.
Type modifiers 7in!ine; restri ted8. +ew types 7!ong !ong8. &nterming!ed de !arations and ode. One !ine omments 7 .. 8. Designated initia!i=ers. >ariadi ma ros.
<

Agenda
C Programming Language Overview. Main Library Methods. Type Modifiers. De !arations " Definitions. Memory. Arrays vs Pointers.
?

&nput . Output
@eader stdio.h
% int printf7 onst har A format; B 8
Prints a formatted string; with optiona! parameters. Ceturns the number of written hara ters; or a negative integer on error.

% int s anf7 onst har A format; B 8


Ceads a formatted string; re3uiring pointers to optiona! arguments. Ceturns the number of read arguments.
5

&nput . Output
@eader stdio.h
% int fprintf7D&L0A stream; onst harA format;B8
As printf; but on a stream.

% int fs anf7D&L0A stream; onst harA format;B8


As s anf; but on a stream.

% Defau!t streams$
stdin$ standard input. stdout$ standard output. stderr$ standard error.
96

&nput . Output
% D&L0A fopen7 onst harA fn; onst harA mode8
Opens a fi!e; or returns +ELL on error; with mode$
% % % % % % FwG$ write mode with reation of the fi!e if it does not e*ist. FrG$ reading mode. FaG$ append mode with reation of the fi!e if it does not e*ist. FrHG$ reading.writing. FwHG$ reading.writing with reation of the fi!e if it does not e*ist. FaHG$ reading.append mode with reation of the fi!e if it does not e*ist.

% int f !ose7 D&L0 A stream 8


C!oses a fi!e; returning =ero on su ess.
99

&nput . Output
@eader string.h
% si=eIt str!en7 onst har A s 8
Ceturns the !ength of a string.

% harA str py7 harA dst; onst harA sr 8


Copies string sr in dst; returning dst.

% int str mp7 onst har A s9; onst har A s28


Compares two strings; returning$
% +egative integer if s9 is !ess than s2. % Jero if s9 is e3ua!s to s2. % Positive integer if s9 greater than s2.
92

&nput . Output
@eader string.h
% voidA mem py7voidAdst; onst harA sr ; si=eIt s8
As str py; but on raw bytes. Ceturns dst. Ce3uires the number of bytes to opy.

% int mem mp7 onst voidA m9; onst voidA m2; si=eIt s8
As str mp; but on raw bytes. Ce3uires the number of bytes to ompare.
9#

Agenda
C Programming Language Overview. Main Library Methods. Type Modifiers. De !arations " Definitions. Memory. Arrays vs Pointers.
9,

C$ Type Modifiers
Const$
% The va!ue annot be hanged 7it is a onstant8.
onst int a K 1L .A a is the onstant 1 A. int onst a K 1L .A idem A. onst int A b K +ELLL .A b points to a onstant int A. int onst A b K +ELLL .A idem A. int A onst b K L .A b is onstant; and points to non constant integers A.

91

C$ Type Modifiers
MemoryMre!ated modifiers$
% Cegister$ suggest to imp!ement the variab!e into registers; for speed.
register unsigned int iL for 7 i K 6L i N 966L ++i 8 O B P

% >o!ati!e$ forces to imp!ement the variab!e in memory. Esefu! with threads.


vo!ati!e int aL Thread 9$ whi!e 7 a N 966 8L Thread 2$ a K #,2L
92

C$ Type Modifiers
' ope modifiers$
% 0*tern$ de !aration has e*terna! !in)age.
.A Di!e in .h A. e*tern int aL .A Di!e p9. A. int a K 6L .A Di!e p2. A. a K ,1L .A de !aration A. .A definition A. .A usage A.

% 'tati $ de !aration has interna! !in)age; and it is a!!o ated in the stati memory.
.A Di!e p9. A. stati int a K 6L .A Di!e p2. A. stati int a K 6L .A !o a! de !arationA. .A !o a! de !aration A.
9<

C$ Type Modifiers
Cestri ted$
% Assume no over!apping memories; to perform more aggressive optimi=ations.
void foo7 har A restri ted; har A restri ted 8L har aQ 96 RL har A b K a H #L .A This may not wor) orre t!y$ A. foo7 a; b 8L
9?

C$ Type Modifiers
&n!ine
% Suggest to e*pand a fun tion in the a!!ing point; in order to improve performan es. % The ompi!er must FseeG the body to perform the e*pansion.
in!ine int foo7void8 Oreturn 11LP int a K foo78L .A &f in!ine is app!ied A.

int a K 11L
95

Agenda
C Programming Language Overview. Main Library Methods. Type Modifiers. De !arations " Definitions. Memory. Arrays vs Pointers.
26

C$ De !arations " Definitions


De !aration$
% (arns the ompi!er of the e*isten e of something.
0.g. the e*isten e of a variab!e or method.

Definition$
% Te!!s to the ompi!er a!! the detai!s about something.
0.g. a!!o ates the memory for a variab!e; imp!ements the body of a method.
29

C$ De !arations " Definitions


Declaration Variable Method Compound type (Struct / Union) New native type New compound type e*tern int aL void foo7void8L Definition int aL int b K #L stati int K ,1L void foo7void8 O P

.A Dorward de !aration A. .ADe !aration " definitionA. stru t 'L stru t ' O PL .A De !aration " definition A. typedef int tabIsi=eItL .A (hen ' is a forward de !aration A. typedef stru t ' 'L .A(hen ' is de !ared " defined A. typedef stru t ' 'L

22

C$ De !arations " Definitions


Dorward de !aration$
% @ides a!! the detai!s of a type. % A forwarded type an be used on!y as a pointer. % Esefu! to avoid i!!ega! type re ursion. % Esed a!so as design pattern.

2#

C$ De !arations " Definitions


0*amp!e of how to avoid type re ursion$
.A &!!ega! A. .A Lega! A. .A Dorward de !aration$ A. struct ! stru t A O stru t S bL PL stru t S O stru t A aL PL stru t A O struct PL " b!

stru t S O stru t A aL PL 2,

Agenda
C Programming Language Overview. Main Library Methods. Type Modifiers. De !arations " Definitions. Memory. Arrays vs Pointers.
21

C$ Memory
Memory Data

&nstru tions aKbH L 'tati

Dynami

Cead On!y
F@e!!o (or!dTG

Cead.(rite
stati int a K ##L

'ta )
void foo78 O int a K ##L P

@eap
int A a K 7intA8 ma!!o 7?8L

22

C$ Memory
Variables on the stac#%
Stac# a K ## b K ,, void foo78 O int a K ##L int b K ,,L P $eap

2<

C$ Memory
Variables in the heap%
void foo78 O int Aa K 7intA8 ma!!o 7si=eof7int88L int Ab K 7intA8 ma!!o 7si=eof7int88L Aa K ##L Ab K ,,L P Aa K ## Stac# a b Ab K ,, $eap

2?

C$ Memory
&rray on the stac#%
Stac# aQ # R K ,, void foo78 O int aQ , RL aQ 6 R K ##L aQ # R K ,,L P aQ 2 R aQ 9 R aQ 6 R K ## $eap

25

C$ Memory
&rray in the heap%
Stac# a void foo78 O int Aa K 7intA8 ma!!o 7,Asi=eof7int88L aQ 6 R K ##L aQ # R K ,,L P $eap

aQ # R K ,, aQ 2 R aQ 9 R aQ 6 R K ##

#6

C$ Memory
Struct with int si'e e(uals to ) bytes* bloc#s of + byte* in the worst case%
stru t ' O int aL har 9L int bL har 2L PL void foo78O stru t ' sL P ,&-N.N/0 in 1eneral si'eof(S) 23 )"si'eof(int)4)"si'eof(char) Stac# padding s. 2 s.b s.b padding s. 9 s.a s.a $eap

#9

C$ Memory
Union with int si'e e(uals to 5 bytes* and bloc#s of + byte%
union E O int aL har L short bL PL void foo78O union E uL P 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 Modifiers. De !arations " Definitions. Memory. Arrays vs Pointers.
##

C$ Arrays vs Pointers
6ointers are not arrays 22
Look back to previous slides!

% Pointers are references to memory !o ations. % Arrays are !o ations of se(uential memory.
.A &f a shou!d be an array; A avoid thisTT A. void foo7 int A a; unsigned !en 8L .A Prefer thisTT A. void foo7 int aQR; unsigned !en 8L

#,

C$ Arrays vs Pointers
Matri7%
Stac# aQ9RQ9RK,, void foo78 O int aQ 2 RQ 2 RL aQ 6 RQ 6 R K ##L aQ 9 RQ 9 R K ,,L P aQ9RQ6R aQ6RQ9R aQ6RQ6RK## $eap

#1

C$ Arrays vs Pointers
&rray of pointers%
void foo78 O int AaQ 2 RL aQ 6 R K 7intA8ma!!o 72Asi=eof7int88L aQ 9 R K 7intA8ma!!o 72Asi=eof7int88L aQ 6 RQ 6 R K ##L aQ 9 RQ 9 R K ,,L P Stac# aQ9R aQ6R aQ9RQ9RK,, aQ9RQ6R $eap

aQ6RQ9R aQ6RQ6RK##

#2

C$ Arrays vs Pointers
6ointer to pointers%
void foo78 O int AAaL a K 7intAA8ma!!o 72Asi=eof7intA88L aQ 6 R K 7intA8ma!!o 72Asi=eof7int88L aQ 9 R K 7intA8ma!!o 72Asi=eof7int88L aQ 6 RQ 6 R K ##L aQ 9 RQ 9 R K ,,L P aQ6RQ9R aQ6RQ6RK## #< Stac# a $eap aQ9R aQ6R

aQ9RQ9RK,, aQ9RQ6R

C$ Arrays vs Pointers
C 'tring$
% An array of hara ters; terminated by UV6U % har aQR K F@e!!oGL
A string on the sta ) % 2 bytes.

% har A a K F@e!!oGL
A string in readMon!y data memory % 2 bytes % and a pointer on the sta ).

% onst har A a K F@e!!oGL


&dem; but better sin e a!!ows ompi!e time he )s.
#?

C$ Arrays vs Pointers
8hree strin1 e7amples%
Stac# a void foo78 O onst har A a K FAAAGL har b Q R K FSSSGL har A K 7 harA8 ma!!o 7,Asi=eof7 har88L str py7 ; FCCCG8L P bQ#RKUV6U bQ2RKUSU bQ9RKUSU bQ6RKUSU Q#RKUV6U Q2RKUCU Q9RKUCU Q6RKUCU aQ#RKUV6U aQ2RKUAU aQ9RKUAU aQ6RKUAU #5
$eap -ead9nly

UV6U UCU UCU UCU

Potrebbero piacerti anche