2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
The general format of the printf function is printf (format control, output table)
For example:
printf("%d,%cn",i,c);
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.
It is some data that the program needs to output, which can be a constant, variable or expression.
- int data = 10;
- int chushu = 3;
- float result;
-
- result = (float)data/chushu;
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.
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:
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.
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);
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.
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.
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.
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
- #include<stdio.h>
-
- int main()
- {
- char a;
- char b;
-
- printf("请输入一个大写字母:");
- scanf("%c",&a);
- b = a+32;
- printf("其对应的小写字母为:%c",b);
-
- return 0;
- }