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
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.
(nput / Output
@eader stdio.h
' int print 8C(L1A stream< "onst "harA ormat<B9
As print < but on a stream.
(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.
(nput / Output
@eader string.h
' si=eHt str#en8 "onst "har A s 9
Returns the #ength o a string.
(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.
Agenda
C Programming Language Overview. Main Library Methods. Type Modi iers. !e"#arations $ !e initions. Memory. Arrays vs Pointers.
:-
:2
' >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
' 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/
:;
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
!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:
/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
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
!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.
%%
' 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
%-
%2
aP7QP:Q aP7QP7QJ%%
%3
aP:QP:QJ-aP:QP7Q