Output string:
Puts () function:
The puts () function only accepts one parameter (a pointer to the string to be displayed ). Because a literal string is a pointer to a string
Puts () can be used to display literal strings and string variables.
After puts () is displayed as a string, it is automatically wrapped.
Puts () is a standard output function that must contain stdio. h.
Printf () function:
The printf () function is a library function that can be used to display strings and use the conversion specifier % s.
When printf () Encounters % s in its format string, it matches % s with the corresponding parameter in the parameter list.
For a string, the parameter must be a pointer to the string to be displayed.
# Define _ CRT_SECURE_NO_WARNINGS # include
Void main () {char input [81]; // Note: it can contain a maximum of 80 characters ("Enter data: \ n"); gets (input ); printf ("input data: \ n % s", input );}
Running result:
Read strings from the keyboard:
The gets () function reads a string from the keyboard. When the gets () function is called, it keeps reading characters from the keyboard
Line breaks (generated by pressing Enter. If an error occurs while reading the string, gets () returns null.
Note: Since gets () does not always know how many characters will be read, gets () will continuously store characters, which may exceed
The end of the buffer. Therefore, be careful when using this function.
Scanf () uses a formatted string that tells it how to read input information.
# Define _ CRT_SECURE_NO_WARNINGS # include
Void main () {char input1 [255], * ptr1; puts ("Enter data: \ n"); if (* (ptr1 = gets (input1 ))! = NULL) {// Note: Do not gets (ptr1); Exceptions will occur because ptr1 is not initialized. Printf ("your input data is: % s \ n", input1); puts ("output data with puts ():"); puts (ptr1 );} system ("pause ");}
Running result: