Search code examples
cfgets

Get the whole input of a string in C


#include
char option[64],line[256];

main()

{

printf(">>")
(fgets(line, sizeof(line), stdin)) {
            if (1 == sscanf(line, "%s", option)) {
            }
    }
print(option)
}

will only get the first word, for example

/>>hello world

would output

/>>hello


Solution

  • #include <stdio.h>
    
    int main(){
        char option[64],line[256];
    
        printf(">>");
        if(fgets(line, sizeof(line), stdin)) {
            if (1 == sscanf(line, "%[^\n]%*c", option)) {//[^\n] isn't newline chars
                 printf("%s\n", option);
            }
        }
        return 0;
    }