我用 C 语言编写了一个简单的程序,如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int length;
printf("Enter the length of the string:\t");
scanf("%d",&length);
char str1[10];
printf("Enter string:\t");
gets(str1);
printf("%s",str1);
return 0;
}
当我执行它时 - 我得到的输出为:
Enter the length of the string: 5
Enter string:
Process returned 0 (0x0) execution time : 1.740 s
Press any key to continue.
我不知道为什么它不要求输入字符串并直接退出程序。
最佳答案
当您输入“5”并按回车键时,您将向程序发送两个字符 -“5”和换行符。因此,您的第一个 scanf
获取“5”,第二个获取换行符,并将其转换为数字零。
关于c - 程序未完全执行并跳过语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41207076/