英文:
Why does my C script not register user input when using scanf() function in VS Code?
问题
The issue in your code is with the scanf
format specifier and variable type. You are using %s
in scanf
, which is meant for strings, but you've declared input
as a char
, which is for single characters.
To fix the issue, you can change the data type of input
to an array of characters (a string) and use %s
in scanf
. Here's the corrected code:
#include <stdio.h>
int main()
{
char input[100]; // Declare an array to store the input
printf("hello, please give input\n >>> ");
scanf("%s", input); // Use %s to read a string
printf("\n%s", input);
return 0;
}
This should allow you to input and display strings properly. The problem is not with VS Code; it's with the code itself.
英文:
when I put the scanf() function in C, it doesnt scan the thing that you type in. Here's the script:
#include <stdio.h>
int main()
{
char input;
printf("hello, pls give input\n >>> ");
scanf(" %s", &input); //This is the problem
printf("\n%s", input);
return 0;
}
And the output looks something like this:
hello, pls give input
> > > hi
*nothing*
I'm also using VS Code, is the problem in VS Code or what?
I was even trying the whitespace, but that didn't work out either.
答案1
得分: 3
当使用%s
格式说明符与scanf
一起使用时,您需要传递一个指向已分配内存的指针,该内存具有足够的空间来存储字符串,包括空字符。
您违反了这个规则,因为您正在传递一个指向仅具有单个字符空间的已分配内存的指针。这意味着此已分配内存仅能存储一个空字符串(仅由终止空字符组成)。任何尝试将超过空字符串的内容写入"input"将导致缓冲区溢出,从而引发未定义行为(这意味着任何事情都可能发生,包括程序崩溃)。
因此,我建议您将"input"更改为数组,而不是单个字符,例如:
char input[100];
我还建议您告诉scanf
不要匹配超过99个字符的%s
格式说明符,以便在用户输入超过99个字符的情况下,不会写入超过100个字符(包括终止空字符)以防止缓冲区溢出:
scanf("%99s", input);
或者,您可以使用fgets
代替scanf
。这将读取整行输入,而不仅仅是一个单词。以下是一个示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char input[100];
// 从用户获取输入
printf("请输入内容:");
if (fgets(input, sizeof input, stdin) == NULL)
{
fprintf(stderr, "输入错误!\n");
return EXIT_FAILURE;
}
// 如果存在换行符,则删除换行符
input[strcspn(input, "\n")] = '#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char input[100];
// 从用户获取输入
printf("请输入内容:");
if (fgets(input, sizeof input, stdin) == NULL)
{
fprintf(stderr, "输入错误!\n");
return EXIT_FAILURE;
}
// 如果存在换行符,则删除换行符
input[strcspn(input, "\n")] = '\0';
// 将输入打印回给用户
printf("您输入的内容是:%s\n", input);
return EXIT_SUCCESS;
}
';
// 将输入打印回给用户
printf("您输入的内容是:%s\n", input);
return EXIT_SUCCESS;
}
请注意,我仅提供了代码部分的翻译。
英文:
When using the %s
format specifer with scanf
, you are required to pass a pointer to allocated memory that has sufficient space to store the string, including the null terminating character.
You are violating this rule, because you are passing a pointer to allocated memory that only has space for a single character. This means that this allocated memory can only store an empty string (consisting only of the terminating null character). Any attempt to write more than an empty string to input
will cause a buffer overflow, thereby invoking undefined behavior (which means that anything can happen, including your program crashing).
For this reason, I suggest that you make input
an array instead of a single character, for example like this:
char input[100];
I also recommend that you tell scanf
to not match more than 99 characters for the %s
format specifier, so that not more than 100 characters (including the terminating null character) is written to input
, in order to prevent a buffer overflow in case the user does enter more than 99 characters:
scanf( "%99s", input );
Alternatively, you can use fgets
instead of scanf
. This will read an entire line of input, instead of only a single word. Here is an example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( void )
{
char input[100];
//get input from user
printf( "Please enter input: ");
if ( fgets( input, sizeof input, stdin ) == NULL )
{
fprintf( stderr, "Input error!\n" );
return EXIT_FAILURE;
}
//remove newline character, if it exists
input[strcspn(input,"\n")] = '#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( void )
{
char input[100];
//get input from user
printf( "Please enter input: ");
if ( fgets( input, sizeof input, stdin ) == NULL )
{
fprintf( stderr, "Input error!\n" );
return EXIT_FAILURE;
}
//remove newline character, if it exists
input[strcspn(input,"\n")] = '\0';
//print input back to user
printf("You entered: %s\n", input);
return EXIT_SUCCESS;
}
';
//print input back to user
printf("You entered: %s\n", input);
return EXIT_SUCCESS;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论