linux下strcmp函数,linux系统库函数之strcmp、strncmp

本文详细介绍了C语言中用于字符串比较的strcmp和strncmp函数的实现原理和使用方法。strcmp函数用于比较两个字符串是否相等,若相等则返回0,否则返回非零值。strncmp函数则在指定长度内比较两个字符串,同样在相等或遇到结束符时返回0。这两个函数在源码级别的实现中通过逐字符比较并判断是否相等来完成比较操作。

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

240 #ifndef __HAVE_ARCH_STRCMP

241 /**

242  * strcmp - Compare two strings

243  * @cs: One string

244  * @ct: Another string

245  */

246 #undef strcmp

247 int strcmp(const char *cs, const char *ct)

248 {

249         unsigned char c1, c2;

250

251         while (1) {

252                 c1 = *cs++;

253                 c2 = *ct++;

254                 if (c1 != c2)

255                         return c1 < c2 ? -1 : 1;

256                 if (!c1)

257                         break;

258         }

259         return 0;

260 }

261 EXPORT_SYMBOL(strcmp);

262 #endif

字符串比较函数,如果两个字符串相等,则返回0,否则返回非零值。

264 #ifndef __HAVE_ARCH_STRNCMP

265 /**

266  * strncmp - Compare two length-limited strings

267  * @cs: One string

268  * @ct: Another string

269  * @count: The maximum number of bytes to compare

270  */

271 int strncmp(const char *cs, const char *ct, size_t count)

272 {

273         unsigned char c1, c2;

274

275         while (count) {

276                 c1 = *cs++;

277                 c2 = *ct++;

278                 if (c1 != c2)

279                         return c1 < c2 ? -1 : 1;

280                 if (!c1)

281                         break;

282                 count--;

283         }

284         return 0;

285 }

286 EXPORT_SYMBOL(strncmp);

287 #endif

同样的是,如果没有遇到字符串结束符,只比较cuont大小数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值