下面程序输出为() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <stdio.h> int fun(int a) { int b = 0; static int c = 3; b++; c++; return (a+b+c); } int main( ) { int i=0; for(; i < 3; i++) printf("%d",fun(2)); return 0; }为什么结果为789
时间: 2024-04-06 17:33:20 浏览: 24
程序的输出结果为 789。
程序中的 fun 函数中有一个静态变量 c,它的初始值为 3,每当 fun 函数被调用时,c 的值都会加 1。在 main 函数中,通过循环调用了三次 fun 函数,每次传入的参数均为 2。 在第一次调用 fun 函数时,a=2,b=1,c=4,返回值为 7; 在第二次调用 fun 函数时,a=2,b=1,c=5,返回值为 8; 在第三次调用 fun 函数时,a=2,b=1,c=6,返回值为 9。
因此,循环结束后程序输出的结果就是 789。
相关问题
#include <stdio.h> int fun(int x)
#include <stdio.h> 是C语言中的一个预处理指令,它告诉编译器在编译过程中要包含stdio.h头文件。stdio.h是标准输入输出库的头文件,其中包含了一些常用的输入输出函数的声明,比如printf和scanf。
int fun(int x) 是一个函数的声明,它定义了一个名为fun的函数,该函数接受一个整数参数x,并返回一个整数值。函数声明告诉编译器函数的名称、参数类型和返回类型,但不包含函数的具体实现。
*定义函数实现n个字符串按字典顺序排序 开头: #include<stdio.h> #include<string.h> void fun (char *str[],int n)
好的,以下是一个实现n个字符串按字典顺序排序的示例代码:
```
#include <stdio.h>
#include <string.h>
void fun(char *str[], int n) {
int i, j;
char *temp;
// 使用冒泡排序算法
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (strcmp(str[j], str[j+1]) > 0) {
// 交换两个字符串的位置
temp = str[j];
str[j] = str[j+1];
str[j+1] = temp;
}
}
}
}
int main() {
char *str[] = {"apple", "banana", "orange", "grape", "kiwi"};
int n = sizeof(str) / sizeof(str[0]);
fun(str, n);
printf("排序后的字符串数组:\n");
for (int i = 0; i < n; i++) {
printf("%s ", str[i]);
}
printf("\n");
return 0;
}
```
在这个示例代码中,我们使用了冒泡排序算法对字符串数组进行排序。在排序过程中,我们使用了 `strcmp()` 函数来比较两个字符串的大小关系,并使用指针变量 `temp` 来交换两个字符串在数组中的位置。最后,我们输出排好序的字符串数组。
请注意,这个示例代码中的字符串数组是在程序中直接定义的。如果您需要从用户输入中获取字符串数据,请使用 `scanf()` 或其他输入函数来读取数据,并将它们存储在字符串数组中。
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)