简体   繁体   中英

How to save a string with multiple words with scanf()

I just started programming in C and I was wondering why I can't store a string with multiple words with scanf().

For example, I enter: "That's an example" and it's stores only the first word "That's"

My code:

int main(void) {

    char string[100];
    
    printf("Please enter something: ");
    scanf("%s", &string);
    printf("You entered: %s", string);


    return (0);
}

You can let scanf() read more than one word with the character class conversion specifier: %[^\n] will stop at the newline and leave it pending in the input stream. Note that you must tell scanf the maximum number of characters to store into the destination array to avoid undefined behavior on long input lines. When passing an array to scanf() , you should not pass its address as &string , but just pass string as arrays decays into a pointer to their first element when passed as a function argument.

Here is a modified version:

#include <stdio.h>

int main(void) {
    char string[100];
    int c;
    
    for (;;) {
        printf("Please enter something: ");
        /* initialize `string` in case the `scanf()` conversion fails on an empty line */
        *string = '\0';
        if (scanf("%99[^\n]", string) == EOF)
            break;
        printf("You entered: %s\n", string);
        /* read the next byte (should be the newline) */
        c = getchar();
        if (c == EOF)   /* end of file */
            break;
        if (c != '\n')
            ungetc(c, stdin);  /* not a newline: push it back */
    }
    return 0;
}

Note however that it is much simpler to use fgets() for this task:

#include <stdio.h>

int main(void) {
    char string[100];
    
    for (;;) {
        printf("Please enter something: ");
        if (!fgets(string, sizeof string, stdin))
            break;
        /* strip the trailing newline, if any */
        string[strcspn(string, "\n")] = '\0';
        printf("You entered: %s\n", string);
    }
    return 0;
}
#include <stdio.h>

#define BUFF_SIZE 512

int main(void) {

    char string[BUFF_SIZE];

    printf("Enter something: ");
    fgets(string, BUFF_SIZE, stdin);
    printf("You entered: %s", string);

    return (0);
}

fgets() is the best option

I think there's a problem in you scanf(); I recommend you to remove & from it. then your code should see like that:

int main(void) {

    char string[100];
    
    printf("Please enter something: ");
    scanf("%s", string);
    printf("You entered: %s", string);


    return (0);
}

In the c language, there is no data type called a string. A string is stored as an array of characters .

Moreover, the variable itself points to the first element of the array. Therefore, there is no need to use the '&' operator to pass the address.

So, all you have to do is the following:

int main(void) {

char string[100];
printf("Please enter something: ");
scanf("%s", string);
printf("You entered: %s", string);
return (0);
}

Don't use '&' in scanf function.


int main()
{

   char string[100];
   printf("Please enter something: ");
   scanf("%[^\n]%*c",string);
   printf("You entered: %s", string);
   return 0;
}

According to https://man7.org/linux/man-pages/man3/scanf.3.html , %s will ignore white-space characters. To capture spaces you would have to use %c with the additional size of the input argument, or use %[ format. Check if scanf will add \0 byte to the end or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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