以下程序的正确运行结果是 main() printf("%d," func(2,2)) printf("%d",func(2.2)) int func(int a, int b){ static int m=0,i=2; i+= m+1; m=i+a+b;return(m);}
时间: 2024-01-29 08:04:56 浏览: 28
这段程序的正确运行结果为:4,8。
首先,在 main 函数中调用 func 函数两次,并打印返回值,即:
```
printf("%d,", func(2, 2)); // 输出 4
printf("%d", func(2.2)); // 输出 8
```
而 func 函数中定义了两个静态变量 m 和 i,m 的初始值为 0,i 的初始值为 2。在第一次调用 func 函数时,i 加上 m+1 的值,即 2+0+1=3,然后将 i+a+b 的值(2+2+2=6)赋给 m,最后返回 m 的值(即 6)。在第二次调用 func 函数时,i 加上 m+1 的值,即 3+6+1=10,然后将 i+a+b 的值(10+2+2=14)赋给 m,最后返回 m 的值(即 14)。因此,程序的输出为 4,8。
相关问题
int main(){struct STU a={"YangSan",2041),b={"WangYin",2043};func1(a);func2(&b);printf ("%d %d n",a.num,b.num)}}
这段代码看起来有些问题,首先是结构体定义中的逗号应该是分号,应该改为:
```
int main() {
struct STU a = {"YangSan", 2041};
struct STU b = {"WangYin", 2043};
func1(a);
func2(&b);
printf("%d %d\n", a.num, b.num);
return 0;
}
```
然后是函数名,应该是`func1`和`func2`,而不是`1`和`2`,同时函数的定义也需要调整,应该是:
```
void func1(struct STU c) {
struct STU b = {"LiSiGoo", 2042};
c = b;
}
void func2(struct STU *c) {
struct STU b = {"SunDan", 2044};
*c = b;
}
```
最终的代码应该是这样的:
```
#include <stdio.h>
struct STU {
char name[10];
int num;
};
void func1(struct STU c) {
struct STU b = {"LiSiGoo", 2042};
c = b;
}
void func2(struct STU *c) {
struct STU b = {"SunDan", 2044};
*c = b;
}
int main() {
struct STU a = {"YangSan", 2041};
struct STU b = {"WangYin", 2043};
func1(a);
func2(&b);
printf("%d %d\n", a.num, b.num);
return 0;
}
```
输出结果应该是:
```
2043 2044
```
希望能为您提供帮助。
以下程序的运行结果是: struct stru {int x; char c; }; main() { struct stru a={10, ' x' }, *p=&a; func (p); printf ( “%d,%c” , a.x, a.c);} func (struct stru *b){b->x=20; b->c='y';}
运行结果是:
```
20,y
```
程序中定义了一个结构体`stru`,包含一个`int`类型的成员`x`和一个`char`类型的成员`c`。在`main`函数中,定义了一个`stru`类型的结构体变量`a`,初始化为`{10, ' x' }`,同时定义了一个指向结构体的指针`p`,并将其指向`a`。然后调用函数`func`,将指针`p`作为参数传递给函数。在`func`函数中,将指针所指向的结构体中的`x`成员赋值为`20`,`c`成员赋值为`'y'`。
最后在`main`函数中,输出`a.x`和`a.c`的值,由于在`func`函数中修改了`a`的成员,因此输出的结果为`20,y`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)