How to read string from keyboard using C? How to read string from keyboard using C? c c

How to read string from keyboard using C?


You have no storage allocated for word - it's just a dangling pointer.

Change:

char * word;

to:

char word[256];

Note that 256 is an arbitrary choice here - the size of this buffer needs to be greater than the largest possible string that you might encounter.

Note also that fgets is a better (safer) option then scanf for reading arbitrary length strings, in that it takes a size argument, which in turn helps to prevent buffer overflows:

 fgets(word, sizeof(word), stdin);


I cannot see why there is a recommendation to use scanf() here. scanf() is safe only if you add restriction parameters to the format string - such as %64s or so.

A much better way is to use char * fgets ( char * str, int num, FILE * stream );.

int main(){    char data[64];    if (fgets(data, sizeof data, stdin)) {        // input has worked, do something with data    }}

(untested)


When reading input from any file (stdin included) where you do not know the length, it is often better to use getline rather than scanf or fgets because getline will handle memory allocation for your string automatically so long as you provide a null pointer to receive the string entered. This example will illustrate:

#include <stdio.h>#include <stdlib.h>int main (int argc, char *argv[]) {    char *line = NULL;  /* forces getline to allocate with malloc */    size_t len = 0;     /* ignored when line = NULL */    ssize_t read;    printf ("\nEnter string below [ctrl + d] to quit\n");    while ((read = getline(&line, &len, stdin)) != -1) {        if (read > 0)            printf ("\n  read %zd chars from stdin, allocated %zd bytes for line : %s\n", read, len, line);        printf ("Enter string below [ctrl + d] to quit\n");    }    free (line);  /* free memory allocated by getline */    return 0;}

The relevant parts being:

char *line = NULL;  /* forces getline to allocate with malloc */size_t len = 0;     /* ignored when line = NULL *//* snip */read = getline (&line, &len, stdin);

Setting line to NULL causes getline to allocate memory automatically. Example output:

$ ./getline_exampleEnter string below [ctrl + d] to quitA short string to test getline!  read 32 chars from stdin, allocated 120 bytes for line : A short string to test getline!Enter string below [ctrl + d] to quitA little bit longer string to show that getline will allocated again without resetting line = NULL  read 99 chars from stdin, allocated 120 bytes for line : A little bit longer string to show that getline will allocated again without resetting line = NULLEnter string below [ctrl + d] to quit

So with getline you do not need to guess how long your user's string will be.