DEV-C ++ Experiment with CHAR type

xiaoxiao2021-03-06  15

Kingwei 2005.3.11

Experimental environment: Windows XP, DEV-C 4.9.6.0 (GCC / MINGW32)

Value output about char

v_char = 0; for (i = 0; i <256; i ) {printf ("#% d / n", i); printf ("% d / n", v_char); / * No.1 * / Printf ("% u / n", v_char); / * No.2 * / Printf ("% d / n", (int) v_char); / * No.3 * / Printf ("% u / n", unsigned int) v_char); / * No.4 * / Printf ("% d / n", (unsigned char) v_char); / * No.5 * / Printf ("% u / n", (unsigned char) v_char ); / * No.6 * / printf ("/ n"); v_char ;}

If you define v_char as unsigned char, these 6 methods can work normally. However, if v_char is char or SIGNED Char, when V_CHAR is 0 ~ 127, the above 6 ways can also work normally. When v_char When the value is 128 ~ 255, only the 5th, 6 can work normally. The results of the first and third methods are the same: value output 128 -128129 -127 ... 254 -2255 -1

The results of the second and fourth methods are the same: value output 128 4294967168129 4294967169 ... 254 4294967294255 4294967295

This is because the 1-byte Signed Char is converted to 4 bytes of int, which is caused. So, if there is a case where the byte value is greater than 127 in the data to be processed, or Use unsigned char. If the program is involved in the program, you should also use the unsigned variable. The problem caused by the high-level symbol extension is often anti-fighting.

2. About Enter Removal

In general, pressing the Enter on the keyboard will generate two characters:

CR (Carriage Return: Enter) ASCII code = 13 = 0dh escape = '/ R'LF (line feed: wrap) ASCII code = 10 = 0ah escape =' / n '

CR is behind, LF is behind.

For example, if you open a notepad, press Enter, save it, check the properties, the file length is 2 bytes. This is the reason.

C, input and output has two modes: text mode and binary method

The description string when the corresponding file is opened (FOPEN / FREOPEN) is:

I / O mode input and output

Text mode "R" "W" binary mode "RB" "WB"

The default console I / O - stdin, stdout, uses text mode. Under text mode, press Enter, getchar () or scanf ("% C", & ch) is just the latter characters' / N '; use Getch () in conio.h, but read is the previous character' / r '. When output, printf ("/ n") or putchar (' / n ') is two Character: '/ r' and '/ n'; and Printf ("/ r") or putcha ('/ r') is only one '/ r', if the output to the file, can be opened with Notepad Go to a small square - not recognizable character 0DH.

Under binary mode, when entering the output, a character is a character. So let's try the following procedures:

NO.1 - OK!

#include int main () {Freopen ("Out.txt", "WB", stdout);

Putchar ('/ r'); putchar ('/ n'); return 0;}

No.2 - Two unrecognizable characters 0AH, 0DH

#include

INT main () {Freopen ("Out.txt", "WB", stdout);

Putchar ('/ n'); putchar ('/ r'); return 0;}

No.3 - an unrecognizable character 0DH

#include

INT main () {Freopen ("Out.txt", "WB", stdout);

Putchar ('/ r'); return 0;}

No.4 - an unrecognizable character 0ah

#include

INT main () {Freopen ("Out.txt", "WB", stdout);

Putchar ('/ n'); return 0;}

3. Others

DEV-C under:

1 CHAR type data length is 1 byte. 2 Char default is a symbolic type, named Signed Char. But not all compilers are processed so .3 Value range: Signed Char 0 ~ 255 Unsigned CHAR-128 ~ 1274 Output Using format% c, it is output according to character mode; or use% D,% U,% X,% O, according to the integer mode output .5 When entering,% c should be used in characters; if an integer mode is used DEV-C will give a warning and do not recommend this.

转载请注明原文地址:https://www.9cbs.com/read-48318.html

New Post(0)