Technology Sharing

C language - printf, scanf, other input and output functions

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

printf Function

1. The general format of the printf function:


The general format of the printf function is printf (format control, output table)
For example:

printf("%d,%cn",i,c);

(1) "Format Control"

It is a string enclosed in double quotes, called "conversion control string", or "format string" for short. It includes two pieces of information:
① Format declaration. The format declaration consists of "%" and format characters, such as %d, %f, etc. Its function is to convert the output data into the specified format and then output it. The format declaration always starts with the "%" character.

Among them: d represents a decimal integer, c represents a single character, s represents multiple characters, f represents a decimal, x represents output in hexadecimal format, p represents printing the memory address, which is also output in hexadecimal format, and the output address uses the variable address operator &.
② Ordinary characters. Ordinary characters are characters that need to be output as they are. For example, the comma, space, and newline characters in the double quotes in the printf function above can also include other characters.


(2) "Output table columns"

It is some data that the program needs to output, which can be a constant, variable or expression.

Note:
(1) Forced conversion: converting integers into decimals
  1. int data = 10;
  2. int chushu = 3;
  3. float result;
  4. result = (float)data/chushu;
(2) Specify the data width and number of decimal places using %m.nf
The format "%7.2" has been used to specify that the output data occupies 7 columns, including 2 decimal places. The last digit is rounded up or down. If the decimal part is specified as 0, not only will the decimal not be output, but the decimal point will also not be output. Therefore, do not easily specify the number of decimal places to be 0.
(3) The output data is aligned to the left, using %-m.nf
Adding a minus sign in front of m,n has basically the same effect as the %m.nf form, but when the data length does not exceed m, the data is moved to the left and spaces are added on the right.

scanf Function

1. The general form of the scanf function


scanf (format control, address list)

The meaning of "format control" is the same as that of printf function. "Address list" is a list composed of several addresses, which can be the address of a variable or the first address of a string.


2.Format syntax in scanf function


Similar to the format declaration in the printf function, it starts with % and ends with a format character, and additional characters can be inserted in the middle.
The scanf function can be rewritten as follows

scanf("a=%f,b=%f,c=%f",&a,&b,&c);


In addition to the format declaration %f, there are some common characters in the format string ("a=", "b=" and ",").

Note:

(1) Address symbol &

The "format control" in the scanf function should be followed by the variable address, not the variable name. For example, if a and b are integer variables, if you write

scaní("%f%f%f",a,b,c);

is wrong. "a,b,c" should be changed to "&a,&b,&c". Many beginners often make this mistake.

(2) Input as is

If there are other characters in the "format control string" besides the format declaration, the same characters as these characters should be entered in the corresponding positions when entering data.

scanf("a=%f,b=%f,c=%f",&a,&b,&c);

When entering data, you should enter the same characters in the corresponding positions.
a=1,b=3,c=2 (pay attention to the input content)

So do not add spaces (you can use newline input for spaces) or commas in scanf, just write the data you want to input directly:

scanf("%d%d%f",&data,&data1,&data2);
(3) Character input

When declaring input characters using the "%c" format, space characters and characters in "escape characters" are input as valid characters, for example:
 

scanf("%c%c%c,&c1,&c2,&c3);

When executing this function, you should enter 3 characters consecutively without any spaces in between.
It is wrong to insert a space between two characters.

Spaces and newlines are also characters and cannot be used when inputting.

(4) Mixed input

When entering numerical data, if you enter a space, enter, or press the Tab key, or encounter an illegal character (a character that does not belong to a numerical value), the data is considered to have ended. For example:
 

scanf("%d%c%f,&a,&b,&c);

The correct input is: 1234h7.6

The first data corresponds to the %d format. After entering 1234, the character 'h' is encountered. Therefore, the system believes that there are no more numbers after the value 1234, and the first data should end here, so 1234 is sent to variable a. The subsequent character 'h' is sent to the character variable b. Since %c only requires the input of one character, the system determines that the character has been entered, so there is no need to add a space after the input character 'h'. The value after the character 'h' should be sent to variable c. If 1230.26 is mistakenly typed as 123o.26 due to negligence, since the letter o appears after 123, it is considered that the numerical data ends here, and the following characters are not read.

Therefore, when we are inputting different types of data, we should use the corresponding scanf function according to the type. There are as many scanf functions as there are types.

(5) Multiple scanf functions

When a program contains two or more scanf functions, the latter scanf function will treat the newline character after the previous scanf function's input as its own input, causing the latter scanf function to be unable to obtain input normally. Therefore, a getchar() should be inserted between the two scanf functions to absorb the extra newline character.

Other input and output functions:

puts: Difference from printf
(1) Automatically add line breaks
(2) printf supports a variety of output formats, while puts outputs strings.

getchar、putchar

Input and Output Exercises:

1. Input uppercase letters and output lowercase letters

Here we use the knowledge of ASCII code. The ASCII code corresponding to the uppercase letter + 32 is the ASCII code corresponding to the lowercase letter.

So the code example is

  1. #include<stdio.h>
  2. int main()
  3. {
  4. char a;
  5. char b;
  6. printf("请输入一个大写字母:");
  7. scanf("%c",&a);
  8. b = a+32;
  9. printf("其对应的小写字母为:%c",b);
  10. return 0;
  11. }