函数参数有字符串参数
需要在函数开始时判断这些字符串是否为合法输入
函数输入参数为 char *
使用字符串比较函数 strcmp
发现是可以的。包括打印传入的字符串,都可以用char * 作为字符串变量的定义和传入
参考文章:
C语言——字符串、打印字符串的三种方式_c语言打印字符串_青春无限坑的博客-CSDN博客
https://blog.csdn.net/2301_79180143/article/details/132797156
strcmp函数介绍
strcmp函数是一个C语言库函数,使用时应包含头文件#include<stdio,h>
函数原型:int strncmp ( const char * str1, const char * str2, size_t num );
用于⽐较str1和str2中的元素,如果相等就继续往后⽐较,如果提前发现不一样,就提前结束,第⼀个字符串⼤于第⼆个字符串,则返回⼤于0的数字,第⼀个字符串⼩于第⼆个字符串,则返回⼩于0的数字,num个字符都相等,就是相等返回0.
————————————————
版权声明:本文为CSDN博主「故城山茶」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/2301_79180143/article/details/132797156
测试程序:
#include"stdio.h"
#include"math.h"
void test(char *s)
{
printf("%s \n", s);
if (strcmp(s,"zeros")==0)
{
printf("string is same!");
}
else
{
printf("string is different!");
}
}
int main()
{
char *a = "zeros";
test(a);
return 0;
}
或:
//#include<stdio.h>
//#include<string.h>
//int main()
//{
// /*char dest[20] = "abe";*/
// char *dest = "abe";
// char src[20] = "abc";
//
// int ret = strcmp(dest, "abe");
// printf("%d\n", ret);
// return 0;
//}