#include<stdio.h>
#include<stdlib.h>
int main()
{
char *userInput;
userInput = (char*)malloc(sizeof(userInput));
printf("Commands:\n");
printf("\ta name - adds given name to list\n");
printf("\tr name - removes given name from list\n");
printf("\tp - prints out list\n");
printf("\te - exits\n");
printf("\n\nEnter a command: ");
scanf("%s\n",userInput);
printf("\nThe user input was: %s\n", userInput);
return 0;
}
予编译代码“GCC -std = gnu99 -m32 -Wall -g -o名称列表namelist.c” 每当我运行所有的第一个printf被显示在可执行文件和我得到提示上的输入。每当我输入的输入,然后按回车,我没有得到提示未来的printf直到我把另一个观点。。为什么我的程序在使用scanf后暂停?
使用'gdb'调试运行一步程序一步 -
也读字符串
示例代码时,我会建议在scanf函数与fgets()。 –
欢迎来到Stack Overflow! [请参阅此讨论,为什么不在'C'中投射'malloc()'和family的返回值。](http://stackoverflow.com/q/605845/2173917)。 –
提示:'\ n'用于回车。您是否在https://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm中看到任何'\ n'?的[scanf函数] –