[Elementary C Language] Learn to use the library functions getchar and putchar

Directory

One, getchar function

1. Function:

2. Recognize functions

3. use

Two, putchar function

1. Function

2. Definition

3. use

3. The use of getchar and putchar

Fourth, the application of getchar

1. getchar function

1. Function:

The function of this function is to receive a character, and then convert the character to the corresponding ASCII value

2. Knowing functions
int getchar(void)//getchar function prototype

Parameter type: the void in the brackets means that no parameters are accepted, that is, there is no need to write anything in the brackets

Function function: The function of this function is to receive a character, and then convert the character to the corresponding ASCII value

Return value type: After accepting a character, the character will be converted into the corresponding ASCII code value, so the ASCII code value of the return value, so the return value type is integer (int). If the getchar function fails to accept characters or encounters the end of the file, it will return a value of EOF, and the value of EOF is -1 by default

3. Use

We are now going to input a capital A, and then output A and the corresponding ASCII code value

#include<stdio.h>
int main()
{
//int ch=getchar();//This definition is also possible
int ch = 0;
ch = getchar();//The first step, getchar will accept the value you enter on the keyboard
printf("%c\
", ch);//The second step, print the character directly
printf("%d\
",ch);//Print the ASCII code value corresponding to the character
return 0;
}

Next, let’s compare two different ways of receiving characters

The same point: both can be used to input (receive) a character.

The difference: the getchar function can only be used to receive characters, and receiving characters is just a form of the scanf function.

Advantages: the getchar function does not need a variable to receive, (when you need to print the character) you can also use a variable to receive its return value; however, the sacnf function must have a variable to receive, whether you want it or not Print.

2. putchar function

1. Function

output (print out) a character on the screen

2. Definition
int putcahr(int charatcer)//putchar function prototype

The parameter type of the function: the parameter required here is an integer character, which is the value that needs to be written in the brackets.

The role of the function: print the parameters on the screen, putchar is essentially an output function

Note: Only one character can be output at a time

Return value type: the return value is the ASCII code of the output character (that is, an integer), and if the output fails, it returns EOF (constant, generally defined as -1).

3. Use

There are two ways to output characters: the first is to use variables to output, and the second is to directly output characters

//The first output method
#include <stdio.h>
int main()
{
int ch1 = getchar();
    int ch2 = getchar();
    putchar(ch1);//store the character in the variable
    putcahr(ch2);//Rely on variables to output characters
return 0;
}
//The second output method
#include <stdio.h>
int main()
{
putchar('A');//No need for variables, directly output characters
putchar('B');
return 0;
}

3. The use of getchar and putchar

It is generally used in conjunction with the getchar function.

We want to output on the screen: AB

#include<stdio.h>
int main()
{
int ch1 = getchar();
int ch2 = getchar();//accept character
putchar(ch1);
putchar(ch2);//print characters
return 0;
}

4. Application of getchar

Apply: Clear Buffer

Topic: Use code to realize the scene of password input and confirmation

#include<stdio.h>
int main()
{
char arr[20] = {0};
printf("Please enter the password\
");
scanf("%s",arr);//Here we first enter the password from the keyboard, and then press Enter

printf("Please confirm (Y/N):");//Here we need to confirm the password we entered again
int ch = getchar();//getchar is responsible for accepting Y or N
if ('Y' == ch)
printf("confirmed successfully\
");
else
printf("Confirmation failed\
");

return 0;
}

Let’s run the above code

Why? Come on slowly after listening! The reason is that getchar receives the carriage return character, and the carriage return key is generally equivalent to \
, so at this time the data stored in ch is ‘\
‘, so ‘Y’!=ch, it directly shows that the confirmation failed.

Let’s understand one thing first: the scanf function and getchar function receive data through the buffer, and the data on the buffer is input by the keyboard. Let’s look at a picture.

What do you mean? Let me demonstrate with the above code:

Step 1:Run the program

Because the program has just started running, we have not input data from the keyboard, so there is nothing on the buffer, it is empty, so the cursor is flashing, waiting for our input.

Step 2: Enter the data (password) and press Enter to let the scanf function receive the password

Here the Enter key is equivalent to \
. So what the sacnf function takes away from the buffer is abcdf, and there is \
left on the buffer. When the getchar function receives characters, it can only take away \
, so why does the screen display confirmation failure without entering Y or N?

Remedy: use another getchar function to receive\

Improve code:

#include<stdio.h>
int main()
{
char arr[20] = {0};
printf("Please enter the password\
");
scanf("%s", &arr);
getchar();//This getchar is used to receive \0
printf("Please confirm (Y/N):");
int ch = getchar();
if ('Y' == ch)
printf("confirmed successfully\
");
else
printf("Confirmation failed\
");

return 0;
}

Let’s run and implement it:

We can see that the expected results can already be achieved.

But what! There will still be bugs, because when the scanf function reads a string, it will not read it when it encounters a space. When we run it, we can see it at a glance

Run result:

Because a getchar can only handle one space, the following content cannot be processed. Then why not add a few more getchars? As a result of this, how do you know how many spaces the user will enter? So we need to use a loop to transform

Code of the second transformation: complete code display

#include<stdio.h>
int main()
{
char arr[20] = {0};
printf("Please enter the password\
");
scanf("%s", &arr);
int ch = 0;
while ((ch=getchar())!='\
')//New improved code, use loop to receive characters
{ //The loop will not end until the reception is complete\

;//We loop the getchar function, so the loop body does not need to execute the task statement, only an empty statement is required
}
printf("Please confirm (Y/N):");
ch = getchar();
if ('Y' == ch)
printf("confirmed successfully\
");
else
printf("Confirmation failed\
");

return 0;
}

After the two transformations, no matter how you enter the password, there will be no problems, and you can steadily let you enter Y or N to confirm the password

The key code to clear the buffer:

int ch = 0;
while ((ch=getchar())!='\
')
{
;
}

5. Summary

1.getchar is a function to receive characters, the ASCII value of the character is returned if the reception is successful, and EOF (-1) is returned if it fails

2.putchar is a function to print a character, it will print the character on the screen

3. Reasonable use of the getcharh function can achieve the effect of clearing the buffer

This section ends here! More complete and detailed content will be added later!

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge C skill treefunction and program structuredeclaration and definition of function