You are on page 1of 5

Training on C Programming Date : 20

th
-24
th
July 98
Topic : miscellaneous Page No : 1 o !
Library Functions
The li"rary unctions is not part o the C language proper# "ut an en$ironment that supports stan%ar%
C &ill pro$i%e the unction %eclarations an% type an% macro %einitions o this li"rary' The unctions#
types an% macros o the stan%ar% li"rary are %eclare% in stan%ar% hea%ers '
(ere are some o the li"rary unctions &hich are re)uently use% &hile &riting a C program '
*tring +unctions
There are t&o groups o string unctions %eine% in ,string'h- ' The irst ha$e names "eginning
&ith .str/ 0 the secon% ha$e names "eginning &ith .mem/ '
1n the ollo&ing ta"le # the $aria"les .s/ #/t/ # .cs/ an% .ct/ are o type .char 2/ '
The $aria"le .n/ is o type .si3e4t/ 5 si3e4t is unsigne% int6 an% .c/ is o type char'
+unction +unctionality
char 2strcpy5s# ct6 Copy string ct to string s# inclu%ing .70/0 return s '
char 2strncpy5s# ct# n6 Copy at most n characters rom string ct to string s# inclu%ing
.70/0 return s ' Pa% &ith .70/ i ct has e&er than n characters'
char 2strcat5s# ct6 Concatenate string ct to en% o string s 0 return s
char 2strncat5s# ct# n6 Concatenate at most n characters o string ct to s 0 terminate s
&ith .70/0return s
char 2strcmp5cs# ct6 Compare string cs to string ct 0 return , 0 i cs,ct# 0 i cs 8 8 ct#
or - 0 i cs - ct'
char 2strncmp5s# ct#n6 Compare at most n characters o string cs to string ct 0 return , 0
i cs,ct# 0 i cs 8 8 ct# or - 0 i cs - ct'
char 2strchr5cs# c6 9eturn pointer to irst occurrence o c in cs or N:;; i not
present
char 2strrchr5cs# c6 9eturn pointer to last occurrence o c in cs or N:;; i not
present
char 2strto<5s# ct6 *earches s or to<ens %elimite% "y characters rom ct
si3e4t strlen5cs6 9eturn length o cs
+ollo&ing are some o the .mem/ unctions :
+unction +unctionality
$oi% 2memcpy5s# ct# n6 Copy n character rom ct to s# return s
$oi% 2memcmp5cs#ct#n6 Compare the irst n characters o cs &ith ct0 return as &ith
strcmp
$oi% 2memchr5cs#c#n6 9eturn pointer to irst occurrence o character c in cs# or
N:;; i not present among irst n characters '
$oi% 2memset5s#c#n6 Place character into irst n characters o s# return s
Mathematical Functions
The hea%er ,math'h- %eclares mathematical unctions an% macro '
1n the ollo&ing ta"le# = an% y are o type %ou"le ' >ngles o trigonometric
unctions are e=presse% in ra%ians '
+unction +unctionality
sin5=6 *ine o =
cos5=6 Cosine o =
asin5=6 *in
-1
=
acos5=6 ?=ponential unction e
=
e=p5=6 Natural logarithm ln5=6# =-0
log5=6 @ase 10 logarithm log105=6# =-0
po&5=#y6 =
y
A$arA&&&AappsAcon$ersionAtmpAscratch41A24B889CB9'%oc
@D : EF
G Copyright Eahin%ra @ritish Telecom 1998
Training on C Programming Date : 20
th
-24
th
July 98
Topic : miscellaneous Page No : 2 o !
a"s5=6 >"solute $alue H = H
s)rt5=6 *)ure root o =
Utility Functions
The hea%er ,st%li"'h- %eclares unctions or num"er con$ersion# storage allocation# an% similar
tas<s '
Function Functionality
%ou"le ato5 char 2s6 Con$erts string s to %ou"le
int atoi5char 26 Con$erts string s to integer
long atol5 char 2s6 Con$erts string s to long
int ran%56 9eturns a pseu%o-ran%om integer in the range 0 to
9>ND4E>I '
int system5char 2s6 This unction passes the string s to the en$ironment or
e=ecution'
char 2geten$5char 2name6 9eturns the en$ironment string associate% &ith name#
N:;; i no string e=ists '
Error Functions
The error unctions are %eclare% in hea%er ile ,errno'h- ' This ile also contains the e=pression
errno &hich may contain an error num"er that gi$es urther inormation a"out the most recent
error '
+unction +unctionality
int eo5 +1;? 2stream 6 9eturns non-3ero i the en% o ile in%icator or stream is
set
$oi% perror5 char 2s6 Prints string .s/ an% error message correspon%ing to integer
in $aria"le errno '
File Positioning Function
These are %eclare% in hea%er ile Jst%io'hK '

