2009年11月28日 星期六

c language standard library function


Reference website: http://www.cplusplus.com/reference/clibrary/ 
Reference: "Embedded Linux, C-language programming practice" 
In fact, in reference to web pages, you can already find all the C language standard library functions. Here I only list what I learned: 
  C-set of the standard library functions are: 
     : the standard set of input and output functions. 
     : a set of function determining character type (whether upper case, numbers, spaces)

     : a set of function Operating string. 
     : a set of mathematical applications related functions. 
     : tool set, including the type conversion and some system functions. 
     : a set of additional assertion functions. 
     : variable parameters, tool kit. such as the printf function will use this package. 
     : a set of function supporting jump feature

     : a set of processing interrupts. 
     :a set of function dealing with the date and time
     : integer type maximum and minimum set.

     : maximum and minimum set of floating-point type.

1. Standard formal input and output class function 
    Header files are involved in stdio.h and stdarg.h 
   * scanf function: Format input string 
   * printf functions: formatting output strings 
   * putchar function: the output characters to the standard output 
   * getchar function: get character from standard input 
   * putc function: output characters to the file 
   * getc function: get characters from a file 
           while ((c = getchar ()! = EOF)) putchar (c) 
           c = getc (pFile) 
           getchar () is equivalent to getc (stdin) 
   * gets function: get the string 
   * puts the function: output the specified string 
   * ungetc function: the return of characters to write


2. Character processing and conversion functions 
   * sprintf function: formatted output string to a buffer 
           sprintf (s, "% d", 123) 
           sprintf (s, "% 08X", 456); turn 16 hex 
           sprintf (s, "% 10.3f", 3.1434) 
   * strcat and strncat functions: string concatenation 
          char * strncat (char * dest, const char * src, size_t n) 
   * strcpy and strncpy functions: string copying 
          char strncpy (char * dest, const char * src, size_t maxlen) 
   * strcmp and strncmp functions: String Comparison 
           int strcmp (const char * s1, const char * s2) 
           s1> s2 return 1 
           s1 = s2 return 0 
           s1
   * strlen function: Get the string length of the 
   * strchr and strrchr function: Character / String location 
           strchar: search a character position of first occurrence 
           strrchr: search a character position of last occurrence 
   * strstr function: string search 
   * strupr and strlwr function: to convert the letters 
   * strdup and strndup function: string copying 
           char * strdup (const char * s) 
           The string s to copy to the specified memory cell


3. Math class functions counting 
    div acos atan cos tan cosh exp frexp ldexp log modf pow sqrt ceil abs floor


4. Data structures and algorithms class functions 
     * bsearch function: binary search 
     * lfind function: linear search 
     * lsearch function: linear search 
     * qsort function: the use of ordered array of quick sort method 
     * rand function: generate random number


5. File I / O operation class correlation function 
     * fopen function: open file 
     * fclose function: close file 
     * fgetc function: read a character from a file 
            fp = fopen ( "exit", "r") 
            while ((c = fgetc (fp))! = EOF) 
     * fputc function: to a specified character is written to the file stream 
     * fgets function: read a string from a file 
     * fputs function: to a specified string into the document
     * rewind function: Reset the file position of the file stream to read and write at the beginning of 
     * ftell function: get the file stream to read position 
     * fseek function: get the file stream to read position 
     * fwrite function: to build the file to write to the file stream 
     * fread function: read data from the file stream 
     * fgetpos function: get the file location 
                The stream's current location is recorded in the * position for the subsequent fsetpos () call using the 
     * fsetpos function: Set File Location


6. Utility functions 
     * assert function: diagnostic procedures 
     * setjmp function: to save call

C language extensions library function


Reference: "Embedded Linux, C-language programming practice" 

