How do you allow spaces to be entered using scanf?

前端 未结 11 1717
我寻月下人不归
我寻月下人不归 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:25

    /*reading string which contains spaces*/
    #include
    int main()
    {
       char *c,*p;
       scanf("%[^\n]s",c);
       p=c;                /*since after reading then pointer points to another 
                           location iam using a second pointer to store the base 
                           address*/ 
       printf("%s",p);
       return 0;
     }
    

提交回复
热议问题