strncmp()和strcmp()之间的C/C++区别

strncmp()和strcmp()之间的c/c++区别

strncmp() 和 strcmp 使用 ASCII 字符比较来比较两个字符串。 strncmp 采用一个附加参数作为要与字符串进行比较的字符数。它非常有用,因为如果字符串无效,那么 strcmp 将无法完成其操作。 strcmp 在字符串末尾搜索结束字符(’/0’)以完成其操作。 strncmp 使用 no。字符来结束其操作,因此是安全的。

示例

#include int main() {   char str1[] = "TutorialsPoint";   char str2[] = "Tutorials";   // Compare strings with strncmp()   int result1 = strncmp(str1, str2, 9);   if(result1 == 0){      printf("str1 == str2 upto 9 characters!");   }   // Compare strings using strcmp()   int result2 = strcmp(str1, str2);   if(result2 == 0){      printf("str1 == str2!");   } else {      if(result2 > 0){         printf("str1 > str2!");      } else {         printf("str1 

输出

str1 == str2 upto 9 characters!str1 > str2!

登录后复制

以上就是strncmp()和strcmp()之间的C/C++区别的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2584168.html

(0)
上一篇 2025年3月6日 14:45:08
下一篇 2025年2月23日 14:56:19

AD推荐 黄金广告位招租... 更多推荐

发表回复

登录后才能评论