Puts () and gets () functions are described in detail, and putsgets functions are described in detail.

Source: Internet
Author: User

Puts () and gets () functions are described in detail, and putsgets functions are described in detail.

Http://blog.csdn.net/pipisorry/article/details/39406501

Non-formatted input/output functions

These functions can be replaced by the standard formatted input and output functions described above. However, these functions have less code after compilation and consume less memory, which increases the speed and is convenient to use.

Puts () and gets () Functions

1. puts () function

The puts () function is used to write strings and line feed to the standard output device (screen,

Call format: puts (s );

S is a string variable (string array name or string pointer ).

The puts () function works the same way as printf ("% s \ n", s.

Main () {char s [20], * f; strcpy (s, "Hello world! ");/* String array variable assignment */f =" Thank you ";/* string pointer variable assignment */puts (s); puts (f );}


Note:

(1). The puts () function can only output strings and cannot output numeric values or perform format conversion.

(2). Strings can be directly written to the puts () function. For example:

Puts ("Hello, Turbo C2.0 ");

 

2. gets () function

The gets () function is used to read a string from the standard input device (keyboard) until the carriage return ends, but the carriage return does not belong to this string.

Call format: gets (s );

S is a string variable (string array name or string pointer ).

The gets (s) function is similar to scanf ("% s", & s), but not exactly the same. Use scanf ("% s", & s) A problem occurs when the function inputs a string, that is, if a space is entered, the input string ends,

The character after the space is processed as the next input, but the gets () function will receive the entire input string until the carriage return.

Main () {char s [20], * f; printf ("input something \ n"); gets (s ); /* wait until the input string ends by pressing enter */puts (s);/* output the input string */puts ("input something \ n"); gets (f ); puts (f );}


From:

Http://blog.csdn.net/pipisorry/article/details/39406501

Ref: http://www.cnblogs.com/wanhl/archive/2012/08/16/2641651.htmlcin、cin.get (), cin. getline (), getline (), gets () Usage



What are the usage of puts () and gets () functions?

The puts function is the output string. gets is the input string:

# Include <stdio. h>
Void main (void)
{
Char buf [256];
Puts ("Hello world from puts! ");
Gets (buf); // enter a string, but it must be within 256 characters.
}

Output: Hello world from puts!

A problem with puts (); and gets () Functions

Obviously, you use others' memory. Your C only has 3 bytes, and you use abcdefghi10 bytes. It is a miracle that no error is made.
Function prototype: char * gets (char * buffer );
The buffer used is a memory address, and the two-dimensional array is also a memory address, so they can also be used in two-dimensional. However, it is easy to calculate errors. If the mathematics is good, you can use them.
The meaning in the book is that two inputs are safe, and two or more inputs are insecure.

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.