WINDOWS下的程序员出身,偶尔也写一些linux平台下小程序, 后转行数据库行业,专注于ORACLE和DB2的运维和优化。 同时也是ios移动开发者。欢迎志同道合的朋友一起研究技术。 数据库技术交流群:58308065,23618606
全部博文(599)
分类: C/C++
2012-01-05 12:35:27
|
|
原型:extern int bcmp(const void *s1, const void *s2, int n);
用法:#include
功能:比较字符串s1和s2的前n个字节是否相等
说明:如果s1=s2或n=0则返回零,否则返回非零值。bcmp不检查NULL。
举例:
/home/cpicsrv/yansp/cstring > cat bcmp.c
//bcmp.c
#include
#include
int main(void)
{
char *s1="Hello Huateng!";
char *s2="Hello huateng!";
int result;
result=bcmp(s1,s2,6);
printf("Result=[%d]\n",result);
if (result==0)
printf("String \"%s\" and \"%s\" is equal in first 6 bytes! \n",s1,s2);
else
printf("String \"%s\" and \"%s\" is not equal in first 6 bytes! \n",s1,s2);
result=bcmp(s1,s2,13);
printf("Result=[%d]\n",result);
if (result==-1)
printf("String \"%s\" is less than \"%s\" in first 13 bytes! \n",s1,s2);
else
printf("String \"%s\" is greater than \"%s\" in first 13 bytes! \n",s1,s2);
result=bcmp(s2,s1,13);
printf("Result=[%d]\n",result);
if (result==1)
printf("String \"%s\" is greater than \"%s\" in first 13 bytes! \n",s1,s2);
else
printf("String \"%s\" is less than \"%s\" in first 13 bytes! \n",s1,s2);
return 0;
}
/home/cpicsrv/yansp/cstring > gcc -Wall bcmp.c -o bcmp
/home/cpicsrv/yansp/cstring > ./bcmp
Result=[0]
String "Hello Huateng!" and "Hello huateng!" is equal in first 6 bytes!
Result=[-1]
String "Hello Huateng!" is less than "Hello huateng!" in first 13 bytes!
Result=[1]
String "Hello Huateng!" is greater than "Hello huateng!" in first 13 bytes!
/home/cpicsrv/yansp/cstring >
|
|
原型:extern void bcopy(const void *src, void *dest, int n);
用法:#include
功能:将字符串src的前n个字节复制到dest中
说明:bcopy不检查字符串中的空字节NULL,函数没有返回值。
举例:
/home/cpicsrv/yansp/cstring > cat bcopy.c
//bcopy.c
#include
#include
int main()
{
char *s1="Hello huateng!";
char s2[50];
bcopy(s1,s2,5);
printf("s1=[%s]\n",s1);
printf("s2=[%s]\n",s2);
return 0;
}
/home/cpicsrv/yansp/cstring > gcc -Wall bcopy.c -o bcopy
/home/cpicsrv/yansp/cstring > ./bcopy
s1=[Hello huateng!]
s2=[Hello]
/home/cpicsrv/yansp/cstring >
|
|
原型:extern void bzero(void *s, int n);
用法:#include
功能:置字节字符串s的前n个字节为零。
说明:bzero无返回值。
举例:
/home/cpicsrv/yansp/cstring > cat bzero.c
//bzero.c
#include
#include
int main()
{
struct
{
int a;
float b;
char c[20];
} huateng;
huateng.a=10;
huateng.b=20.322;
strcpy(huateng.c,"HUATENG");
printf("a=[%d]\n",huateng.a);
printf("b=[%f]\n",huateng.b);
printf("c=[%s]\n",huateng.c);
bzero(&huateng,sizeof(huateng));
printf("a=[%d]\n",huateng.a);
printf("b=[%f]\n",huateng.b);
printf("c=[%s]\n",huateng.c);
return 0;
}
/home/cpicsrv/yansp/cstring > gcc -Wall bzero.c -o bzero
/home/cpicsrv/yansp/cstring > ./bzero
a=[10]
b=[20.322001]
c=[HUATENG]
a=[0]
b=[0.000000]
c=[]
/home/cpicsrv/yansp/cstring >
|
memccpy |
原型:extern void *memccpy(void *dest, void *src, unsigned char ch, unsigned int count);
用法:#include
功能:由src所指内存区域复制不多于count个字节到dest所指内存区域,如果遇到字符ch则停止复制。
说明:返回指向字符ch后的第一个字符的指针,如果src前n个字节中不存在ch则返回NULL,并且源字符串会被整个复制到目标字符串中。
举例:
/home/cpicsrv/yansp/cstring > cat memccpy.c
//memccpy
#include
#include
int main()
{
char *s="Hello -Huateng!";
char d[20],*p;
p=memccpy(d,s,'-',strlen(s));
printf("Address of p is :[%x]\n",p);
printf("Address of p is :[%x]\n",d);
if(p)
{
*p='\0';
printf("string d is :[%s]\n",d);
}
else
printf("Char not found!");
p=memccpy(d,s,'$',strlen(s));
if (!p)
printf("Char not found! string d is :[%s]\n",d);
return 0;
}
/home/cpicsrv/yansp/cstring > gcc -Wall memccpy.c -o memccpy
memccpy.c: In function `main':
memccpy.c:10: warning: unsigned int format, pointer arg (arg 2)
memccpy.c:11: warning: unsigned int format, pointer arg (arg 2)
/home/cpicsrv/yansp/cstring > ./memccpy
Address of p is :[bff7fbd7]
Address of p is :[bff7fbd0]
string d is :[Hello -]
Char not found! string d is :[Hello -Huateng]
/home/cpicsrv/yansp/cstring >