scanf("%d", &score[i])
时间: 2023-11-27 14:46:21 浏览: 38
这是一个C语言中的scanf函数,用于从标准输入中读取一个整数,并将其存储在score数组的第i个位置中。其中,&符号表示score[i]的地址,即输入数据所在的位置。需要注意的是,scanf函数中的格式控制后面应当是变量的地址,而不是变量名。如果写成scanf("%d", score[i]),则会出现写内存错误,无法运行的情况。
相关问题
若变量已正确定义,执行以下程序段,输入负数时,循环结束。 total = 0; scanf ("%d", &score); while(score >= 0){ total = total + score; scanf ("%d", &score);
这个程序段的作用是计算输入的所有非负整数的和,当输入负数时结束循环,并输出计算结果 `total`。
程序的执行流程如下:
1. 初始化变量 `total`,将其赋值为 0。
2. 读入一个整数 `score`。
3. 进入循环体,判断 `score` 是否大于等于 0,如果是则执行循环体语句,否则跳出循环。
4. 在循环体中,将 `score` 的值累加到 `total` 中。
5. 读入下一个整数 `score`。
6. 重复步骤 3-5,直到输入的整数为负数。
7. 循环结束后,输出计算结果 `total`。
可以用以下代码实现:
```c
int total = 0;
int score;
scanf("%d", &score);
while (score >= 0) {
total += score;
scanf("%d", &score);
}
printf("总和为:%d\n", total);
```
若变量已正确定义,执行以下程序段,输入0或者负数时,循环结束。 total = 0; scanf ("%d", &score); while(score > 0){ total = total + score; scanf ("%d", &score); }
这个程序段的作用是计算输入的所有正整数的和,当输入非正数时结束循环,并输出计算结果 `total`。
程序的执行流程如下:
1. 初始化变量 `total`,将其赋值为 0。
2. 读入一个整数 `score`。
3. 进入循环体,判断 `score` 是否大于 0,如果是则执行循环体语句,否则跳出循环。
4. 在循环体中,将 `score` 的值累加到 `total` 中。
5. 读入下一个整数 `score`。
6. 重复步骤 3-5,直到输入的整数为 0 或负数。
7. 循环结束后,输出计算结果 `total`。
可以用以下代码实现:
```c
int total = 0;
int score;
scanf("%d", &score);
while (score > 0) {
total += score;
scanf("%d", &score);
}
printf("总和为:%d\n", total);
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)