c语言怎么输入两组数据后合并输出
时间: 2024-10-21 11:06:12 浏览: 37
在 C 语言中,如果你想要接收两组用户输入的数据并将其合并后输出,可以使用两个独立的输入函数(例如`scanf()`),分别获取每组数据,然后再将它们拼接成字符串或者结构体进行输出。这里以简单的整数和字符数组为例:
```c
#include <stdio.h>
int main() {
int num1, num2;
char str1[50], str2[50];
// 输入第一组数据
printf("请输入第一个整数:");
scanf("%d", &num1);
printf("请输入第一个字符串:");
scanf("%s", str1); // 注意这里不需要 \n,因为 scanf 已经会在输入完成后自动读取换行
// 输入第二组数据
printf("\n请输入第二个整数:");
scanf("%d", &num2);
printf("请输入第二个字符串:");
scanf("%s", str2); // 同上,scanf 会自动读取换行
// 合并数据
char combined[] = "First data: " + // 这里假设加法能表示字符串连接,实际上应使用 strcat 或 sprintf
(char*)&num1 + " ";
combined[strlen(combined)] = '\0';
combined[strlen(combined) + strlen(str1)] = ' ';
combined[strlen(combined) + strlen(str1)] = ' ';
combined[strlen(combined) + strlen(str1) + 1] = '\0'; // 添加最后一个结束符
combined[strlen(combined) + 2] = ' ';
combined[strlen(combined) + 2 + strlen(str2)] = '\0'; // 同理,合并第二个字符串
// 输出结果
printf("合并后的数据是:%s\n", combined);
return 0;
}
```
这个程序首先分别获取两个整数和两个字符串,然后通过字符串连接的方式把它们放在一起。注意,对于字符串输入,我们直接使用`scanf()`,因为它会自动处理换行。
阅读全文
相关推荐


















