Why is the return type of getchar of in C detailed

  • 2020-06-12 10:03:20
  • OfStack

preface

There is an important library function in C language, getchar(), which can obtain the 1-character ASCII code value from the terminal. When a character is entered at a terminal, instead of being returned when a character is entered, there is an important library function in the C language, getchar(), which can obtain the value of the ASCII code of a character from the terminal before a carriage return is encountered. In terminal input character is not input a character will return, but before met carriage returns, all input characters will buffer in the keyboard buffer, until the carriage returns one sex to assign all characters according to the sequence in turn to the corresponding variables, here need to pay attention to the final 1 character '\ n', the character will be assigned to a corresponding variable (of course this is used to receive the character variable defined you number 1 more than the visible characters you entered can only).

Lately, however, I've been rereading K & R's C Programming Language was confused about the return value of the built-in function getchar(). In the literal sense, the return type to the function should be of type char, but in the example, the return is assigned to an int variable, as follows:


#include <stdio.h>
 
/*  Copy the input to the output  */ 
int main(void)
{
 int c;
 while ((c = getchar()) != EOF){
 putchar(c);
 }
 return 0;
}

The book gives the following explanation:

[

We use the int type here for some potentially important reasons.

]

Here the author did not give a clear explanation for the important reasons. I tried to modify int to char, but found that the program could still compile and execute normally, which made me more confused! After 1 - page search, the answers found are as follows.

1. getchar() returns the input terminator EOF(end of file) in addition to the normal characters. The prototype of this function is as follows:


int getchar(void)
{
 static char buf[BUFSIZ];
 static char *bb = buf;
 static int n = 0;
 if(n == 0)
 {
  n = read(0, buf, BUFSIZ);
  bb = buf;
 }
 return(--n >= 0)?(unsigned char) *bb++ : EOF;
}

2. EOF is usually in < stdio.h > In the file is defined as -1:


#define BUFSIZ 512
#define _NFILE _NSTREAM_
#define _NSTREAM_ 512
#define _IOB_ENTRIES 20
#define EOF (-1)

3. The range of values that the various data types can represent is determined by the compiler. The char type is defined in a range of 0 to 255 in some compilers and -128 to 127 in others. An error occurs when char is used to receive the return value of getchar() when the range defined in the compiler is 0 to 255. The scope of the specific definition of the data type is available in < limits.h > Found in the file:


#define CHAR_BIT 8
#define SCHAR_MIN (-128)
#define SCHAR_MAX 127
#define UCHAR_MAX 0xff
 
#define CHAR_MIN SCHAR_MIN
#define CHAR_MAX SCHAR_MAX

4. After changing int to char, it can compile normally on my computer. Since the range defined in my compiler is -128~127, it will be converted to char implicitly when it is received by char.

5. Even if the compiler has a total defined range of -128 to 127, errors are possible. Although common characters end at 127 bits. But the ASCII table is allocated to time 256. 128-256 are extended characters, such as common euro symbols, etc.

To sum up: getchar() returns a larger range of int type to receive, so that the program is more robust.

conclusion


Related articles: