4
.
6
.
8
.
4
、函数库中库函数的使用
( 1 )gcc中编译链接程序默认是使用动态库的,要想静态链接需要显式用 - static 来强制静态链接。
( 2 )库函数的使用需要注意 3 点:第一,包含相应的头文件;第二,调用库函数时注意函数原型;第三,有些库函数链接时需要额外用 - lxxx来指定链接;第四,如果是动态库,要注意 - L指定动态库的地址。
4 . 6 . 9 . 3 、常用字符串处理函数
( 1 )C库中字符串处理函数包含在string.h中,这个文件在ubuntu系统中在 / usr / include 中
( 2 )常见字符串处理函数及作用:
( 1 )gcc中编译链接程序默认是使用动态库的,要想静态链接需要显式用 - static 来强制静态链接。
( 2 )库函数的使用需要注意 3 点:第一,包含相应的头文件;第二,调用库函数时注意函数原型;第三,有些库函数链接时需要额外用 - lxxx来指定链接;第四,如果是动态库,要注意 - L指定动态库的地址。
4 . 6 . 9 . 3 、常用字符串处理函数
( 1 )C库中字符串处理函数包含在string.h中,这个文件在ubuntu系统中在 / usr / include 中
( 2 )常见字符串处理函数及作用:
void *memcpy(void *dest, const void *src, size_t n);
//function:The memcpy() function copies n bytes from memory area src to memory area dest. The memory areas must not overlap. Use memmove(3) if the memory areas do overlap.
//return: a pointer to dest.
//remarks:确定src和dst不会overlap,则使用memcpy效率高
void *memmove(void *dest, const void *src, size_t n);
//function:The memmove() function copies n bytes from memory area src to memory area dest. The memory areas may overlap:copying takes place as though the bytes in src are first copied into a temporary array that does not overlap src or dest, and the bytes are then copied from the temporary array to dest.
//return:returns a pointer to dest.
//remarks:确定会overlap或者不确定但是有可能overlap,则使用memove比较保险
void *memset(void *s, int c, size_t n);
//memory set
//The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c.总的作用:将已开辟内存空间 s 的首 n 个字节的值设为值 c。他的填充是以字节为单位,一般用于对字符型变量的初值进行赋值.用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为‘ ’或‘/0’;例:char a[100];memset(a, '/0', sizeof(a));
//return: a pointer to the memory area s.
int memcmp(const void *s1, const void *s2, size_t n);
//memory compare
//The memcmp() function compares the first n bytes (each interpreted as unsigned char) of the memory areas s1 and s2.(一般用来比较两个字符串是否相等)
//return:当s1<s2时,返回值-1;当s1==buf2时,返回值=0;当buf1>s2时,返回值1
void *memchr(const void *s, int c, size_t n);
//在参数 str 所指向的字符串的前 n 个字节中搜索第一次出现字符 c(一个无符号字符)的位置。
//str -- 指向要执行搜索的内存块。
//c -- 以 int 形式传递的值,但是函数在每次字节搜索时是使用该值的无符号字符形式。
//n -- 要被分析的字节数。
//返回值:该函数返回一个指向匹配字节的指针,如果在给定的内存区域未出现字符,则返回 NULL。
char *strcpy(char *dest, const char *src);
//The strcpy() function copies the string pointed to by src, including the terminating null byte ('\0'), to the buffer pointed to by dest.
//return: a pointer to the destination string dest.
char *strncpy(char *dest, const char *src, size_t n);
// The strncpy() function is similar, except that at most n bytes of src are copied.
//return: a pointer to the destination string dest.
char *strcat(char *dest, const char *src);
//The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result.
//return: a pointer to the resulting string dest.
char *strncat(char *dest, const char *src, size_t n);
//The strncat() function is similar, except that it will use at most n bytes from src; and src does not need to be null-terminated if it contains n or more bytes.
//return:a pointer to the resulting string dest.
int strcmp(const char *s1, const char *s2);
//字符串大小的比较是以ASCII 码表上的顺序来决定,此顺序亦为字符的值。strcmp()首先将s1 第一个字符值减去s2 第一个字符值,若差值为0 则再继续比较下个字符,若差值不为0 则将差值返回。例如字符串"Ac"和"ba"比较则会返回字符"A"(65)和'b'(98)的差值(-33)。
int strncmp(const char *s1, const char *s2, size_t n);
//strncmp()首先将s1 第一个字符值减去s2 第一个字符值,若差值为0 则再继续比较下个字符,直到字符结束标志'\0',若差值不为0,则将差值返回。例如字符串"Ac"和"ba"比较则会返回字符"A"(65)和'b'(98)的差值(-33)。
//若str1与str2的前n个字符相同,则返回0;若s1大于s2,则返回大于0的值;若s1 若小于s2,则返回小于0的值。
char *strdup(const char *s);
//函数说明:strdup()会先用maolloc()配置与参数s 字符串相同的空间大小,然后将参数s 字符串的内容复制到该内存地址,然后把该地址返回。该地址最后可以利用free()来释放。
//返回值:返回一字符串指针,该指针指向复制后的新字符串地址。若返回NULL 表示内存不足。
char *strndup(const char *s, size_t n);
//The strndup() function is similar, but copies at most n bytes. If s is longer than n, only n bytes are copied, and a terminating null byte ('\0') is added.
char *strchr(const char *s, int c);
//strchr() 将会找出 str 字符串中第一次出现的字符 c 的地址,然后将该地址返回。
//【返回值】如果找到指定的字符则返回该字符所在地址,否则返回 NULL。
char *strstr( char *str, char * substr );
//strstr()函数用来检索子串在字符串中首次出现的位置
//【参数说明】str为要检索的字符串,substr为要检索的子串。
//【返回值】返回字符串str中第一次出现子串substr的地址;如果没有检索到子串,则返回NULL。
char * strtok(char *s, const char *delim);
//strtok()用来将字符串分割成一个个片段。参数s 指向欲分割的字符串,参数delim 则为分割字符串,当strtok()在参数s 的字符串中发现到参数delim 的分割字符时则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s 字符串,往后的调用则将参数s 设置成NULL。每次调用成功则返回下一个分割后的字符串指针。
//返回下一个分割后的字符串指针,如果已无从分割则返回NULL。
//用法:http://blog.csdn.net/beyondhaven/article/details/6679206
int atoi(const char *nptr); //将字符串转为int格式的数
long atol(const char *nptr);
long long atoll(const char *nptr);
long long atoq(const char *nptr);