(03) format input and output

1, Concept introduction

Basic learning stage: keyboard input and screen output.
Input data from the keyboard and realize simple human-computer interaction.
Output integers, floating-point numbers, characters, strings, and align data by formatting.

1. Standard input and output

In C language, there are three functions that can be used to input data on the keyboard. They are:
1) gets(): used to enter a line of string;
2) getchar(): used to enter a single character;
3) scanf(): it can input various types of data. As the most flexible, complex and commonly used input function, it can not completely replace the first two, but it must be mastered.

In C language, there are three functions that can be used to output data on the screen. They are:    
1) puts(): only string can be output, and the line will wrap automatically after output;
2) putchar(): only a single character can be output;
3) printf(): it can output various types of data. As the most flexible, complex and commonly used output function, it can completely replace both, so it must be mastered.

The naming meaning of scanf function is: Scan and Format, that is, Format input.
The printf function is named after Print and Format, that is, formatted output.

By analogy with output, we find that the difference between input and output lies in:
(1) Different function names;
(2) Input is missing line breaks \ n;
(3) The address character &;

2. Format

The formatting of input and output can be compared.

When we input, it is actually a string, but after the string is input, it may be used as an integer or a string. The computer itself does not know the rules. The person who needs to write code tells it. The process of telling it how to input is called formatting.

The output of the computer may be called one decimal place or two decimal places when we don't know how to format the code.

2, Formatted input of integers

1. Input of single data

#include <stdio.h>
int main() 
{
    int a;
    scanf("%d", &a);
    printf("%d\n", a);
    return 0;
}
1314↙  //↙ Representative enter
1314

2. Input of multiple data

Two data:

#include <stdio.h>
int main() 
{
    int a, b;
    scanf("%d", &a);
    scanf("%d", &b);
    printf("%d %d\n", a, b);
    return 0;
}
520↙
1314↙
520 1314

You can also put the input on one line:
The input of multiple data can be completed in one scanf statement.

#include <stdio.h>
int main() 
{
    int a, b;
    scanf("%d %d", &a, &b);
    printf("%d %d\n", a, b);
    return 0;
}
520 1314↙
520 1314

3. Space immunity

For the case where there is one space and multiple spaces between the input data:

#include <stdio.h>
int main()
{
    int a, b;
    scanf("%d %d", &a, &b);
    printf("%d %d\n", a, b);
    return 0;
}
520    1314↙
520 1314
#include <stdio.h>
int main()
{
    int a, b;
    scanf("%d       %d", &a, &b);
    printf("%d %d\n", a, b);
    return 0;
}
520 1314↙
520 1314

4. Carriage return settlement

scanf() settles an input with a carriage return.
Each time the user presses the Enter key, the computer will think that an input operation has been completed, and scanf() starts to read the content entered by the user and extract valid data from it according to the formatted content defined by us. As long as the user's input content matches the formatted content, it can be extracted correctly.

3, Input buffer

#include <stdio.h>
int main()
{
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    printf("%d %d %d %d\n", a, b, c, d);
    return 0;
}

Here are our expected inputs

1 2 3 4↙
1 2 3 4

1. Less input

We try to input less than one number. After pressing enter, we find that the program has no output. When we input the next number again, the correct output is generated:

1 2 3↙
4↙
1 2 3 4

2. Multiple input

We tried to input one more number. After pressing enter, we found that the first four numbers we entered were output:

1 2 3 4 5↙
1 2 3 4

3. Try again

We add a line of code, that is, after outputting four numbers, we call scanf():

#include <stdio.h>
int main()
{
    int a, b, c, d, e;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    printf("%d %d %d %d\n", a, b, c, d);
    scanf("%d", &e);
    printf("%d\n", e);
    return 0;
}

Then we use the above method of inputting 5 numbers at one time:

1 2 3 4 5↙
1 2 3 4
5

At this time, we found that the program was running normally.

This is because the data we input from the keyboard is not directly handed over to scanf(), but put into the input buffer. When we press enter, scanf() reads the data in the input buffer. If the data in the buffer meets the format requirements given by scanf(), the reading ends; Otherwise, continue to wait for user input, or read fails.

[example 1] give a piece of code as follows, and give an input. What is the output.

#include <stdio.h>
int main()
{
    int a = 9, b = 8, c = 7, d = 6, e = 5;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    printf("%d %d %d %d\n", a, b, c, d);
    scanf("%d", &e);
    printf("%d\n", e);
    return 0;
}

Enter as follows:

1 2b 3 4 5↙

4, Formatted input of other data types

1. String input

