Mad Man C-representing constants with symbolic Constants

Source: Internet
Author: User

/*
Obtain and output the ASCII code numbers corresponding to the numbers in integer 123.
*/

# Include <stdio. h>
# Include <stdlib. h>

# Define ZHENGSHU_3 123 // The constant in the problem should generally be represented by a symbolic constant
# Define GE 1
# Define SHI 10
# Define bai100
# Define QIAN 1000

Int main (void)
{

Printf ("% c's ASCII code value is % d ",
ZHENGSHU_3 % QIAN/BAI + 0, ZHENGSHU_3 % QIAN/BAI + 0 );

Printf ("% c's ASCII code value is % d ",
ZHENGSHU_3 % BAI/SHI + 0, ZHENGSHU_3 % BAI/SHI + 0 );

Printf ("% c's ASCII code value is % d ",
ZHENGSHU_3 % SHI/GE + 0, ZHENGSHU_3 % SHI/GE + 0 );

System ("PAUSE ");
Return 0;
}
First, constants in the problem should generally be written as symbolic constants. This is a common sense for compiling C Programs.
Reason:
Get a meaningful name for the constant, and the code is more readable. Code readability is an important indicator of code quality.
Reduces the possibility of errors. Embedding constants in code is highly likely to cause errors, and it is even hard to find out. For example, if 123 is written to 12 by mistake, because 123 and 12 are both valid int type constants, no error will be reported during compilation. However, the probability of incorrect identifier of a symbolic constant is much lower, especially when the identifier has a "literal meaning", and even if it is wrong, it is more likely to be noticed, the compiler will find that there are no identifiers defined or described in the Code.
The program is easy to modify and expand. This convenience comes from the separation of data and code. Data and code separation is an important principle in modern programming. Return to God, and return to the devil. The weaker the coupling between data and code, the better the code quality. In industrial design, people have long understood this truth, but in the software industry, people have paid a lot of money to understand this truth.

It helps cultivate abstract thinking abilities. It only keeps an eye on 123 writing code, just as a pupil only performs arithmetic instead of algebra. Abstract thinking capabilities are required in programming. Therefore, although this is only a specific problem for integer 123, we should also learn to use abstract methods to describe it and solve it.
Second, algorithms:
There are two main algorithms: one is to find the numbers of an integer, and the other is to find the ASCII code of a number.
You can use the remainder and Division operations to obtain the numbers of an integer. For example, you can use 123 123% to calculate the ten-digit number.
Obviously, a hundred-digit number of 123 can be 123/100 rather than 123% 1000/100. Similarly, a single-digit number of 123 can be 123% 10 rather than 123% 10/1. However, unifying algorithms for numbers is a particularly important training for learning to write programs. Otherwise, it will cause great difficulties when learning to write loop statements and functions. In addition, the Code is also more neat and elegant by writing algorithms in the form of consistent search for numbers.
The code can also be written using variables, and only a single digit can be obtained at a time.
# Include <stdio. h>
# Include <stdlib. h>

# Define ZHENGSHU_3 123
# Define SHI 10

Int main (void)
{
Int temp = ZHENGSHU_3;

Printf ("% c's ASCII code value is % d", temp % SHI + 0, temp % SHI + 0 );
Temp/= SHI;

Printf ("% c's ASCII code value is % d", temp % SHI + 0, temp % SHI + 0 );
Temp/= SHI;

Printf ("% c's ASCII code value is % d", temp % SHI + 0, temp % SHI + 0 );

System ("PAUSE ");
Return 0;
}
The C language requires that the character set used in the runtime environment (whether or not ASCII code is used, the encoding of the 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 characters is continuous. Because the encoding of the 0 character can be written as 0 in the code, therefore, you only need to obtain the value of each digit and add 0 to get the character encoding of the digit, regardless of whether the runtime environment uses ASCII code.
Third, deficiency: if a problem is raised from a more abstract perspective, for example, a three-digit N-base INTEGER (2 <= N <= 10) is given ), ask for the corresponding ASCII code numbers and output them, and you will find that names such as GE, SHI, BAI, and QIAN are awkward-they are too specific and only apply to decimal. From a general perspective, they should be the weights of all BITs in various hexadecimal systems.

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.