C语言几个常用的函数

本文详细介绍了C语言中常用的字符串操作函数,包括memset用于填充内存、memcpy用于复制内存以及memcmp用于比较两个内存块的内容。通过具体的代码示例展示了如何使用这些函数,并解释了它们的参数意义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有几个函数我是经常用到,但是老师分不清楚具体参数的含义,下面做个记录:

memset

extern void *memset(void *buffer, int c, int count);
将buffer区域的前count个字节设置成字符c

char *s="Golden Global View";
memset(s,'G',6);

memcpy

extern void *memcpy(void *dest, void *src, unsigned int count);
将src区域的前count个字节赋值到dest区域,src和dest所指内存区域不能重叠,函数返回指向dest的指针

char *s="Golden Global View";
char d[20];
memcpy(d,s,strlen(s));

memcmp

extern int memcmp(void *buf1, void *buf2, unsigned int count);
比较buf1区域以及buf2区域的前count个字节
  当buf1<buf2时,返回值<0
  当buf1=buf2时,返回值=0
  当buf1>buf2时,返回值>0

 char *s1="Hello, Programmers!";
 char *s2="Hello, programmers!";
 int r; 
 r=memcmp(s1,s2,strlen(s1));
 if(!r)
   printf("s1 and s2 are identical");
 else
 if(r<0)
   printf("s1 less than s2");
 else
   printf("s1 greater than s2");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值