碧海潮生's profile碧海潮生的小屋PhotosBlogLists Tools Help

Blog


    06 October

    C语言专题——标准库<stdlib.h>

    1 字符串转换

    double atof (const char*);
    int atoi (const char*);
    long atol (const char*);

    double strtod (const char*, char**);
    long strtol (const char*, char**, int);
    unsigned long strtoul (const char*, char**, int);

    1> 第二组函数的参数意义如下:
    const char* 指向需要转换的字符串
    char**  更新后指向当前数字串之后的一个位置
    int  基数(进制数)

    2> strtol函数举例:

    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>

    int main()
    {
     char *buf = "  0B 00 41 42 43 44 45 46 47 48 49  ";
     char *ptr = buf;
     
      while (isspace(*ptr))
      ptr++;
     while (*ptr != '\0')
     {
      printf("%ld\n", strtol(ptr, &ptr, 16));
      while (isspace(*ptr))
       ptr++;
     }
     
     return 0;
    }
     
    2 随机数

    常量
    #define RAND_MAX 0x7FFF  rand的最大返回值

    函数
    void srand (unsigned int);  置随机数发生器(种子)
    int rand (void);   返回下一个伪随机数
     
    3 内存管理

    常量
    #define NULL ((void *)0)  空指针

    函数
    void* calloc (size_t, size_t); 分配内存, 并清零
    void* malloc (size_t);  分配内存
    void* realloc (void*, size_t); 重新分配内存, 返回新指针
    void free (void*);  释放内存
     
    4 与环境的接口

    常量
    #define EXIT_SUCCESS 0
    #define EXIT_FAILURE 1

    函数
    void abort (void);
    void exit (int);
    int atexit (void (*)(void));

    int system (const char*);
    char* getenv (const char*);
     
    5 查找与排序

    void* bsearch (const void*, const void*, size_t, size_t,
                     int (*)(const void*, const void*));
    void qsort (const void*, size_t, size_t,
                     int (*)(const void*, const void*));

    1> comp函数的返回值
    int comp(const void *p1, const void *p2)
    {
     const int *pi1 = (const int *)p1;
     const int *pi2 = (const int *)p2;
     return *pi1 - *pi2;
    }
    若第一个指针所指向的内容, 排序后应该放在第二个指针所指向的内容之前, 那么应返回负值;
    反之返回正值; 其他情况返回0. 上例中的comp函数将int数组按升序排序.

    2> qsort的调用方法
    qsort((void *)list, length, sizeof(int), comp);
     
    6 整数运算

    结构
    typedef struct { int quot, rem; } div_t;
    typedef struct { long quot, rem; } ldiv_t;

    函数
    int abs (int);
    long labs (long);

    div_t div (int, int);
    ldiv_t ldiv (long, long);
     
    7 多字节字符

    常量
    MB_CUR_MAX  多字节字符中的最大字节数

    函数
    size_t wcstombs (char*, const wchar_t*, size_t);
    int wctomb  (char*, wchar_t);

    int mblen  (const char*, size_t);
    size_t mbstowcs (wchar_t*, const char*, size_t);
    int mbtowc  (wchar_t*, const char*, size_t);

     
    source: 《C & C++ Code Capsules》

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://jx-kingwei.spaces.live.com/blog/cns!F7A152EB74B9576E!724.trak
    Weblogs that reference this entry
    • None