C printf and scanf & multiple sets of input

Table of contents

1. Introduction to the library function scanf

2. Input of multiple sets of data

2.1 EOF method

2.2 Counting method

2.3 Special value method

3. How to stop typing


1. Library function printf

1. printf()  is used to format the output to the screen. The printf()  function is declared in  the "stdio.h"  header file.

2. Regarding the return value: the return value of this function is an incidental use of its printout function, which may be used when checking output errors.

#include<stdio.h>
int main()
{
    int bph2o = 212;
    int rv;
    rv = printf("%d F is water's boiling point.\n", bph2o);  //把printf返回值赋给rv
    printf("%d", rv);
    return 0;
}

//输出结果为:212 F is water's boiling point.
             32  //32是上一个输出的所有字符数,包括空格和不可见换行符'\n'

3. In printf, if the content in " " is newline, an error will be reported.

solution:

① Use multiple printf functions, be careful not to use \n, so that the next printf content can be continued.

② Use the backslash (/) and Enter (or Return) key combination to break the line.

③ String concatenation introduced by ANSI C. Between two double-quoted strings separated by blanks, the C compiler will treat multiple strings as one string. Therefore, the following three forms are equivalent:

printf("hello, young lovers, wherever you are.");

printf("hello, young"  "lovers "   ", wherever you are.");

printf("hello, young lovers"   
", wherever you are.");

Note: "young" "lovers" is equal to "younglovers"

Only "young" "lovers" is equal to "young lovers".

2. Library function scanf

1. Description: Read formatted input from stdin.

2. Declaration: Indicates that the return value of scanf is int type data.

int scanf(const char *format, ...)

3. About the return value:

① If successful, the function returns the number of successful matches and assignments.

② If the end of the file is reached or a read error occurs, EOF is returned.

4. When scanf reads a string, it will stop reading if it encounters a space.

3. Input of multiple sets of data

2.1 EOF method

1. Look at the code first:

#include <stdio.h>
 int main()
 {
 	int a,b;
 	while(scanf("%d%d",&a,&b)!=EOF) 
    //while一定不要有分号,否则你就会发现你无限输入但并不输出
 	{
 		printf("%d\n",a+b);
	}
 	return 0;
 }

2. Introduction to the method and principle: Because the input data of the online evaluation system is stored in a file, it can be judged whether the input data is over by checking whether the file is over. When scanf reads the end of file, it will return the identifier EOF (end of life). EOF is a predefined constant equal to -1. Determine whether to exit the loop by judging whether the return value of scanf is EOF.

2.2 Counting method

1. Look at the code first:

#include <stdio.h>

int main()
{
    int T;
    int a;
    scanf("%d",&T);     //这里T为要输入数据的组数
    while(T--)     //这里的T每次减1知道为0,结束循环
    {
        scanf("%d",&a);
        printf("%d ",a);
    }
    return 0;
}

//当然除了while,用for自增也是可以的

 #include <stdio.h>
 int main()
 {
 	int T;
    int i;
 	scanf("%d",&T);    //T为你要输入的数据组数
 	for(int i=1;i<=T;i++)     //这里i从1开始,一直输入到你需要的组数T,结束循环
 	{
 		int a
 		scanf("%d",&a);
 		printf("%d\n",a);
	 }
 	return 0;
 }
、

2. Method principle introduction: Through the loop method, first determine the number of data sets you want to input, and then loop the input data until the number of data sets you set in advance is reached to end the cycle.

2.3 Special Value Method (Pacesetter Method)

1. Look at the code first:

#include<stdio.h>

int main()
{
    int a;
    scanf("%d", &a);
    while(a != 0)    //这里的0是我规定的特殊值,也可以是别的值
    {
        printf("%d", a);
        scanf("%d", &a);   //输出后再次输入,继续进入循环
    }
    return 0;
}

2. Method principle introduction: When the input data is a special value specified by you, if the loop condition is not satisfied, the next input will be stopped.

4. How to stop typing

1. Windows platform, that is, the dos command window: input CTRL+Z to end the file.

Note: VS needs to enter this command three times (press Enter after pressing the command once) (currently the reason is unknown)

2. Other platforms, such as Unix, Linux, Mac, etc.: Enter CTRL+D to end the file.

Guess you like

Origin blog.csdn.net/2303_77414881/article/details/131798515