13. Format input\output function

Table of contents

1. Format input function

2. Format output function


1. Format input function

format input function scanf . The function of this function is to specify a fixed format, and receive the data entered by the user on the keyboard according to the specified format, and finally store the data in the specified variable.

The format characters commonly used in the scanf function are:

The general is as follows:

As can be seen from the general format of the scanf function, the format control in the parameter position is the same as that of the printf function. For example, "%d" means a decimal integer, and "%c" means a single character. The address list is used to give the address of the receiving data variable. For example, the code to get an integer data is as follows:

In the above code, the "&" symbol means to take the address of the iInt variable. The user does not need to care about the specific address of the variable

At least, as long as "&" is added before the identifier of the variable, it can indicate the address of the access variable.

Note : When writing a program, in the address list of scanf function parameters, you must use the address of the variable instead of the identifier of the variable, otherwise the compiler will prompt an error.

Example 1 : Use the scanf function to get two integer data input by the user, because the scanf function can only be used for input operations, so if you want to display information on the screen, you need to use the display function

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
	int i1, i2;  //定义两个整型变量
	puts("请输入两个数字:");  //通过puts函数输出提示信息的字符串
	scanf("%d%d", &i1, &i2);  //通过scanf函数得到输入数据
	printf("第一个数是:%d\n", i1);  //输出第一个数字
	printf("第二个数是:%d\n", i2);  //输出第二个数字
	return 0;  //结束,无用时可省略
}

operation result:

Note : Using the scanf function directly will report an error . There are two solutions. The first ( not recommended) is to change scanf to the scanf_s function ; the second (recommended) is to define #define _CRT_SECURE_NO_WARNINGS at the top of the code .

In addition, in the format description, the additional symbols that can be inserted between the "%" symbol and the above format characters are:

Example 2 : Use the additional formats of the scanf function for format input in turn, compare the results before and after the input, and observe the effect of the additional formats

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
	long i;  //定义长整型变量
	short ii;  //定义短整型变量
	int i1,i2;  //定义整型变量
	char c[10];  //定义字符数组变量

	printf("输入长整型变量:\n");  //输出提示信息
	scanf("%ld", &i);  //输入长整型变量

	printf("输入短整型变量:\n");  //输出提示信息
	scanf("%hd", &ii);  //输入短整型变量

	printf("请输入两个数字:\n");  //输出提示信息
	scanf("%d%d", &i1, &i2);  //输入整型数据

	printf("输入字符串:\n");  //输出提示信息
	scanf("%3s", c);  //输入字符串

	printf("长整型值为:%ld\n", i);  //输出长整型值
	printf("短整型值为:%ld\n", i);  //输出短整型值
	printf("第一个数是:%d\n", i1);  //输出第一个数字
	printf("第二个数是:%d\n", i2);  //输出第二个数字
	printf("字符串为:%s\n", c);  //输出字符串的前三个字符
	return 0;  //结束,无用时可省略
}

operation result:

2. Format output function

The printf function is a function for format output , also known as a format output function. The format characters related to the printf function are:

The function of the printf function is to output some arbitrary types of data to the terminal (output device), and its syntax format is as follows:

1. Format Control

Format controls are strings enclosed in double quotes, also known as conversion control strings. These include format characters and normal characters.

  • The format character is used for format description, and its function is to convert the output data into the specified format. Format characters usually start with a "%" character.
  • Ordinary characters are characters that need to be output as-is, including commas, spaces, and newlines within double quotes.

2. Output list

The output list lists some data to be output, which can be variables or expressions.

For example, to output an integer variable, the code is as follows:

Execute the above statement, the displayed character is "this is 10". The characters in the format control double quotes are "this is%d", where the "this is" string is an ordinary character, and "%d" is a format character, indicating that the output is the following iInt data.

Since printf is a function, the two positions of "format control" and "output list" are parameters of the function, so the general form of the printf function can be expressed as:

Example 3 : Use the format output function printf to output variables of different types

#include<stdio.h>
int main()
{
	int i = 1;  //定义整型变量
	char c = 'A';  //定义字符型变量
	float f = 12.34f;  //定义单精度浮点型
	
	printf("整数是:%d\n", i);  //输出结果
	printf("字符是:%c\n", c);
	printf("浮点数是:%f\n", f);
	printf("字符串是:%s\n", "I LOVE YOU!");
	return 0;  //结束
}

operation result:

In addition, in the format description, the additional symbols that can be inserted between the "%" symbol and the above format characters are:

Note : When using the printf function, except for X, E, and G, other format characters must use lowercase letters, such as "%d" cannot be written as "%D". If you want to output the "%" symbol, you can use "%%" in the format control to output.

Example 4 : Use the additional format specification characters of the printf function to design the output data in a more precise format

#include<stdio.h>
int main()
{
	long i = 100000;  //定义长整型变量并赋值
	printf("该长整型变量是:%d\n", i);  //输出长整型变量
	
	printf("字符串是:%s\n", "LOVE");  //输出字符串
	printf("字符串是:%10s\n", "LOVE");  //使用m控制输出列
	printf("字符串是:%-10s\n", "LOVE");  //使用-表示向左靠拢
	printf("字符串是:%10.3s\n", "LOVE");  //使用n表示取字符数
	printf("字符串是:%-10.3s\n", "LOVE");  
	return 0;  //结束
}

operation result:

Guess you like

Origin blog.csdn.net/qq_25990967/article/details/128793496