The use of getchar and putchar in the C language

  • 2020-05-30 20:53:21
  • OfStack

The use of getchar and putchar in the C language

getchar is accessed as a behavior unit.

When using getchar input, if the input of the first character to be valid character (at the end of the input is the file descriptor EOF, for key combination under Windows Ctrl + Z, Unix/Linux for key combination Ctrl + D), then only when the last one input for a newline character '\ n (also can be end of file descriptor EOF, EOF will be discussed later), getchar would stop execution, the whole program will be down. For example, the following program segment:


while((c = getchar()) != EOF){
  putchar(c);
}

Execute the program, enter: abc, and press enter. So it's going to execute puchar(c), and then it's going to print abc, and remember, there's a carriage return. You can then continue typing, and when you encounter a newline character again, the program outputs the input character of that line to the terminal.

As for getchar, I'm sure many beginners will ask, isn't getchar read in character units? So, since I typed the first character a, it must satisfy the while loop (c = getchar())! = EOF, then putchar(c) should be executed in the terminal output 1 character a. Yes, I used getchar with 1 in mind, but the program didn't execute as it should. Instead, it had to read a newline or EOF to output once.

One explanation for this problem is that when the master wrote C, there was no concept of terminal input at that time. All input was actually read according to the file, and the 1 in the file was generally based on the behavior unit. Therefore, only when a newline character is encountered will the program consider the input to be finished, and then take the rest of the program to execute. Also, if the input is accessed as a file, EOF (Enf Of File) is required to complete the input of a file. This is why EOF is used when getchar finishes the input and exits.


#include <stdio.h>


int main()
{
  int c;
  c = getchar();
  while (c != EOF)
  {
     putchar();
       c= getchar(); 
  }
  return 0;
}

The main reason why int is used to accept the getchar function is explained here.
A lot of times, we'll write two lines of code like this:


char c;
c = getchar();

There is a good chance that something will go wrong. Because the getchar function returns getchar () when Ctrl+D(Linux) (EOF), getchar () returns EOF in addition to the characters entered by the terminal, EOF is generally defined as -1 in the library. Therefore, in this case, the getchar function returns a negative value, and it is not correct to assign a negative value to a variable of type char.

Let's take a look at how to actually get and output a character.


#include "stdio.h" 
main() 
{ 
  char c,d,e,f; 
  printf("please input two characters:\n"); 
  c=getchar(); 
  putchar(c); 
  putchar('\n'); 
  d=getchar(); 
  putchar(d); 
  putchar('\n'); 
  e=getchar(); 
  putchar(e); 
  putchar('\n'); 
  f=getchar(); 
  putchar(f); 
  putchar('\n'); 
  printf("c= %c\n",c); 
  printf("d= %c\n",d); 
  printf("e= %c\n",e); 
  printf("f= %c\n",f); 
} 

After running, enter "12", press enter, and then "34", press enter.

The running environment is redhat gcc

Operation results:


please input two characters:
12 // The input 
1
2
// A blank line (\n)
// A blank line (\n)
34 // The input 
3
c= 1
d= 2
e=
//e Is the null line (\n)
f= 3
// A blank line (\n)

The following is a specific explanation:

The getchar function gets one character at a time from the buffer, and the putchar function outputs one character at a time.

I typed two characters, 12, and then I hit enter, and notice that I have three characters in the write cache, 1,2, enter.

There are 4 getchar () in the program, so c='1',d='2',e='\n'.

Run to f=getchar(); All 3 characters in the input cache are obtained by the first 3 getchar, so user input is required.

I typed in 34

So f='3', 4 and the return are not used.

That's the whole process.

If you have any questions, please leave a message or come to the site community to exchange discussion, thank you for reading, hope to help you, thank you for your support of the site!


Related articles: