How do you allow spaces to be entered using scanf?

前端 未结 11 1675
我寻月下人不归
我寻月下人不归 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<stdio.h>
    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;
     }
    
    0 讨论(0)
  • 2020-11-21 06:28

    While you really shouldn't use scanf() for this sort of thing, because there are much better calls such as gets() or getline(), it can be done:

    #include <stdio.h>
    
    char* scan_line(char* buffer, int buffer_size);
    
    char* scan_line(char* buffer, int buffer_size) {
       char* p = buffer;
       int count = 0;
       do {
           char c;
           scanf("%c", &c); // scan a single character
           // break on end of line, string terminating NUL, or end of file
           if (c == '\r' || c == '\n' || c == 0 || c == EOF) {
               *p = 0;
               break;
           }
           *p++ = c; // add the valid character into the buffer
       } while (count < buffer_size - 1);  // don't overrun the buffer
       // ensure the string is null terminated
       buffer[buffer_size - 1] = 0;
       return buffer;
    }
    
    #define MAX_SCAN_LENGTH 1024
    
    int main()
    {
       char s[MAX_SCAN_LENGTH];
       printf("Enter a string: ");
       scan_line(s, MAX_SCAN_LENGTH);
       printf("got: \"%s\"\n\n", s);
       return 0;
    }
    
    0 讨论(0)
  • 2020-11-21 06:29

    getline()

    Now part of POSIX, none-the-less.

    It also takes care of the buffer allocation problem that you asked about earlier, though you have to take care of freeing the memory.

    0 讨论(0)
  • 2020-11-21 06:33

    This example uses an inverted scanset, so scanf keeps taking in values until it encounters a '\n'-- newline, so spaces get saved as well

    #include <stdio.h>
    
    int main (int argc, char const *argv[])
    {
        char name[20];
        scanf("%[^\n]s",name);
        printf("%s\n", name);
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-21 06:36

    Don't use scanf() to read strings without specifying a field width. You should also check the return values for errors:

    #include <stdio.h>
    
    #define NAME_MAX    80
    #define NAME_MAX_S "80"
    
    int main(void)
    {
        static char name[NAME_MAX + 1]; // + 1 because of null
        if(scanf("%" NAME_MAX_S "[^\n]", name) != 1)
        {
            fputs("io error or premature end of line\n", stderr);
            return 1;
        }
    
        printf("Hello %s. Nice to meet you.\n", name);
    }
    

    Alternatively, use fgets():

    #include <stdio.h>
    
    #define NAME_MAX 80
    
    int main(void)
    {
        static char name[NAME_MAX + 2]; // + 2 because of newline and null
        if(!fgets(name, sizeof(name), stdin))
        {
            fputs("io error\n", stderr);
            return 1;
        }
    
        // don't print newline
        printf("Hello %.*s. Nice to meet you.\n", strlen(name) - 1, name);
    }
    
    0 讨论(0)
提交回复
热议问题