为什么在使用VS Code时,我的C脚本在使用scanf()函数时无法注册用户输入?

huangapple go评论55阅读模式
英文:

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 &lt;stdio.h&gt;

int main()
{
    char input;
    printf(&quot;hello, pls give input\n  &gt;&gt;&gt; &quot;);
    scanf(&quot; %s&quot;, &amp;input);  //This is the problem
    printf(&quot;\n%s&quot;, input);
    return 0;
}

And the output looks something like this:

hello, pls give input
&gt; &gt; &gt; 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( &quot;%99s&quot;, 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 &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

int main( void )
{
    char input[100];

    //get input from user
    printf( &quot;Please enter input: &quot;);
    if ( fgets( input, sizeof input, stdin ) == NULL )
    {
        fprintf( stderr, &quot;Input error!\n&quot; );
        return EXIT_FAILURE;
    }

    //remove newline character, if it exists
    input[strcspn(input,&quot;\n&quot;)] = &#39;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
int main( void )
{
char input[100];
//get input from user
printf( &quot;Please enter input: &quot;);
if ( fgets( input, sizeof input, stdin ) == NULL )
{
fprintf( stderr, &quot;Input error!\n&quot; );
return EXIT_FAILURE;
}
//remove newline character, if it exists
input[strcspn(input,&quot;\n&quot;)] = &#39;\0&#39;;
//print input back to user
printf(&quot;You entered: %s\n&quot;, input);
return EXIT_SUCCESS;
}
&#39;; //print input back to user printf(&quot;You entered: %s\n&quot;, input); return EXIT_SUCCESS; }

huangapple
  • 本文由 发表于 2023年5月20日 23:39:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定