Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

C speech character array and character pointer

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Constant and symbolic constant

In the process of running the program, the amount whose value cannot be changed is called a constant. Constants are divided into different types, including integer constants such as 1,2,3,100; floating point constants 3.14,0.56,-4.8; character constants a, b, 0; string constants "a", "abc", "1234", "1234abcd" and so on.

Careful students will find that integers and floating-point constants are numbers that we write directly, while character-type constants use single quotes to represent a character and double quotes to represent a string, especially to note that an and "a" are different. We'll introduce you to this later.

Constants are generally expressed in two ways:

Direct constant: a constant that is expressed directly as a value is called a direct constant. Most of the above examples are direct constants and are written directly.

Symbolic constant: a constant named by an identifier is called a symbolic constant, which is another name for the following direct constant. The use of symbolic constants is easy to understand, improve the readability of the program, and more importantly, facilitate the follow-up protection of the program. We all use capital letters and underscores to name symbol constants in habits.

For example, we can name 3.14 PI (that is, π). For example, in our serial port program last class, we used a baud rate of 9600. If we use a symbolic constant to stop the early declaration, then if we want to correct it to other speeds, we do not have to find 9600 correction in the program. It is OK to amend the declaration directly. Two methods are illustrated by examples. Declare with const. For example, we say a symbolic constant BAUD at the beginning of the program.

The definition is as follows:

Const type symbol constant name = constant value

Such as

Const unsigned int BAUD = 9600; / * notice that there is a semicolon at the beginning * /

We can directly change 9600 into BAUD in the program, so that if we want to change the baud rate, we can just change this value at the end of the program. It is done with the pre-disposal decree # define, and the pre-disposal decree gives us a look at # define first.

The definition is as follows:

# define symbol constant name constant value

Such as

# define BAUD 9600 / * Note that there is no semicolon at the beginning * /

If it is said that in the future, only need to present BAUD in the program, it means to completely replace the following number 9600.

I wonder if everyone can remember that when we defined the digital tube truth table, we used the word code.

Unsigned char code LedChar [] = {/ / display character conversion table 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E}

We said in advance that after adding code, the data of this truth table can only be used and cannot be changed, if we directly write LedChar [0] = 1; that would be wrong. In practice, the word code is unique to 51 single-chip microcomputer. if it is other types of single-chip microcomputer, we only need to write it as const unsigned char LedChar [] = {} and take the initiative to store it in FLASH, while 51 single-chip microcomputer only uses const instead of code, this array will be kept in RAM, not in FLAHS. In view of this, under the system of 51, const has become less important, and its influence has been replaced by code. Everyone here knows what happened.

Let's further clarify the various types of constants.

Integer constants and floating-point constants have nothing to say, before we are very familiar with the use of integer directly write numbers is decimal such as 128, the front 0x sweep performance is hexadecimal 0x80, floating-point directly write data with decimal points on it.

A character-type constant is a single character enclosed by a pair of single quotes. It is divided into two ways, one is popular characters, the other is literal characters.

Popular characters are those invisible characters that we can see directly in writing, such as the Arabic numeral 0909, the English character Ayogz, and punctuation marks. Most of them are characters in the ASCII code table, and they all occupy a byte of space in the single-chip microcomputer, and their value is the corresponding ASCII code value. For example, the value of an is 97, the value of An is 65, and the value of 0 is 48. If you define a variable unsigned char a = a, then the value of an is 97.

In addition to the above characters, there are some special characters, some of which are tangible, such as carriage returns and line feeds, and some characters like\ "which used to be of special use. imagine what the compiler would say if you wrote''. In view of these special symbols, in order to allow them to enter our program code normally, C language regulates the literal characters, which is a specific sequence of characters ending with a backslash (\) to represent these special characters. for example, we use\ n to represent line breaks.

Table 12-2 commonly used literal characters and connotations

Character mode\ nWrap\ t horizontal jump (equivalent to Tab)\ v vertical jump\ b backspace\ r cursor moved to the beginning of the line\\ backslash character\\ single quotation mark character\ "double quotation mark character\ f feed\ 0 null value

There is no need for everyone to remember the form. If you need it, you can check it in the past.

