Sei sulla pagina 1di 24

Lecture

   08:  Standard  Libraries  


2  
https://www.dartmouth.edu/~rc/classes/softdev_linux/Images/multifile_compile.gif  
¡  Object  files  are  compiled  code  for  functions  and  
additional  information  to  use  the  functions.  
§  produced  by  the  compiler  or  the  assembler  
§  one  for  each  *.c  file  
¡  Linked  with  other  object  files  to  produce  an  
executable  
§  There  must  only  be  one  definition  of  each  function  
¡  A  library  is  a  file  that  serves  as  a  collection  of  
object  files.  It  is  also  referred  to  as  an  archive.  

3  
¡  Header  files  contain  C  type  descriptions,  
function  prototypes,  macros  and  special  
variable  declarations.  
¡  These  files  contain  valid  C  source  code  but  
typically  does  not  produce  an  executable  
program  on  its  own.  
¡  Conventionally  named  *.h  
¡  Used  with  #include  command  

4  
¡  a  set  of  libraries  that  are  available  in  any  
standards-­‐compliant  C  compiler  

¡  each  function  has  a  necessary  C  header  file  


that  should  be  included  

5  
<assert.h>   Diagnostics   <stddef.h>   Common  definitions  
<ctype.h>   Character  handling   <stdio.h>   Input  and  output  
<errno.h>   Errors   <stdlib.h>   General  Utilities  
<float.h>   Floating  point  charac.   <string.h>   Memory  and  String  handling  
<limits.h>   Sizes  of  Integer  types   <tgmath.h>   Type-­‐generic  math  
<locale.h>   Localization     <time.h>   Date  and  Time  
<math.h>   Mathematics   <wchar.h>   Extended  multibyte  
<setjmp.h>   Non-­‐local  jumps   and  wide  character  utilities  
<signal.h>   Signal  handling   <wctype.h>   Wide  character  classification  
<stdarg.h>   Variable  arguments   and  mapping  utilities    
<stdbool.h>   Boolean  type  and  values  

6  
¡  Portability  refers  to  the  ability  of  the  code  to  
be  used  in  different  environments  (e.g.  
different  OS,  computer  architecture  J  )  
¡  With  standard  libraries:  
§  No  need  to  be  concerned  with  system-­‐dependent  
implementation  of  the  standard  library  functions  
§  Porting  to  different  systems  is  typically  as  easy  as  
recompiling  the  program  source  

7  
¡  Defines  the  assert()  macro  which  outputs  the  file,  line  number,  function  
and  assertion  expression  when  the  assertion  fails.  
§  void  assert(int  expr);  

Example    
 void  foo(int  n){  
   assert(n  >  0);  
   /*  do  important  stuff  here  */  
 }  

