初识strncmp函数

来源:互联网 发布:mysql 5.6 中文手册 编辑:程序博客网 时间:2024/05/16 02:20
函数名: strncmp
  功 能: 串比较
  用 法: int strncmp(char *str1, char *str2, int maxlen);
  说明:此函数功能即比较字符串str1和str2的前maxlen个字符。如果前maxlen字节完全相等,返回值就=0;在前maxlen字节比较过程中,如果出现str1[n]与str2[n]不等,则返回(str1[n]-str2[n])。

程序例:

    #include <string.h>
  #include <stdio.h>
  int main(void)
  {
  char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc";
  int ptr;
  ptr = strncmp(buf2,buf1,3);
  if (ptr > 0)
  printf("buffer 2 is greater than buffer 1\n");
  else if(ptr<0)
  printf("buffer 2 is less than buffer 1\n");
  ptr = strncmp(buf2,buf3,3);
  if (ptr > 0)
  printf("buffer 2 is greater than buffer 3\n");
  else if(ptr<0)
  printf("buffer 2 is less than buffer 3\n");
  return(0);
  }
原创粉丝点击