何为函数?C语言函数的概念
发布时间:2022-07-01 11:26:57 所属栏目:语言 来源:互联网
导读:从表面上看,函数在使用时必须带上括号,有必要的话还要传递参数,函数的执行结果也可以赋值给其它变量。例如,strcmp() 是一个用来比较字符串大小的函数,它的用法如下: #include stdio.h #include string.h int main(){ char str1[] = http://c.bianche
从表面上看,函数在使用时必须带上括号,有必要的话还要传递参数,函数的执行结果也可以赋值给其它变量。例如,strcmp() 是一个用来比较字符串大小的函数,它的用法如下: #include <stdio.h> #include <string.h> int main(){ char str1[] = "http://c.biancheng.net"; char str2[] = "http://www.baidu.com"; //比较两个字符串大小 int result = strcmp(str1, str2); printf("str1 - str2 = %dn", result); return 0; } str1 和 str2 是传递给 strcmp() 的参数,strcmp() 的处理结果赋值给了变量 result。 我们不妨设想一下,如果没有 strcmp() 函数,要想比较两个字符串的大小该怎么写呢?请看下面的代码: #include <stdio.h> #include <string.h> int main(){ char str1[] = "http://c.biancheng.net"; char str2[] = "http://www.baidu.com"; int result, i; //比较两个字符串大小 for(i=0; (result = str1[i] - str2[i]) == 0; i++){ if(str1[i] == ' |