1. File I / O operations functions 

   * open function: open file 
          int open (const char * pathname, int flags) 

   * close function: close file 

   * read function: read the file 
          ssize_t read (int_fd, void * buf, size_t count) 
            transfer bytes of count number into point buf
         

   * write function: write file 
          ssize_t write (int fd, const void * buf, size_t count) 
          write bytes of count number into file(fd) with beginning of point buf in the memory 

   * lseek Function: File Positioning 
          off_t lseek (int fildes, off_t offset, int whence); 
          retuen the current position of reading and writing 

   * flock function: lock file 
          int flock (int fd, int operation) 

   * mmap function and munmap functions: memory-mapped 
          int munmap (void * start, size_t length) 
         map the contents of a file into memory, to write or read this memory region is the direct access to to the content of the document. 

   * create a function: to create a new file 
          int create (const char * pathname, mode_t mode) 

   * fcntl function: to change the file attributes which has been opened 

          int fcntl (int fields, int cmd, int arg) 
          If success,return the implementation of the new descriptor, otherwise returns -1 

2. the function of related file permissions

   * access functions: to determine whether access to the file permissions 
          int access (const char * pathname, int mode) 
          if (0 == access (argv [1], W_OK)) R_OK X_OK F_OK 

   * chown function and fchown function: to change the file owner of the 
          int fchown (int fd, uid_t owner, gid_t group) 

   * chmod function and fchmod function: to change the permissions 
         int fchmod (int fildes, mode_t mode) 

   * unlink function: delete the file 

3. User group manipulation functions 

   * getegid function and setegid function: get / set the effective group ID 
         gid_t gete (gid_t egid) 

   * geteuid function and seteuid function: get / set the real user ID 

         uid_t getuid (void) 

  4. Signaling Function 

    * kill function: to send a signal to the specified process

         int kill (pid_t pid, int sig) 

    * rais function: signaling 

         int raise (int sig) 

    * alarm function: Set the timer 

         unsigned int alarm (unsigned int seconds) 

    * signal function: Signal Installation Function 
         sighandler_t signal (int signum, sighandler_t handler) 

5. The process of dealing with the function 

    * getpid function and getppid function: to obtain the process ID and the parent process ID 
         pid_t getpid (void) 
         pid_t getppid (void) 

    * fork function: the establishment of child process 
         pid_t fork (void) 
         The parent process has been the return value is the child process ID, while the child process returns 0 

    * sleep function: Let the process was suspended for a period of time 
         unsigned int sleep (unsigned int seconds) 

    * exec function: find the executable file 

    * _exit function: the end of the process of implementation of the

function pointer


Reference: "Embedded Linux, C-language programming practice" 

1. The concept of function pointers: 
   In the C language: the nature of the pointer is a memory address, function pointer is a pointer to a function of the code in the code segment address of the pointer. 
   For the function, its address and function name represents the same meaning 
   In the C language programming, data structures and algorithms are two basic elements, through a function pointer, the algorithm can be embedded into data structure. 

  

2. The usage of function pointers 

  
*the basic usage
    int add (int a, int b) 
    {....} 
    int sub (int a, int b) 
    {....} 
    int main ()
    ( 
      int (* pf) (int, int); 
      pf = add; 
      result = pf (100,200); 
      pf = sub; 
      result = pf (100,200); 
     )


   
* Type conversion: 
   int add (int a, int b) 
    {....} 
    int sub (int a, int b) 
    {....} 
    int main () 
    ( 
      void * pf; 
      pf = add; 
      result = (int (*) (int, int) pf) (100,200); 
      pf = sub; 
      result = (int (*) (int, int) pf) (100,200); 
     )


   
 * Function pointer type definition 
   typedef int (* fun_t) (int, int); 
   fun_t pf; 
   pf = add; 
   result = pf (100,200);


    
* Function pointer as a structure member 
struct source 

   int a; 
   int b; 
   fun_t operation; / / equivalent to int * fun_t (int, int); 
); 
int main (int argc, char * argv []) 

   struct source data; 
   int result; 
   data.a = 200; 
  
 data.b = 100; 
   data.operation = add; 
   result = data.operation (data.a, data.b); 


   
