In-depth analysis of input and output in C Language

Source: Internet
Author: User



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:










Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.