gpt4 book ai didi

c - 第二个字符串没有被扫描

转载 作者:行者123 更新时间:2023-11-30 14:53:01 29 4
gpt4 key购买 nike

#include<stdio.h>

void main()
{

char str1[100];
char str2[100];

printf("\n Enter the first String\n");
scanf("%[^\n]s",str1);

printf("Enter the second String");
scanf("%[^\n]s",str2);

printf("\n The strings are %s %d \n",str1,i);
}

最佳答案

由于您想读取整行,请使用fgets。至少您可以控制输入的字符串长度,并且无需处理 scanf 特性

printf("\n Enter the first String\n");    
fgets(str1, 100, stdin);

printf("Enter the second String");
fgets(str2, 100, stdin);

printf("\n The strings are %s %s \n",str1,str2);

请注意,尾随的 \n 仍在字符串中(如果它们的最大长度为 98 个字符)。

Worth reading - scanf vs fgets

关于c - 第二个字符串没有被扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47448933/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com