Gets() and puts()
The gets() function reads a line from stdin(standard input) into the buffer pointed to by str pointer, until either a terminating newline or EOF (end of file) occurs.
The puts() function writes the string str and a trailing newline to stdout.
#include<stdio.h>
void main()
{
/* character array of length 100 */
char str[100];
printf("Enter a string");
gets( str );
puts( str );
getch();
}
Output :
Enter a string
(Suppose we entered "Hello")
Hello
When you will compile the above code, it will ask you to enter a string. When you will enter the string, it will display the value you have entered.
Difference between scanf() and gets()
The main difference between these two functions is that scanf() stops reading characters when it encounters a space, but gets() reads space as character too.
Note :If you enter name as Hello World using scanf() it will only read and store Hello and will leave the part after space. But gets() function will read it completely.
Post a Comment
If you have any doubts, Please let me know
Thanks!