C/C ++ misunderstanding 5: Char c = getchar ();

Source: Internet
Author: User
Tags types of functions

 

Many beginners are accustomed to using char variables to receive the return values of functions such as getchar, GETC, and fgetc. In fact, this is not correct, and it implies enoughFatal Error. The return value types of functions such as getchar areIntWhen these functionsRead ErrorOrAfter reading the file, Will returnEOF. EOF is a macro. The standard specifies that its value must beNegative constant of int type. Generally, the compiler defines EOF-1. This is where the problem occurs. Receiving the return values of functions such as getchar using the char type variable will lead to an error in EOF identification, or mistakenly mistake the good data for EOF or for good data. For example:

Int C;/* is correct. The Int type variable should be used to receive the return value of fgetc */
While (C = fgetc (FP ))! = EOF)
{
Putchar (C );
}

As shown in the preceding example, we often need to use a variable to receive the return values of functions such as fgetc, and then compare the variable with EOF to determine whether the file has been read. The above example is correct. Defining C as the int type ensures that it can correctly receive the EOF returned by fgetc, thus ensuring the correctness of this comparison. However,If C is defined as char, unexpected consequences will occur..

First, because the return values of functions such as fgetc are int type, when a value is assigned to a char type variableDowngrade, Resulting in data truncation. For example:

---------------------------------
| Decimal | int | char |
| -------- | -------------- | ------- |
| 10 | 00 00 00 0a | 0a |
|-1 | FF |
|-2 | FF Fe |
---------------------------------

Here, we assume that int and Char are 32-bit and 8-bit respectively. From the table above, from int to Char, three bytes of data are lost. When we want to compare the char type with the int type, the char type will automaticallyUpgradeInt type. After char is upgraded to intSigned CharOrUnsigned charBut it is different. Unfortunately, if we do not use signed or unsigned to modify char, we do not know whether char refers to unsigned char or signed Char, because this isDetermined by the compiler. However, whether char is signed or unsigned, the return values of functions such as fgetc received using char variables cannot be changed.Error. The only thing that can change is the consequence of this error. As we mentioned above, Char will be automatically upgraded to int when comparing the char and INT types. Let's take a look at the conversion of signed Char and unsigned char to int, their values are different:

---------------------------------------
| Char | unsigned | signed |
| ------- | --------------- | ------------- |
| 10 | 00 00 1A | 00 00 1A |
| FF | 00 00 FF |
| Fe | 00 00 00 Fe | FF Fe |
---------------------------------------

The table above shows that when char is unsigned, the value after converting it to Int IsPositive number. That is to say, if we define C as a char variable and the default char of the compiler is unsigned char, the following expression willAlways available.

(C = fgetc (FP ))! = EOF/* C is always a positive number, while the Standard stipulates that EOF is a negative number */

That is to say, the following cycle isEndless loop.

While (C = fgetc (FP ))! = EOF)
{
Putchar (C );
}

After reading this, some readers may say, "it's okay to clearly define C as signed Char !" Unfortunately, it is still wrong to define C as signed Char. Assume that the value of a byte read by fgetc and other functions isFFThe returned value is00 00 00 FF. After this value is assigned to C, the value of C is changed to ff. Then, to compare the value of C with EOF, it will be automatically upgraded to the value of int type, that isFF. As a result, the followingThe expression is invalid..

(C = fgetc (FP ))! = EOF/* read the character with the value FF, mistaken for EOF */

That is to say, the following cycle occurs inFile not readIn the caseExit early.

While (C = fgetc (FP ))! = EOF)
{
Putchar (C );
}

To sum up, it is wrong to use a char variable to receive the return values of functions such as fgetc.The Int type variables must be used to receive the return values of these functions.And then determine whether the received value is EOF. Only when the returned value is not EOF can we assign the value to the char variable.

Similarly, in C ++, char variables are used to receiveCin. Get ()The returned value is also incorrect. However, the char variable is treatedParametersPassCin. GetIt is correct. For example:

Char c = cin. Get (); // error for the same reason

Char C;
Cin. Get (c); // correct

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.