C language - data input and output

Foreword:

There is no special input and output statement in C language, and the input and output of C language is realized by library functions.

1. Format input and output functions

1. Format output function printf()

Format:
printf ("format control string", output list);
function:
output to the output device (usually a display) according to the format specified by the format control string, and output the value of each output item in the list.
Such as:
insert image description here
format control: the string enclosed by double quotes is used to specify the output format.
— Ordinary characters: Characters that are output as-is.
—Format description: used to specify the output format of the output data.
Form: %[modifier] format character
Output list: The list of data to be output, separated by commas, can be any legal expression.
insert image description here
insert image description here

Supplement:
insert image description here
Description:
1) d format character.
Function: Output a signed decimal integer (positive numbers do not output signs)
Format:
insert image description here
Example:

#include <stdio.h>
int main()
{
    
    
    int x = 123;
    printf("|%d|\n", x);
    printf("|%5d|\n", x);
    printf("|%-5d|\n", x);
    printf("|%+5d|%+5d|\n", x,-x);
    printf("|%-5d|%-5d|\n", x,-x);
    printf("|%2d|\n", x);
    return 0;
}

Test:
insert image description here
2) o, x and u format characters.
Function: output an unsigned octal, hexadecimal or decimal unsigned integer.
3) c format character.
Function: used to output a character.
Note:
An integer, if it is between 0~~127, can also be output in character form, and the system will convert the integer into the corresponding ASCII code character.
4) s format character.
Function: used to output a string.
Format:
insert image description here
5) f format character.
Function: Output single and double precision real numbers in decimal form.
Format:
insert image description here
6) e format character.
Function: output real numbers in the form of standardized exponents. (There must be one and only one non-zero number before the decimal point)
Format:
insert image description here
Example:

#include <stdio.h>
int main()
{
    
    
    char ch = 'a';
    float x = 123.456f;
    double y = 321.564321;
    float f = 123.456f;
    printf("%3c\n",ch);
    printf("%8.2s\n","computer");
    printf("%f\n",x);
    printf("%.0lf\n",y);
    printf("%10.2e\n", f);
return 0;
}

Test:
insert image description here
Note :
The output table columns can be variables, constants, array elements, function references, and expressions with operators.
Example: printf("%f%d%d%f", 3.14, a+b, x, cos(y)); each
output item and each format specification in the output list must be in the type from left to right Match one by one.
If the real data is output with %d, the result is an error, and the integer data is output with %f, the result is zero.
When there are more format characters than output items, the extra format characters output an indefinite value; when there are fewer format characters than output items, the extra output items are not output.
Evaluation order of output table columns: vc is evaluated from right to left, and output is still from left to right.
You can add 0 in front of the field width of the format character to make up 0.
Example: printf(“|%-05d|\n”,12); //output |12 |
printf(“|%05d|\n”, 12); //Output|00012|
Except X, E, G format characters, other format characters must use lowercase letters.

2. Format input function scanf()

Format:
scanf ("format control", address list);
function:
according to the format specified by the format control, input data from the specified input device (usually a keyboard) to the specified variable.
insert image description here
Format control:
- Ordinary characters: Characters to be entered as they are.
— Format description: used to specify the input format of the input data.
Form: %[modifier] format character.
Address list: give the address of a variable.
- The address is obtained by the '&' operation.
—'&' is the address operator, which gets the address of the variable in memory, unary operator, and right associativity.
insert image description here

insert image description here
Use and attention of format input:
1) The delimiter used when inputting data should be consistent with the delimiter in the format control.
When there are no ordinary characters in the format control, spaces, tab keys, and carriage return keys can be used as separators for inputting numeric data.
Such as:
int a, b; float f, e;
scanf("%d%d",&a,&b);//Enter 23 spaces, 456 carriage returns
2) The precision cannot be specified when inputting data, but the width can be specified.
Example: scanf("%7.2f", &a);//Modifier error
3) The input format character with * means that the data is not assigned.
Example: scanf ("%d%*d%d", &a, &c);
input: 12 spaces, 34 spaces, 567 carriage return; output a=12, c=567, 34 is skipped
4) unsigned numbers can be %d , %o, %x format input.
5) When using the c format character to input characters, spaces and escape characters are valid characters. (——> carriage return)
scanf ("%c%c%c", &a, &b, &c);
if you type: A space B space C carriage return //then a=A, b=space, c= BIf
you type: AEnter BEnter//then a=A, b=Enter, c=B
6) When %d, %f, %s, etc. read numbers, the leading empty characters are skipped.
In the following cases, it can be considered that the data input is over:
meet a space, carriage return, tab key;
meet the end of the width;
non-numeric characters. (such as x)
7) Double must use %lf (or %le)
float must use %f (or %e)
Example:

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

insert image description here
Note:
There can be no spaces between the format symbols %f%c, and they must be entered directly.

2. Character input and output functions

1. Character output function putchar()

