In C, using scanf interrupts input when encountering spaces.
we should use the fgets function to read the entire row.
Use the fputs function to display the string.
/* Example: scanf in C language */ #include <stdio.h> #include <stdlib.h> #include<conio.h> int main() { char name[30]; printf("Enter name: "); scanf("%s",name); //fgets(name, sizeof(name), stdin); printf("Name: "); puts(name); return 0; }
output:
Enter name: hello lautturi Name: hello
Use fgets function to get the entire line of string
/* Example: fgets in C language */ #include <stdio.h> #include <stdlib.h> #include<conio.h> int main() { char name[30]; printf("Enter name: "); fgets(name, sizeof(name), stdin); printf("Name: "); puts(name); return 0; }
输出:
Enter name: hello lautturi Name: hello lautturi