+unction +unctionality
int see<5+1;? 2stream# long
oset# int origin6
This sets the ile position or stream # a su"se)uent rea% or
&rite &ill access %ata "eginning at the ne& position '
long tell5+1;? 2stream6 This returns the current ile position or stream '
$oi% re&in%5+1;? 2stream6 This sets the ile position at the "eginning o the stream'
int getpos5+1;?2 stream# pos4t
2ptr6
9ecor%s the current position in the stream in 2ptr# or
su"se)uent use "y setpos'
int setpos5+1;?2 stream# pos4t
2ptr6
Positions the stream at the position recor%e% "y getpos in
2ptr'
Character Class Tests
The hea%er ,ctype'h- %eclares unctions or testing characters' The unction return non-3ero
5true6 i the argument c satisies the con%ition %escri"e% an% 3ero i not'
+unction +unctionality
is%igit5 c 6 Decimal %igit
islo&er5 c 6 ;o&er-case letter
isupper5 c 6 :pper-case letter
isspace5 c 6 *pace# ormaee%# ne&line# carriage return# ta"
A$arA&&&AappsAcon$ersionAtmpAscratch41A24B889CB9'%oc
@D : EF
G Copyright Eahin%ra @ritish Telecom 1998
Training on C Programming Date : 20
th
-24
th
July 98
Topic : miscellaneous Page No : C o !
is=%igit5 c 6 (e=a%ecimal %igit
isprint5 c 6 Printa"le character
Date And Time Functions
The hea%er ,time'h- %eclares types an% unctions or manipulating %ate an% time ' time4t is the
%ata type use% to represent times an% structure tm hol%s the components o the calen%ar time '
int tm4sec *econ%s ater the minute 5 0# B1 6
int tm4min Einutes ater the hour 5 0# !9 6
int tm4hour (ours since mi%night 5 0# 2C6
int tm4m%ay Day o the month 5 1# C16
int tm4mon Eonth since January 5 0# 116
int tm4year Dear since 1900
int tm4&%ay Days since *un%ay 5 0# B6
int tm4y%ay Days since January 5 0# CB! 6
+ollo&ing are the unctions use% or time manipulation :
+unction +unctionality
time4t time5time4t 2tp6 9eturns the current calen%ar time '
%ou"le %itime5time4t
time2# time4t time16
9eturns the %ierence "et&een the t&o time2 an% time 1
e=presse% in secon%'
time4t m<time 5struct
tm2tp6
Con$erts the local time in the structure 2tp into calen%ar time
in the same representation use% "y time
char 2asctime5struct tm 2tp6 Con$erts the structure 2tp into string
char 2ctime5time4t 2tp6 Con$erts the calen%ar time 2tp to local time'
struct tm 2localtime5time4t
2tp6
Con$erts the calen%ar time 2tp into local time '
Coding Style And Standards
C +ile Lrgani3ation
1' >ny hea%er ile inclu%es shoul% "e the irst thing in the ile '
2' 1mme%iately ater the inclu%es shoul% "e prologue that tells &hat is in the ile '
C' >ny type%es %eines that apply to the ile as a &hole are ne=t'
4' Ne=t comes the glo"al5e=ternal6 %ata %eclarations'
!' The unction come last '
(ea%er +iles
1' (ea%er iles are use% to contain the %ata %eclarations an% the %eines that are nee%e% "y more
than one program '
2' (ea%er iles shoul% "e unctionally organi3e%# i'e' %eclarations or separate su"systems
shoul% "e separate hea%er iles'
C' 1 set o %eclarations is li<ely to "e change &hen the co%e is porte% rom one machine to
another # those %eclarations shoul% "e in separate hea%er ile'
4' (ea%er ile shoul% not "e neste% ' *ome o"Mects li<e type%es an% initiali3e% %ata %einitions
cannot "e seen t&ice "y the compiler in one compilation'
?=ternal Declarations
A$arA&&&AappsAcon$ersionAtmpAscratch41A24B889CB9'%oc
@D : EF
G Copyright Eahin%ra @ritish Telecom 1998
Training on C Programming Date : 20
th
-24
th
July 98
Topic : miscellaneous Page No : 4 o !
1' ?=ternal %eclarations shoul% "egin in column 1'
2' ?ach %eclaration shoul% "e on a separate line ' > comment %escri"ing the role o the o"Mect
"eing %eclare% shoul% "e inclu%e% '
Comments
1' Comments that %escri"e the %ata structure # algorithms etc' *houl% "e in "loc< comment
orm &ith the opening A2 an% closing 2A
e'g'
A2
2 This is the e=ample o "loc< comment
2 Comment shoul% "e ta""e% o$er
2A
2' Fery short comments may appear on the same line as the co%e they %escri"e'
@ut the comment shoul% "e ta""e% o$er '
e'g'
print5J(elloK60 A2 printing hello 2A
+unction Declarations
1' ?ach unction shoul% "e prece%e% "y a "loc< comment prologue that gi$es the name an% a
short %escription o &hat the unction %oes '
2' 1 the unction returns a $alue# the type o the return $alue returne% shoul% "e alone on a line
in column 1 5 %o not %eault to int 6'
Compoun% statement
1' Compoun% statements are statements are those &hich contain lists o statements enclose% in
"races ' The enclose% list shoul% "e ta""e% o$er more than the ta" position o the compoun%
statement itsel '
2' The opening let "race shoul% "e at the en% o the line "eginning the compoun% statement
an% the closing right "race shoul% alone on a line# ta""e% un%er the "eginning o the
compoun% statement '
e'g'
i5e=pr6N
statement0
statement0
Oelse N
statemnt
statement
O
Naming Con$entions
1' >n initial un%erscore shoul% not "e use% or any user create% names' :N1I uses it or
creating system li"rary'
2' Eacro names type%e # an% preprocessor names shoul% "e all in C>P* '
C' Ne$er use system call names as $aria"les'
Lther Consi%erations
1' Poto statements shoul% "e use% sparingly as in &ell-structure% co%e'
2' The logical e=pression operan% "eore ternary operator 5 Q : 6 shoul% "e parenthesi3e% '
e'g 5 =-8 06 Q = : =
C' There is time an% place or em"e%%e% assignments ' +or e'g
The co%e
a 8 " R c
% 8 a R r
shoul% not "e replace% "y
% 8 5a8"Rc6 R r
A$arA&&&AappsAcon$ersionAtmpAscratch41A24B889CB9'%oc
@D : EF
G Copyright Eahin%ra @ritish Telecom 1998
Training on C Programming Date : 20
th
-24
th
July 98
Topic : miscellaneous Page No : ! o !
4' 1n%i$i%ual proMects may &ish to esta"lish a%%itional stan%ar%s # particularly system prei=
con$entions or unctional grouping o glo"al %ata an% also or structure or union mem"er names
can "e useul'

A$arA&&&AappsAcon$ersionAtmpAscratch41A24B889CB9'%oc
@D : EF
G Copyright Eahin%ra @ritish Telecom 1998

You might also like