For example, "a", "1234", "welcome to www.kingst.org" and so on are mostly string constants. The string constant stores the ASCII code values of the characters in the string one by one in order in memory, and special note that at first there is a character\ 0, and the ASCII code value of the\ 0 character is 0, which is the mark at the end of the string. When writing the string, this\ 0 is hidden, we can't see it, but there is practice. So "a" has one more\ 0 than a, "a" takes up two bytes, and an only takes one byte.

There is another place to note, that is, the space in the string is also a character, such as "welcome to www.kingst.org" takes up a total of 26 bytes of space. There are 21 letters, 2., 2 (space characters) and one\ 0.

Character and string array instance

In order to compare the differences among strings, character arrays, and constant arrays, we have written a complex demonstration program that defines four array identifiers as follows:

Unsigned char array1 [] = "1-Hello!\ r\ n"; unsigned char array2 [] = {'2mm,' -', 'Haugh,' eBay, 'lump,' lump, 'oasis,'!,'\ rust,'\ n'}; unsigned char array3 [] = {51,45,72,101,108,108,111,33,13, 10}; unsigned char array4 [] = "4-Hello!\ r\ n"

Under the serial port debugging assistant, send hexadecimal 1, 2, 3, 4, and use character display to identify which of the four arrays are sent to the computer. We just identified the status at the beginning, and there was no difference in the rest. Everyone can compare the consequences.

In addition, I would like to clarify that array 1 and array 4, array 1 we are good strings, while array 4 we only send characters in the array, not finished symbols. There is no difference in character display by serial debugging assistant, but if everyone changes to hexadecimal display, everyone will find that array 1 has an extra byte\ 0 ASCII value of 00 more than array 4.

# include bit cmdArrived = 0; / Royal decree arrival mark, that is, the edict unsigned char cmdIndex = 0 is accepted to the upper computer; / / the edict index, that is, the array number unsigned char cntTxd = 0 agreed with the upper computer; / / the serial port sending counter unsigned char * ptrTxd; / / serial port sending pointer unsigned char array1 [] = "1-Hello!\ r\ n" Unsigned char array2 [] = {'2girls,' -', 'hacks,' estranges, 'lags,' oaks,'!,'\ rranks,'\ n'}; unsigned char array3 [] = {51,45,72,101,108,108,111,33,13,10}; unsigned char array4 [] = "4-Hello!\ r\ n"; void ConfigUART (unsigned int baud); void main () {EA = 1 / / Open total suffix ConfigUART (9600); / / set the equipment configuration baud rate to 9600 while (1) {if (cmdArrived) {cmdArrived = 0; switch (cmdIndex) {case 1: ptrTxd = array1; / / the first address of array 1 is assigned to the sending pointer cntTxd = sizeof (array1); / / the length of array 1 is assigned to the sending counter TI = 1; / / the manual method starts sending the infix, and disposes the data to send break; case 2: ptrTxd = array2 CntTxd = sizeof (array2); TI = 1; break; case 3: ptrTxd = array3; cntTxd = sizeof (array3); TI = 1; break; case 4: ptrTxd = array4; cntTxd = sizeof (array4)-1; / / string practical length is array length minus 1 TI = 1; break; default: break;} / * serial port setting equipment function, baud- communication baud rate * / void ConfigUART (unsigned int baud) {SCON = 0x50 / / set the serial port of equipment installation to form 1 TMOD & = 0x0F; / / set the control bit TMOD of zero T1 to be 0x20; / / set equipment configuration T1 to form 2 TH1 = 256-(11059200x12x32) / baud; / / calculate T1 overload value TL1 = TH1; / / initial value equals overload value ET1 = 0; / / stop T1 infix ES = 1; / / enable serial port suffix TR1 = 1 / / start T1} / * UART suffix service function * / void InterruptUART () interrupt 4 {if (RI) {/ / accept to byte RI = 0; / / clear the suffix mark bit cmdIndex = SBUF; / / the accepted data is stored in the edict index cmdArrived = 1; / / set the imperial decree arrival mark} if (TI) {/ / byte end of transmission TI = 0 / / clear the marked bit if (cntTxd > 0) {/ / when data is to be sent, continue to send subsequent bytes SBUF = * ptrTxd; / / retrieve the data pointed to by the pointer cntTxd--; / / send counter increment ptrTxd++; / / send pointer increment}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report