Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Usage of format character i in scanf
Usage of format character i in scanf

Output table column)

For example: printf("i=%d,ch=%c\n",i,ch);

Explanation:

(1) "Format control" is a string enclosed in double apostrophes, also called "conversion control string". It includes two kinds of information:

①Format description : Composed of "%" and format characters, its function is to convert the output data into the specified format for output.

② Ordinary characters, that is, characters that need to be output as they are.

(2) "Output table column" is some data that needs to be output, which can be an expression

(3) The general form of the printf function can be expressed as

printf(parameter 1, parameter 2,..., parameter n)

The function is to output parameter 2~parameter n according to the format given by parameter 1

2. Format characters ( 9 types)

(1) d (or i) format character. Used to output decimal integers, there are the following usages:

①%d, output according to the actual length of integer data.

②%md, m is the width of the specified output field. If the number of digits in the data is less than m, spaces will be added to the left end. If it is greater than m, the actual number of digits will be output.

③%ld (%mld is also available), outputs long integer data.

For example: long a=123456;

printf("%ld",a);

(2) o format character, output in the form of octal number integer. Format: %o, %mo, %lo, %mlo are all acceptable.

(3) x (or X) format character, outputs an integer in the form of a hexadecimal number. Format: %x, %mx, %lx, %mlx are all acceptable.

(4)u format character, used to output unsigned data, that is, unsigned numbers, output in decimal form. Format: %u, %mu, %lu are all acceptable.

See: li4-3.c/*Unsigned data output*/

(5)c format character, used to output a character. Format: %c, %mc are both acceptable.

(6)s format character, used to output a string. Format: %s, %ms, %-ms, %m.ns, %-m.ns are all acceptable.

See: li4-5.c /*String output*/

(7) f format character, used to output real numbers (including single and double precision), in decimals form output. Format: %f, %m.nf, %-m.nf are all acceptable.

Note: The number of significant digits for single-precision real numbers is generally 7 digits, and for double precision, it is 16 digits.

See: li4-6.c/*The number of significant digits when outputting a single-precision real number*/

li4-7.c/*The number of significant digits when outputting a double-precision real number */

li4-8.c/*Specify the number of decimal places when outputting real numbers*/

(8) e (or E) format character, output real numbers in exponential form. Format: %e, %m.ne, %-m.ne are all acceptable.

(9) The g (or G) format character is used to output real numbers. It automatically selects the f format or the e format (select the one with a smaller width when outputting) according to the size of the value.

3. Note

(1) Except for X, E, and G (expressed in uppercase letters), other format characters must be in lowercase letters;

( 2) The "format control" string can contain escape characters;

(3) If you want to output the character "%", you should use two consecutive % in the "format control" string, For example:

printf("%f%%",1.0/3);

The scanf() function is encountered by all C language learners in the process of learning C language. The second function (the first function is printf(), Brian W.Kerninghan & Dennis M.Ritchie's "hello, world" program is basically the first example for all C language learners), so the scanf() function It should be a function that C learners can use proficiently, but many beginners cannot use this function well. In actual programming, they use the scanf() function incorrectly, causing the program to generate some kind of error and not run normally, or even occur. "The scanf() function has BUG", "scanf() function is useless" and other misconceptions.

This article combines the author's programming practice and the problems encountered by netizens on the forum to explain the doubts. However, the author's level is limited (rookie level), and there are inevitably some fallacies. I hope experts can give me some advice.

(Email: knocker.k@126.com)

This article is divided into the first and second parts to describe the usage of the scanf() function in C language, focusing on common problems that occur during the use of the scanf() function. Errors and Solutions. Of course, some of the solutions in this article can be better solved by using other functions and methods, but this article only discusses the scanf() function itself.

In the previous article, the composition of the scanf() function control string was introduced in detail. The next article uses actual routines to introduce common errors and countermeasures when using the scanf() function control string.

2. Control string of scanf() function

Function name: scanf

Function: Perform formatted input

Usage: int scanf(char *format[,argument,...]);

The scanf() function is a general terminal formatted input function. It reads input information from the standard input device (keyboard). Can read data of any intrinsic type and automatically convert the values ??into the appropriate on-board format.

The calling format is: scanf("",

);

The scanf() function returns the number of successfully assigned data items. When an error occurs Then return EOF.

The control string consists of three types of characters:

1. Format specifier;

2. White space character;

3. Non-whitespace character;

(A) Format specifier

Format character description

%a reads a floating point value (valid only in C99)

p>

%A Same as above

%c reads a character

%d reads a decimal integer

%i reads decimal, octal, decimal Hexadecimal integer

%o Read in octal integer

%x Read in hexadecimal integer

%X Same as above

%c reads a character

%s reads a string

%f reads a floating point number

%F Same as above

%e Same as above

%E Same as above

%g Same as above

%G Same as above

%p reads a pointer

%u reads an unsigned decimal integer

%n has read the equivalent number of characters of the value so far

%[] scans the character set

< p>%% Read % symbol

Additional format description character table

Modifier description

L/l length modifier input "long" data

h length modifier input "short" data

W integer constant specifies the width of the input data

* asterisk reads a data empty

hh,ll Same as h,l but only valid for C99.

(B) Blank characters

Blank characters will cause the scanf() function to omit one or more blank characters in the input during the read operation. The blank characters can be space, tab, newline Wait until the first non-whitespace character appears.

(C) Non-whitespace characters

A non-whitespace character will cause the scanf() function to eliminate characters that are the same as the non-whitespace character when reading.