Input and output functions in C language

Source: Internet
Author: User
Tags clear screen

1.1.1 formatting input and output functions
The Turbo C2.0 Standard Library provides two console format inputs, output functions printf (), and
scanf (), these two functions can read and write data in a variety of formats on a standard input.
The printf () function is used to write data to a standard output device (screen); The scanf () function is used from the standard input
Read data on the device (keyboard).

the printf () function
printf () function is a formatted output function that is typically used to output
information to a standard output device in a prescribed format. This function is often used when writing programs. The printf () function is called in the format:
printf ("< formatted string >", < Parameter table >);
Where the formatted string consists of two parts: a normal character that will be output as the original
, such as printf ("trials and Tribulations \ \"), and a formatted character, starting with "%", followed by one or several specified characters, to determine the output content format. The
parameter table is a series of parameters that need to be output, the number must be as many as the output
parameter as described in the formatted string, the parameters are separated by "," and the order one by one corresponds, otherwise the intended
error will appear.
1. Formatting specifier
Turbo C2.0 provides the following formatting rules:
━━━━━━━━━━━━━━━━━━━━━━━━━━
Symbolic function
——————————————————————————
% d decimal signed integer
%u decimal unsigned integer
%f floating-point number
%s string
%c single character
%p pointer value
%e exponential floating-point numbers
%x,%x unsigned integers in hexadecimal
% 0 unsigned integer represented in octal
%g automatically selects the appropriate notation
━━━━━━━━━━━━━━━━━━━━━━━━━━
Description:
(1). You can insert a number between "%" and a letter to indicate the maximum field width.
For example:%9.2f represents a floating-point number with an output field width of 9, where the decimal bit is 2, the integer bit is 6, the
decimal point is one, and not enough 9 bits are right-aligned. The
%8s represents the output of a 8-character string, not 8 characters aligns.

(3). You can control the output to align left or right, i.e. add a "-" sign between "%" and the letter
to indicate that the output is left-aligned, otherwise right-justified.
For example:%-7d represents the left-aligned output 7-bit integer.
If the length of the string or the number of integers exceeds the indicated field width, it is output at its actual length.
But for floating-point numbers, if the integer number of bits exceeds the width of the indicated integer digits, the actual integer digits will be output;
If the fractional number of bits exceeds the indicated decimal width, the output is rounded by the width of the description.
In addition, if you want to add some 0 before the output value, you should add 0 before the field width.
For example, d means that when you output a numeric value that is less than 4 bits, 0 is added to the front to make the total width
4 bits.


For example:%6.9s represents a string with a length of not less than 6 and not greater than 9. If it is greater than 9, , the
9th character will be deleted.
(2). You can add a lowercase letter L between "%" and the letter, indicating that the output is a long form.
For example,%ld means that the output long integer
%lf represents the output double floating-point number
2. Some special rules characters
━━━━━━━━━━━━━━━━━━━━━━━━━━
character Action
—————————————— ————————————
\ n NewLine
\f Clear screen and change page
\ r Enter
\ t tab character
\xhh denotes an ASCII code with 16-in notation,
where HH is 1 to 2 16 decimal number
━━━━━━━━━━━━━━━━━━━━━━━━━━

Second, scanf () function
The scanf () function is a formatted input function that reads input information from a standard input device (keyboard).
Its invocation format is:
scanf ("< formatted string >", < Address table >);
The formatted string includes the following three different types of characters;
1. Format specifier: The format specifier is basically the same as the format specifier in the printf () function.
2. Whitespace character: Whitespace character causes the scanf () function to omit one or more of the inputs in the read operation
A blank character.
3. Non-whitespace characters: a non-whitespace character causes the scanf () function to be removed from the read-out
Characters with the same white space characters.
The Address table is the address of all the variables that need to be read in, not the variable itself. This is associated with the printf () function
Completely different, pay special attention to it. Separate the addresses of each variable with ",".
Example 1:

int i,j;
scanf ("%d,%d", &i, &j);

The scanf () function in the example above reads an integer and then strips out the comma that is then entered, the most
Read into the other integer number. If "," this particular character is not found, the scanf () function terminates. If
The delimiter between the parameters is a space, you must enter one or more spaces between the parameters.
Description
(1). For string arrays or string pointer variables, the array name and pointer variable name itself
are addresses, so when using the scanf () function, you do not need to precede them with the "&" operator.
Example 2
Main ()
{
Char *p, str[20];
scanf ("%s", p);
scanf ("%s", str);
printf ("%s\n", p);
printf ("%s\n", str);
}

(2). You can add an integer between the "%" formatting specifiers in the formatted string to indicate
The maximum number of digits in any read operation.
In Example 2 if you can only enter 10 characters to the string pointer p, then the first scanf () function statement
Into
scanf ("S", p);
When the program runs, if the number of characters is greater than ten, p will not continue to read, and a later read
The entry function, scanf ("%s", str), starts reading in the 11th character.
There is an issue with the actual use of the scanf () function, which is illustrated below:
When you use multiple scanf () functions to enter multiple character variables consecutively, for example:
Main ()
{
Char C1, C2;
scanf ("%c", &C1);
scanf ("%c", &C2);
printf ("C1 is%c, C2 is%c", C2, C2);
}

Run the program, enter a character a after carriage return (to complete the input must enter), in the execution scanf
("%c", &C1), assign "A" to the variable c1, but the carriage return remains in the buffer, executing the input statement
scanf ("%c", &C2), the variable C2 output is a blank line, if the input ab after the return, then the output junction
The fruit is: C1 is A, C2 is B.
To solve these problems, you can add the Purge function fflush () before the input function. Modify the above procedure into:
#include <stdio.h>
Main ()
{
Char C1, C2;
scanf ("%c", &C1);
Fflush (stdin);
scanf ("%c", &C2);
printf ("C1 is%c, C2 is%c", C1, C2);
}

1.1.2 Unformatted Input-output functions
Unformatted input-output functions can be replaced by the standard format input and output functions described above, but
These functions are compiled with less code, are relatively small in memory, and thus improve speed, while also comparing
Convenient. The following are described separately.
One, puts () and gets () function
1. puts () function
The puts () function is used to write a string to a standard output device (screen) and wrap it in a format called:
Puts (s);
where S is a string variable (string array name or string pointer).
The puts () function works the same as the language printf ("%s\n", s).
Example 3:
Main ()
{
Char s[20], *f;
strcpy (S, "hello! Turbo C2.0″);
f= "Thank You";
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. The gets () function
The Get () 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. Its invocation 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 not identical, using scanf ("%s", &s)
There is a problem with the function input string, that is, if you enter a space, the input string ends,
The character after the space is processed as the next entry, but the Get () function receives the entire character of the input
String until the carriage return.
Example 4
Main ()
{
Char s[20], *f;
printf ("What ' s Your name?\n");
Gets (s);
Puts (s);
Puts ("How old is You?");
Gets (f);
Puts (f);
}

Input and output functions in C language

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.