¡  When  assertion  fails,  the  program  stops  with  the  message:  
§  prog:  prog.c:12:  foo:  Assertion  `n  >  0'  failed  

8  
¡  Defines  macros  used  to  test  a  char  argument  
and  two  functions  that  map  a  char  argument  
(to  upper/lower  case)  

9  
¡  Using  the  macros  in  ctype  works  regardless  of  the  
character  set  used  by  the  machine  (not  all  machines  
use  ASCII).  
¡  Using  the  macros  is  straightforward:  
char  mychar='c';  
if  (isupper(mychar)){  
   printf("the  character  %c  is  upper  case",  mychar);  
}  
if  (islower(letter)){  
   printf("the  character  %c  is  lower  case",  mychar);  
}  

10  
¡  Floating  point  characteristics  (particular  to  
the  implementation  on  each  machine)  and  
limits  (minimum,  maximum,  resolution)  
¡  Examples  
§  DBL_MAX  -­‐  maximum  value  of  a  double  
§  DBL_EPSILON  –  the  minimum  magnitude  
representable  
§  FLT_DIG  –  number  of  decimal  digits  that  can  
reliably  be  represented  given  the  limitations  in  
floating  point  representation  
11  
¡  limits.h  contains  macros  defining  various  integer  type  
characteristics  
¡  Examples:  
§  CHAR_BIT    /*  number  of  bits  in  a  byte  */  
§  WORD_BIT  /*  number  of  bits  in  an  int  */  
§  INT_MIN    /*  minimum  value  of  an  int  */  
§  for  more  info,  refer  to  the  manpage  entry  for  limits.h  

¡  Useful  if  your  program  needs  to  work  on  multiple  
different  systems  and  it  relies  on  knowing  machine  
dependent  integer  type  characteristics.  

12  
¡  sin(),  cos(),  tan(),  acos(),  asin(),  atan()  
▪  Uses  RADIANS  
¡  cosh(),  sinh(),  tanh()  –  hyperbolic  functions  
¡  exp(),  log(),  log10()  –  exponentiation  (base  is  the  natural  
number  e),  logarithms  (log  returns  the    base  e  logarithm  of  
the  argument).  
¡  ceil(),  floor()  –  ceiling  and  floor  functions  
¡  fabs()  -­‐  floating  point  absolute  value  
¡  fmod()  –  floating  point  modulo  division  
¡  pow()  -­‐  power  function  
¡  sqrt()  -­‐  square  root  function  
13  
¡  printf,  scanf  and  related  functions  
¡  basic  C  file  I/O,  character  I/O  
¡  File  I/O  funtions  and  definitions  (to  be  
discussed)  

14  
¡  Contains  assorted  functions  and  definitions  for  
general  use  
¡  Contains  functions  for  
§  Dynamic  memory  allocation  :  calloc(),  malloc(),  realloc(),  
free()  
§  Searching  and  sorting  :  bsearch(),  qsort()  
§  Pseudo  random  number  generation  :  rand(),  srand()  
§  Communicating  with  the  environment  :  getenv(),  system()  
§  Integer  arithmetic  :  abs(),  labs(),  div(),  ldiv()  
§  String  conversion  :  ato..(),  strto...()  
§  Multibyte  character  functions  and  string  functions  
§  Program  termination  :  abort(),  atexit(),  exit()  

15  
16  
¡  Property  of  a  system  to  be  decomposed  into  
smaller,  self-­‐contained  parts.  
§  A  system  built  using  a  divide-­‐and-­‐conquer  
strategy  will  exhibit  a  degree  of  modularity.  
¡  Each  self-­‐contained  part  is  called  a  module.  
§  Contents  of  each  module  are  ideally  related  
(associated  with  the  a  common  object/task)  
§  The  system's  functionalities  can  be  traced  to  
specific  modules.  
17  
¡  System  complexity  becomes  harder  to  
manage  as  it  gets  bigger.  (imagine  
maintaining  1,000+  lines  of  code).  
¡  Smaller,  more  focused  problems  are:  
§  easier  to  solve    
§  easier  to  understand    
§  easier  to  test  against  a  solution  
¡  Software  systems  have  an  uncanny  tendency  
to  grow  larger  than  originally  planned.  
18  
¡  Modules  in  C  can  be  a:  
§  library  
§  source  file  
§  function  
¡  The  guideline  is  to  keep  those  things  that  
support  a  particular  functionality  close  
together.  

19  
¡  Workspace  refers  to  the  group  of  source  code,  
header  files  and  libraries  used  to  build  the  output  
§  Sometimes  referred  to  as  project    
¡  Separate  source  files  are  created,  each  having  its  
own  header  file  
¡  Only  one  source  file  in  the  workspace  will  
contain  the  main(  )  function  
§  This  calls  functions  from  other  source  files  in  the  
workspace  
§  Usually,  this  source  file  contains  ONLY  the  main  
function  

20  
¡  myprogram.c   ¡  util.c  

#include <stdio.h>
int get_factorial(int val)

#include “util.h”
{

int main(void)

if (val == 0 || val == 1)

{


return 1;


int num;

else if (val > 1)


scanf(“%d”, &num);


return val*get_factorial(val-1);


if (num < 0 || num > 12)

else



return 1;


return -1;


num = get_factorial(num);
}


printf(“%d\n”, num);


return 0;
¡  util.h  
}

int get_factorial(int val);

21  
build  command:  $gcc  myprogram.c  util.c  

22  
¡  When  myprogram.c  is  compiled,  it  assumes  
get_factorial()  is  implemented  elsewhere  
§  Definition/usage  of  get_factorial()  is  detected  by  
preprocessor  in  util.h  

¡  Linker  checks  if  actual  definition  exists  for  all  


references    
§  Also  checks  for  duplicates  (e.g.  main()  exist  in  util.c)  
§  Flags  an  error  if  get_factorial(  )  was  not  found  in  any  
of  the  object  files  

23  
24  

Potrebbero piacerti anche