The basic course of C language (iii) input and output functions and control flow statements (5)

Source: Internet
Author: User
Tags printf
1.1.2 Unformatted Input-output function
The unformatted input-output function can be replaced by the standard formatted input-output function described above, but these functions are compiled with fewer code and less memory, which increases speed and is easier to use. The following are described separately.
One, puts () and gets () functions
1. puts () function
The puts () function is used to write a string to a standard output device (screen) and wrap it in a format that is:
Puts (s);
where S is a string variable (string array name or string pointer).
The puts () function is the same as the language printf ("%s\n", s).
Example 4:
Main ()
{
Char s[20], *f; /* Define string array and pointer variable * *
strcpy (S, "hello! Turbo C2.0 "); /* String array variable assignment * *
f= "Thank You"; /* String pointer variable assignment * *
Puts (s);
Puts (f);
}
Description
(1). The puts () function can only output strings, cannot output numeric values, or format transformations.
(2). You can write a string directly into the puts () function. such as: Puts ("Hello, Turbo C2.0");
2. Gets () function
The gets () function is used to read a string from a standard input device (keyboard) until the carriage return ends, but the carriage return does not belong to this string. The calling format is:
Gets (s);
where S is a string variable (string array name or string pointer).
The gets (s) function is similar to scanf ("%s", &s), but is not exactly the case when you enter a string using the scanf ("%s", &s) function, that is, if you enter a space, you think the input string ends, and the character after the space is processed as the next entry , the gets () function will receive the entire string entered until the carriage return.
Example 5
Main ()
{
Char s[20], *f;
printf ("What ' s Your name?\n");
Gets (s); /* Wait for input string until carriage return is complete * *
Puts (s); /* The input string output/*
Puts ("How old are You?");
Gets (f);
Puts (f);
}
Description
(1) The variable s in the gets (s) function is a string. If you are a single character, there is no error in compiling the connection, but a "Null pointer asignmemt" error occurs after running.

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.