Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在将输入放入数组时要使用%c之前的空格?_C - Fatal编程技术网

为什么在将输入放入数组时要使用%c之前的空格?

为什么在将输入放入数组时要使用%c之前的空格?,c,C,我刚刚写了一个程序来反转一个单词。我首先将程序编译如下: //This program will reverse a given word #include <stdio.h> int main() { int letters, i, j; printf("Enter the number of letters in your word: "); scanf("%d", &letters); int word[letters]; printf("Enter %d Let

我刚刚写了一个程序来反转一个单词。我首先将程序编译如下:

//This program will reverse a given word

#include <stdio.h>
int main()
{
int letters, i, j;
printf("Enter the number of letters in your word: ");
scanf("%d", &letters);

int word[letters];

printf("Enter %d Letters: ", letters);
for( i = 0; i < letters; i++){
    scanf("%c", &word[i]);
}

for( j = i - 1; j >= 0; j-- ){
    printf("%c", word[j]);
}


return 0;
}
//此程序将反转给定的单词
#包括
int main()
{
int字母,i,j;
printf(“输入单词中的字母数:”);
scanf(“%d”和字母);
int字[字母];
printf(“输入%d个字母:”,字母);
对于(i=0;i<字母;i++){
scanf(“%c”和单词[i]);
}
对于(j=i-1;j>=0;j--){
printf(“%c”,单词[j]);
}
返回0;
}
然后,我输入5以字母形式存储,输入单词“rubel”(忽略倒逗号)以反转。预期产出为“勒布尔”。但不幸的是我得了埃布尔。然后我重新编译了代码,如下所示:

//This program will reverse a given word

#include <stdio.h>
int main()
{
int letters, i, j;
printf("Enter the number of letters in your word: ");
scanf("%d", &letters);

int word[letters];

printf("Enter %d Letters: ", letters);
for( i = 0; i < letters; i++){
    scanf(" %c", &word[i]);
}

for( j = i - 1; j >= 0; j-- ){
    printf("%c", word[j]);
}


return 0;
}
//此程序将反转给定的单词
#包括
int main()
{
int字母,i,j;
printf(“输入单词中的字母数:”);
scanf(“%d”和字母);
int字[字母];
printf(“输入%d个字母:”,字母);
对于(i=0;i<字母;i++){
scanf(“%c”和单词[i]);
}
对于(j=i-1;j>=0;j--){
printf(“%c”,单词[j]);
}
返回0;
}

这一次我得到了预期的输出,即“lebur”。现在,我的问题是,在我没有得到预期的输出之前有什么错误,我刚才做了什么,通过放置一个空格,这次我得到了预期的结果。提前感谢。

在第一种情况下,
单词[0]
变成了
\n
(您输入了
5\n
)。在第二种情况下,
\n
被跳过,因为您告诉scanf跳过空白字符(通过
)。

您阅读了
scanf
的文档吗?它拥有一切。因为%c不会跳过空格,例如换行符。这是一个重复的问题。我想,我没有。我可以得到任何链接或一些有助于我了解的信息吗?