int strncmp ( const char * str1, const char * str2, size_t num );

本文介绍了一个C语言程序示例,该程序使用strncmp函数来比较两个C风格字符串的前几个字符。通过具体代码演示了如何进行字符串长度的获取及比较,并展示了不同情况下字符串比较的结果。

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

Compares up to num characters of the C string str1 to those of the C string str2.

This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ, until a terminating null-character is reached, or until num characters match in both strings, whichever happens first.

*如果到达 terminating null-character也会停止对比的。



程序:

      1 #include <stdio.h>
      2
      3 int main(void){
      4     int c;
      5     char *a;
      6     a = "aaaaaac";
      7     char b[20] = "aaaaaabc";
      8     printf("%s\n",a);
      9     printf("%s\n",b);
     10
     11     printf("%d\n",sizeof(b));
     12     printf("*a:%d\n",sizeof(*a));
     13     printf("a:%d\n",sizeof(a));
     14     c =strncmp(a,b,sizeof(b));
     15     printf("%d\n",c);
     16
     17     return 0;
     18 }



结果:

aaaaaac
aaaaaabc
20
*a:1
a:4
1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值