How to handle Chinese characters in the C program

zhaozj2021-02-11  229

People who handle Chinese characters in the C program may sometimes encounter such a problem: how to store Chinese characters with variables and these variables. At present, many C language reference books are not involved in this problem. The program is mostly handled in English variables and English strings, which involves the Chinese characters, mostly in the PrintF statement to output prompt information or results, such as: Printf ("please Enter a, b value: / n "); Printf (" Output power is% s kW ./n" ,power); Considering that there is a considerable number of people in learning and applying C language, I will introduce the reader here. The author has explored experience in this area. Storage Chinese characters should be used in the number of characters, this is affirmative, the key issue is that Chinese characters take a few bytes on your computer system. Most people will think that a Chinese character is definitely accounting for two bytes, in fact. The Chinese characters share a few bytes, which is different from the system, but also on the software environment, such as the Chinese characters in Visual Basic accounts for one byte. You can test it with a strlen () function on your computer, such as Printf ("% D", Strlen ("Computer")); if the output is 6, each Chinese character accounts for two bytes; if you output 12, Each Chinese is four bytes. Most systems are two bytes per Chinese, that is, the above statement output value is 6. It should be noted that a full-corner character (including punctuation) with Chinese characters occupying the same byte. This article assumes that each Chinese character accounts for two bytes. This way you can store Chinese characters with a character array, but don't forget, because the string in the C language is the end tag with '/ 0', the system will automatically add this tag, and use the strlen () function test The return value does not include this '/ 0', so the length should be minimized for the actual string long and add 1, such as: static char name [7] = "Sun Wukong"; / * Array length is 7 Description Up to 3 Chinese characters * / If the length of the array is defined as 6, the system does not report at the time of compile, but when the program is running, it is sometimes inexplicably repeated or more output or more output Chinese characters. If this happens, if this happens, you should first Consider whether there is a problem with the length of the array. Of course, initialization can omit an array length while defining, such as: static char array [] = {"China Computer Software Professional Technology Level Examination"}; Similarly, the two-dimensional array can be stored in this two-dimensional array can store 10 people: char Member [10] [8]; / * 10 elements, you can put up to 3 Chinese characters in each element (not 4!) * / because the two-dimensional array member [10] [8] can be seen as a special one Array MEMBER [0], Member [1], ..., Member [9], these one-dimensional arrays respectively indicate each line, and a line represents a person name, so if you want to operate each name, just write into Member [0], MEMBER [1], ... and do not write into Member [0] [8], Member [1] [8], .... By the way, enter Chinese characters in the compilation window in the C compilation window. If the reader uses a DOS operating system, you need to install CCDOS or UCDOS; if it is a Win95 / 98 operating system, run the following batch file in the DOS command window, without having to install Chinese DOS system: C: / Windows> PDOS95 Starts the WIN95 Chinese Input Method with Ctrl Spacebar in the CTRL Space button. In addition, since the C program is DOS, the compilation C program is best in full screen state, otherwise it is prone to problems.

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

New Post(0)