简体   繁体   English

输入未从Eclipse CDT中获取

[英]Input not taken from eclipse CDT

I'm trying simple "printing your name C program in Eclipse CDT", but the console is not reading the user input. 我正在尝试简单的“在Eclipse CDT中打印您的名称C程序”,但是控制台未读取用户输入。

    #include <stdio.h>
    int main()
    {
      char* name = NULL;
      printf("enter you name ");

      gets(name);

      printf("Hi");
      fflush(stdout);
      puts(name);

      return 0;
    }

This is the output I get: 这是我得到的输出:

    enter you name Hi

Any idea why this is not reading the input from the user? 知道为什么它不读取用户的输入吗?

gets doesn't allocate memory for you, so your example causes undefined behavior. gets不会为您分配内存,因此您的示例导致未定义的行为。 Also, its use is highly discouraged because of the ease of causing buffer overflows. 另外,强烈建议不要使用它,因为这样容易导致缓冲区溢出。 Use 采用

char name[SIZE];
fgets(name, SIZE, stdin);

instead. 代替。 From man gets : man gets

Never use gets() . 永远不要使用gets()

Shouldn't get any clearer. 应该不会变得更清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM