C Language Input and Output Functions

Source: Internet
Author: User

In C language, input and output functions play an important role. They are the only way for our programs to interact with users, now I will introduce several common input and output functions in C language to you. The following are all for the VC debugging platform.

(1) format input and output functions

(1) scanf () format the Input Function

Format input function scanf (), which is in the format of scanf ("Format String", input list ). A "Format String" contains three types of characters: A and A. It must start with "%" to indicate the type and format of the data in the input list. B. common characters, which must be input in the original format. C. Blank characters (space, hop key, and enter key) are the default Separators of adjacent data.

Note the following in the scanf () function:

[1] The format strings in the scanf () function do not have escape characters. Therefore, all characters in the format strings must be input as they are.

For example:

Int;

Scanf ("% d/N", & A); if we want to assign 10 to a, what we need to input on the keyboard is:

10/n press enter to assign 10 to A. Here,/N is not an escape character, but a common character.

[2] If the input list variable is of the basic type (that is, integer, real, and character type), you must use the address operator (except when the input string is in the character array ), it must be a variable and cannot be a constant or expression.

For example:

Int A, B;

Scanf ("% d", a, B); this is invalid. The scanf function is used to store the values of A and B in the memory according to the address of A and B. "& A" refers to the address of a in memory.

[3] An attempt to specify the accuracy of input data

Float;

Scanf ("%. 2f", & );

This is not legal. Precision cannot be specified when data is input. Remember that a program cannot control the accuracy of data input by people operating on it.

[4] The input data method does not comply with the requirements

① Scanf ("% d", & A, & B );

When entering data, you cannot use commas (,) as the delimiter between the two data. For example, the following input is invalid: When you enter 3 or 4 data, one or more spaces are used to separate the two data. You can also use the Enter key, the hop key tab.

② Scanf ("% d, % d", & A, & B );

C: If there are other characters besides the format description in the "format control" string, you should enter the same characters as these characters when entering data. The input below is valid: 3, 4

It is incorrect to use spaces or other characters instead of commas. 3 4

Another example is scanf ("A = % d, B = % d", & A, & B );

The input format is as follows: a = 3, B = 4

[5] The format and requirements of input characters are inconsistent

When the characters are entered in "% C" format, "space character" and "Escape Character" are both valid characters.

Scanf ("% C", & C1, & C2, & C3 );

For example, enter a B c

The character "a" is sent to C1, the character "is sent to C2, and the character" B "is sent to C3, because % C requires only one character to be read, and spaces are not used as the interval between the two characters.

[6] when a single character is entered using the format character "% C", spaces, enter keys, and other characters will be used as valid characters. Therefore, pay attention to the use of fflush (stdin.

[7] note that the number of elements in the input list must be the same as the number of characters in the format string.

[8] When entering long integer data and double-precision floating-point numbers, the length modifier "L" must be used ".

[9] Do not use an accesskey where an accesskey is not used.

For example, char STR [10];

Scanf (% C, & Str); Do not add the address character here &.

 

(2) printf () format output function

The format output function printf () is in the format of printf ("Format String", output list ). The format string here also contains three types of characters, but the characters here are slightly different from those of the scanf () function. These three types of characters are respectively A and format characters starting with %. They are used to describe the format in which the data in the output list is output. B. escape characters, which provide special formatting control. They consist of a slash (/) and a character. The backslash causes the printf () function to interpret a character in a special way. C. Common characters, except the format characters and escape characters.

Note the following in the printf () function:

[1] The output list consists of several output items. The output items can be constants, variables, or expressions.

[2] The format characters in the format string must correspond to the element types in the output list.

For example:

Float a = 1.9999;

Printf ("% d", a); the final output result is garbled.

Another example is:

Int A = 12;

Printf ("% F", a); the final output result is 0.000000. type conversion is not performed here.

However, if you want to output the above integer A as a floating point number, you must perform forced type conversion. For example:

Int A = 12;

Printf ("% F", (float) A); the output result is 12.000000.

[3] The number of characters in a format string must be the same as the number of elements in the output list.

For example:

Int A = 12;

Printf ("% d, % d", a); A 12 and garbled code will be output on the screen.

[4] When a floating point number is output in the format of %. NF, the number is rounded off.

For example:

Float a = 1.9999;

Printf ("%. 2f", a); the final output result is 2.00.

[5] The length modifier "L" is not required when outputting long integer data, because in VC, the length of long and INT is 4, "L" can be excluded from all ".

[6] In % ± nd, N indicates the width of n characters on the screen when a decimal integer is output. If n is preceded by a plus sign, it indicates the right alignment of the output integer, and spaces are filled on the left of the output integer. If-is before N, it indicates the left alignment of the output integer, and spaces are filled on the right of the output integer.

[7] In-128 ~ Integers between 127 can be output in character format or in integer format.

For example:

Int A = 97;

Printf ("% C", a); the output result is 'A', because 'A' is stored in the memory in the form of ASCII code.

Another example is:

Char A = 'a ';

Printf ("% d", a); the output result is 97 for the same reason.

 

(2) Single-Character Input and Output Functions

The C language provides two formats without control, specifically for the getchar () and putchar () functions used to input and output a single character ().

(1) Single-Character Input Function getchar ()

[1] The getchar () function has no parameter and has a return value. The returned value is the character entered.

[2] The getchar () function can receive only one character at a time.

[3] The getchar () function also uses spaces, enter keys, and other characters as valid characters. Therefore, be careful when using the fflush (stdin) function.

(2) Single-character output function putchar ()

[1] The putchar () function has parameters and does not return values. The parameter is the character to be output. It can be a character variable or a character constant.

[2] The putchar () function outputs one character at a time to the display.

 

(3) string Input and Output Functions

The C language also provides two unformatted functions for string processing, gets () and puts ().

(1) gets () is used to receive user input strings. This is a function with parameters. The parameters are of the character array type, the input string is passed to the character array through address transfer.

(2) puts () is used to display a string to the user. It requires a string parameter to display this parameter and wrap it automatically.

For example:

Puts ("Hello, world. ); and printf ("Hello, world. /n "); the effect is the same, if it is puts (" hello. world. /n), two line breaks are output. That is to say, a string in puts () can contain escape characters.

Input string:

Scanf ("% s", in_buff );

-- The input can be formatted; the second parameter is the address, which can be a character pointer or a character array;Cross-border check of character array data is not provided; Press enter to end the input.

Gets (in_buff );

-- The parameter is the address, which can be a character pointer or a character array;The character array out-of-bounds check is not provided; Press enter to end the input.

Fgets (in_buff, buffer_size + 1, stdin );

-- Parameter 1 indicates the character point or character array. Parameter 2 indicates the number of characters to be entered. + 1 indicates the end mark of the string. Parameter 3 indicates the input standard;Cross-border check, With buffer_size + 1 as the standard; end the input with a carriage return.

Input characters:

Getch ();

-- Read a character from the console, but it is not displayed on the screen. Do not press enter to finish.

Getche ();

-- Read a character from the console and display the screen. Do not press enter to finish.

Getchar ();

-- Read a character from the console and display the screen. Press enter to finish (If you use this function consecutively, note that the last input carriage return will be read when you call this function next time.).

Scanf ("% C", char *);

-- Read a character from the console and display the screen back. You need to press enter to end (the same as above ).

These are several input and output functions commonly used in C language, and these functions are included in the stdio. h header file. Among these functions, the use of format input and output is the most likely to cause problems.

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.