C语言里,下列选项正确的赋值语句是( )。 A. t += 1; B. n1 = (n2 = (n3 = 0)); C. k = i = j; D. a = b + c = 1;
时间: 2023-08-12 11:06:36 浏览: 1170
选项 A、B、C 都是正确的赋值语句,选项 D 不是正确的赋值语句。
选项 A 是将变量 t 的值加 1,并将结果赋值给 t。
选项 B 是将 n3 赋值为 0,然后将 n3 的值赋给 n2,最后将 n2 的值赋给 n1。
选项 C 是将 j 的值赋给 i,然后将 i 的值赋给 k。
选项 D 中的 a = b + c = 1 不是一个正确的赋值语句,因为赋值运算符的优先级低于加法运算符,所以会先执行 b + c,然后将结果赋给 a,最后再将 1 赋给 a,这显然不是我们想要的结果。
相关问题
比较大小:输入3 个整数n1、n2 和n3,将这3 个数按从小到大的顺序输出。试编写相应程序。用C语言编写
以下是用C语言编写的程序:
```c
#include <stdio.h>
int main() {
int n1, n2, n3;
printf("请输入三个整数n1、n2、n3:");
scanf("%d %d %d", &n1, &n2, &n3);
if (n1 <= n2 && n1 <= n3) {
printf("%d ", n1);
if (n2 <= n3) {
printf("%d %d\n", n2, n3);
} else {
printf("%d %d\n", n3, n2);
}
} else if (n2 <= n1 && n2 <= n3) {
printf("%d ", n2);
if (n1 <= n3) {
printf("%d %d\n", n1, n3);
} else {
printf("%d %d\n", n3, n1);
}
} else {
printf("%d ", n3);
if (n1 <= n2) {
printf("%d %d\n", n1, n2);
} else {
printf("%d %d\n", n2, n1);
}
}
return 0;
}
```
程序中使用了条件语句if和else if来判断三个数的大小关系,并按照从小到大的顺序输出。
用c语言函数实现将输入的一组数据逆序输出的功能。(参考函数原型:void isort ( int a [ ],int n ) )输入输出格式要求int n ) ;编写函数void isort ( int a [ ] , n表示数组元素个数输出格式为:n1 , n2 , n3 , . , rn例如a = [ 1 , 2 , 3 ] n = 3输出3 , 2 , 1
好的,以下是用C语言函数实现将输入的一组数据逆序输出的代码:
```c
#include <stdio.h>
void isort(int a[], int n) {
int i, j, temp;
for(i = 0, j = n - 1; i < j; i++, j--) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
for(i = 0; i < n; i++) {
printf("%d", a[i]);
if(i != n-1) {
printf(", ");
}
}
}
int main() {
int n, i;
printf("请输入数组元素个数:");
scanf("%d", &n);
int a[n];
printf("请输入数组元素:");
for(i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("逆序输出的结果为:");
isort(a, n);
return 0;
}
```
运行结果如下:
```
请输入数组元素个数:3
请输入数组元素:1 2 3
逆序输出的结果为:3, 2, 1
```
注:本代码中使用了冒泡排序的思想来逆序输出数组元素。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)