Format:
putchar(c);
Function:
output a character to the terminal.
Note:
c is a parameter, which can be a character variable, an integer variable, a character constant, an expression or an escape character, but it can only be a single character instead of a string.

2. Character input function getchar()

Format:
getchar();
Function:
read a character from the keyboard.
char ch=getchar();
Description:
Only one character can be read; this character can be assigned to a character variable, an integer variable or as part of an expression.

3. String input and output functions

1. String output function puts()

Format:
puts (character array name); puts (string);
function:
output the value of the character array, when '\0' is encountered, the output ends.
Explanation:
—puts() can only output one character string at a time, and automatically wrap (\n) after the character string is output, and escape characters can be output.

2. String input function gets()

Format:
gets (character array name);
function: assign the input string to the character array. When inputting, the first carriage return key (\n) is encountered to end the output, which can end spaces and tabs.
Explanation:
The function gets() can only input one string at a time.

Fourth, the difference between functions

1. The difference between the gets() function and the scanf() function

——The gets() function is the same as the scanf() function. After reading a string, the system automatically adds a string end mark '\0' after the string.
——gets() ends the input with Enter, but spaces can be accepted, and the final carriage return will be discarded! And gets() can read multiple characters.
Differences:
scanf cannot accept spaces, tabs, and carriage returns;
gets can accept spaces, tabs, and carriage returns;
scanf: when encountering a carriage return, the space and tab keys will automatically add '\' to the end of the string 0', but the carriage return, space and tab keys will still remain in the input buffer.
gets: Accept all characters entered before the Enter key, and replace '\n' with '\0'. The Enter key will not stay in the input buffer

#include <stdio.h>
int main()
{
    
    
    char str1[20], str2[20];
    gets(str1);
    scanf("%s", str2);
    printf("str1:%s\n", str1);
    printf("str2:%s\n", str2);
    return 0;
}

test:
insert image description here

2. The difference between the puts() function and the printf() function

——The puts() function can only output one string at a time, and automatically wrap (\n) after outputting the string (automatically wrap '\0' into '\n'), and can output escape characters.
——The printf() function can output multiple strings at the same time, and can flexibly control whether to wrap lines.
example:

#include <stdio.h>
int main()
{
    
    
    char str1[] = "student", str2[] = "teacher";
    puts(str1);//自动\n
    puts(str2);
    printf("%s", str1);
    printf("%s\n%s", str1,str2);
    return 0;
}

test:
insert image description here

3. The difference between the getchar() function and the scanf() function

- Read the next character from standard input (keyboard). The return value is the ASCII code entered by the user, and -1 is returned when an error occurs. The characters entered by the user are stored in the keyboard buffer until the Enter key is pressed to extract the characters from the buffer. Reads the first character from the stream.
The difference:
when scanf encounters a carriage return (enter) or a space, TAB will end an input and will not receive a space.
The getchar function only ends input with carriage return and Enter, and accepts space characters.
scanf will not discard the last carriage return after an input (that is, the carriage return will remain in the buffer)
getchar carriage return is used as a sign of the end, so '\n' is also stored in the cache when the carriage return is hit
scanf() skips spaces, carriage returns (Enter), tabs, and newlines when reading numbers!
The getchar function can only input characters, and the characters are sequentially extracted from the buffer when the Enter key is encountered during input.

Code 1:

#include <stdio.h>
int main()
{
    
    
    char c1=0, c2=0;
    c1=getchar();//\n留在缓冲区
    c2 =getchar();//遇到\n什么都没输出
    printf("%c,%c", c1,c2);
    return 0;
}

insert image description here

Code 2:

#include <stdio.h>
int main()
{
    
    
    char c1=0, c2=0;
    c1=getchar();
    getchar();
    c2 =getchar();
    printf("%c,%c", c1,c2);
    return 0;
}

test:
insert image description here

Improvement:
There are several situations where you need to use getchar() to eat the carriage return:

  1. When there is scanf in the front and scanf() is also used in the back, an empty getchar() should be used in the middle to eat carriage return;
  2. There is scanf in the front, and when you use ch=getchar() to receive characters later, you need to use an empty getchar() in the middle to eat carriage return;
  3. An empty getchar() is needed between the two ch=getchar() to eat carriage return.

4. The difference between the putchar() function and the printf() function

-putchar(a) outputs a character to the terminal. Where a can be a character surrounded by single quotes [putchar('e')], can be a decimal integer between 0~127 (including 0 and 127) [putchar(23)], or It is a character variable defined in advance with char and can only output a single character.
——printf() formatted output function, which is used to output information to the standard output device in a specified format, and can output various data types, and can also have parameters.
The difference:
putchar will have a return value, and the return value is the unsigned int value converted to the parameter value in (). If an error occurs or the end of the file, putchar() returns EOF, while the printf() function returns the length of the string.
test:
insert image description here

Guess you like

Origin blog.csdn.net/plj521/article/details/131316690