How do you allow spaces to be entered using scanf?

前端 未结 11 1993
我寻月下人不归
我寻月下人不归 2020-11-21 06:05

Using the following code:

char *name = malloc(sizeof(char) + 256); 

printf(\"What is your name? \");
scanf(\"%s\", name);

printf(\"Hello %s. Nice to meet y         


        
11条回答
  •  醉话见心
    2020-11-21 06:18

    If someone is still looking, here's what worked for me - to read an arbitrary length of string including spaces.

    Thanks to many posters on the web for sharing this simple & elegant solution. If it works the credit goes to them but any errors are mine.

    char *name;
    scanf ("%m[^\n]s",&name);
    printf ("%s\n",name);
    

提交回复
热议问题