* Function pointer being as a parameters of the function 
int calculate (int a, int b, fun_t operation) 

   int result; 
   result = operation (a, b); 
   return result; 

  int main (int argc, char * argv []) 

   int a, b, result; 
   a = 200; 
   b = 100; 
   result = calculate (a, b, add); 
   ) 

   
* Function pointer as the function return value 
fun_t getoperation (char a) 

   fun_t result; 
   switch (a) 
  ( 
   case "+"; 
       result = add; 
       break; 
   case "-"; 
       result = sub; 
       break; 
   ) 
    return result; 

int main (int argc, char * argv []) 

   int a, b, result; 
   char oper; 
  
 a = 200; 
   b = 100; 
   oper ="+"; 
   result = getoperation (oper) (a, b); 
)


 
  * Array of function pointers 
enum ( 
       oper_add = 0, 
       oper_sub 

static const fun_t oper_table [oper_num] = 

   add, 
   sub 

int main (int argc, char * argv []) 

   int a, b, result; 
   a = 200; 
   b = 100; 
   result = oper_table [oper_add] (a, b); 
)

Dynamic memory Heap and Stack


Reference: "Embedded Linux, C-language programming practice" 

1. The use of program memory area 

   Storage area is divided into: static and dynamic 

                 Static: read-only data area (RO DATA), has been initialized to read and write data area (RW DATA), an uninitialized read-write data area (BSS:
Block Started by Symbol


                 dynamic: heap Memory (heap) and stack memory (stack) 

   Heap memory is from low address to high address allocation, stack memory is from high address to low address allocation 

2.C program stack space usage 

   In the C language program, the stack space is managed by the compiler, can be reflected in the program stack space using the example of parameter passing, return values, and automatic variable is the use of space. 

3.C program heap space usage 

   In the C language program, the heap memory allocation and release is done by calling the library function, and their use need to include standard library files: 
    # include
 

  There are four functions to achieve the heap memory allocation and release: 
     void * malloc (size_t size) to allocate memory space 
     void free (void * ptr) free memory space 
     void * calloc (size_t nmemb, size_t size) to allocate memory space 
     void * realloc (void * ptr, size_t size) to re-allocate memory space 

4. Heap memory and stack memory usage comparison 
    The swap of the most classic examples: 
   void swap (int a, int b) 
   ( 
     int tmp; 
     tmp = b; 
     b = a; 
     a = tmp; 
     return; 
   ) 

    This swap can not be achieved, because, swap function is called when you create a new stack space, when the stack upon return, the stack area has been released. Therefore, we must use the pointer. 

   I seriously appreciate the two examples: 

typedef struct _S100 



   char string [100]; 

) S100; 

  

void fun_para1 (S100 a) 



printf ( "From para:% s \ n", a.string); 

strcpy (a.string ,"-----"); 

printf ( "fun_para1 change:% s \ n", a.string); 

return; 



void test_fun_para1 (void) 



S100 a; 

strcpy (a.string, "+++++") 

fun_para1 (a); 

printf ( "after fun_para1 return:% s \ n", a.string); 

return; 



Running Results: 

From para :+++++ 

fun_para1 change: ----- 

after fun_para1 return :+++++ 

  

void fun_para3 (char a [100]) 



strcpy (a ,"-----"); 

printf ( "fun_para1 change:% s \ n", a); 

retrun; 



void test_fun_para3 (void) 



char a [100]; 

strcpy (a ,"+++++"); 

fun_para3 (a); 

printf ( "after fun_para3 return:% s \ n", a); 

return; 



Running Results: 

from para :+++++ 

fun_para1 change :----- 

after fun_para3 return :----- 

  

From the run results, such a conclusion can be drawn:

when structure being used as parameters in function, the entire structure has been pushed onto the stack memory, just like a variable. 
when array being used as a parameter in function, it will be handled as a pointer