How do you allow spaces to be entered using scanf? How do you allow spaces to be entered using scanf? c c

How do you allow spaces to be entered using scanf?


People (and especially beginners) should never use scanf("%s") or gets() or any other functions that do not have buffer overflow protection, unless you know for certain that the input will always be of a specific format (and perhaps not even then).

Remember than scanf stands for "scan formatted" and there's precious little less formatted than user-entered data. It's ideal if you have total control of the input data format but generally unsuitable for user input.

Use fgets() (which has buffer overflow protection) to get your input into a string and sscanf() to evaluate it. Since you just want what the user entered without parsing, you don't really need sscanf() in this case anyway:

#include <stdio.h>#include <stdlib.h>#include <string.h>/* Maximum name size + 1. */#define MAX_NAME_SZ 256int main(int argC, char *argV[]) {    /* Allocate memory and check if okay. */    char *name = malloc(MAX_NAME_SZ);    if (name == NULL) {        printf("No memory\n");        return 1;    }    /* Ask user for name. */    printf("What is your name? ");    /* Get the name, with size limit. */    fgets(name, MAX_NAME_SZ, stdin);    /* Remove trailing newline, if there. */    if ((strlen(name) > 0) && (name[strlen (name) - 1] == '\n'))        name[strlen (name) - 1] = '\0';    /* Say hello. */    printf("Hello %s. Nice to meet you.\n", name);    /* Free memory and exit. */    free (name);    return 0;}


Try

char str[11];scanf("%10[0-9a-zA-Z ]", str);

Hope that helps.


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];    // get up to buffer size - 1 characters (to account for NULL terminator)    scanf("%19[^\n]", name);    printf("%s\n", name);    return 0;}