In C language, scanf and fgets functions are usually used to obtain the data input from the keyboard.
If scanf is used, the following modes can be used:
"%[^\n]%*c"Soecru:www.lautturi.com
example:
#include <stdio.h> #include <stdlib.h> int main(int argc, char const *argv[]) { char name[20]; printf("Enter a name : "); scanf("%[^\n]%*c", &name); printf("the name is: %s\n", name); return 0; }
example of output:
Enter a name : lautturift idea the name is: lautturift idea
We can also use fgets to obtain data from files or pipes.
So we just need to set stdin input from the standard input. The standard input is generally keyboard, mouse, etc.
#include <stdio.h> #include <stdlib.h> int main(int argc, char const *argv[]) { char name[20]; printf("Enter a name : "); fgets(name, 20, stdin); printf("the name is: %s\n", name); return 0; }
output:
Enter a name : hello wolrd the name is: hello wolrd