As for strings, we will focus on them after talking about arrays. There are also many matching algorithms applied to strings, which is a very important content.
Remember that when entering a string & you can leave it blank:

#include <stdio.h>
int main()
{
	char str[100]; 
    scanf("%s", str);    // (1) More commonly used!!!
    printf("%s\n", str);
    
    scanf("%s", &str);   // (2)
    printf("%s\n", str);
    return 0;
}

2. Do a simple fortune telling game

#include <stdio.h>
int main()
{
	char str[100]; 
	int height;
	
	printf("Please enter your name:");
    scanf("%s", str);
    
    printf("Please enter your height(cm): ");
	scanf("%d", &height);
	
	printf("%s Great Xia, height%dcm,It is a talent once in a century. As long as you study hard C Language will become a great thing in the future!\n", str, height);
    return 0;
}

5, Format output

1. Data type formatting

All formatting starts with a percent sign%.

1) Integer

For int, we use% d to format the content to be output, and then output it. The simple understanding is to replace% d with the corresponding variable,% lld is used to format long long variables:

#include <stdio.h>
int main()
{
    int a = 520;
    long long b = 1314;
	printf("a is %d, b is %lld!\n", a, b);
	return 0;
}
a is 520, b is 1314!

2) Floating point number

For floating-point numbers, we use% f to format single precision floating-point numbers; Use% lf to format double precision floating-point numbers, and use Add "number" to represent the number to be output, accurate to several decimal places:

#include <stdio.h>
int main()
{
	float f = 1.2345;
	double df = 123.45;
	printf("f is %.3f, df is %.0lf\n", f, df);
	return 0;
}
f is 1.235, df is 123


In addition, the difference between single precision and double precision is that the precision of double precision is higher, that is, the range of decimals that can be expressed is more accurate. This will be introduced in detail when introducing the storage method of floating-point numbers.

3) Character

For characters, we use% C to format; The characters in C language are enclosed in single quotation marks. Of course, the concept of characters is too far. A separate chapter will be opened. For details, please refer to ASCII code.
By the way, let's explain the line break that has always appeared but I won't mention it. This symbol is an escape symbol. It doesn't represent two characters (backslash \ and letter n), but the meaning of line break;

#include <stdio.h>
int main()
{
    char ch = 'A';
    printf("%c\n", ch);
	return 0;
}
A //The output is A character A

[example 1] line 1 outputs one 1, line 2 outputs two 2, line 3 outputs three 3, and line 4 outputs four 4. (understand the meaning of line breaks)

#include <stdio.h>
int main()
{
    printf("1\n");
    printf("22\n");
    printf("333\n");
    printf("4444\n");
	return 0;
}

It can also be solved with one statement:

#include <stdio.h>
int main()
{
    printf("1\n22\n333\n4444\n");
	return 0;
}

4) String

String is a combination of multiple characters, enclosed in double quotation marks and formatted with% s:

#include <stdio.h>
int main()
{
    char str[100] = "I love you!";
    printf("%s\n", str);
	return 0;
}
I love you!

2. Align formatting

All formatting above has a% and a letter. In fact, there are other contents between the percent sign and the letter:

It mainly includes the following contents:
   1) negative sign: If yes, output according to left alignment;
   2) number: specify the minimum width of the field. If it is insufficient, fill it with spaces;
   3) decimal point: use and to separate the minimum field width and precision;
   4) precision: used to specify the maximum number of characters, the number of digits after the decimal point of floating-point number, and the minimum number of digits output by integer for important printing of string;

[example 2] given the following code, find its output content.

#include <stdio.h>
int main()
{
    double x = 520.1314;
	int y = 520;
	
    printf("[%10.5lf]\n", x);
	printf("[%-10.5lf]\n", x);
	
	
	printf("[%10.8d]\n", y);
	printf("[%-10.8d]\n", y);

    return 0;
}

First look at the part after the decimal point, determine the actual length of the content to be output, then look at the minimum width of the field, and finally look at whether to align left or right:

[ 520.13140]
[520.13140 ]
[  00000520]
[00000520  ]

Then, let's look at the effect of combining different types of variables:

#include <stdio.h>
int main()
{
    char name[100] = "Zhou";
    int old = 18;
    double meters = 1.7;
    char spostfix = 's';
    printf("My name is %s, %d years old, %.2lf meter%c.\n", 
        name, old, meters, spostfix);
    return 0;
}
My name is Zhou, 18 years old, 1.70 meters.

Keywords: C

Added by unknown101 on Sun, 02 Jan 2022 03:31:34 +0200