puts function
The puts function can be used to write a
sequence of characters to the standard output stream:
#include <stdio.h> int *puts(const char *s);
s refers to the character array that
contains the string. If the function is successful, it returns
0. Otherwise, it returns a nonzero value.
The following program shows an example of using
gets and puts.
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <stdio.h>
#define MAX_LENGTH 80
int main(void)
{
char string[MAX_LENGTH];
printf("Please, write a line of no more than 80 characters:\n");
gets(string);
printf("The entered line is:\n");
puts(string);
return 0;
}
|
As you can see, the use of gets is quite
dangerous. Since gets does not know the size of the
character array you pass it, it simply reads data until a newline is
encountered. This can be a trouble when user types more characters than
your array can hold. As a result, your other variables would get
overwritten and your program will crash or, at best, behave
unpredictably. We will see more secure functions later on.