A few interesting C + + topics

Source: Internet
Author: User
Tags integer numbers


Example 1: It is known that a computer machine word length 16 bits, try to indicate the following C statement execution results.

int i=40000;printf ("%d", I);

Analysis: First we review the data types in C + +:

Data type Symbols The corresponding machine word length
int, unsigned int Integer numbers (with, without symbols)
Short, unsigned short Integer half word (with, unsigned)
Long, unsigned long Integer double word (with, unsigned)
Long Long Integer four words
Char, unsigned char Integer number section (with, without symbols)
Float Floating point numbers
Double Floating-point Double word
Long double Floating point four characters (double word)

The data type is closely related to the machine word length, the type of int defined in the topic, so the storage length is a word size, that is, 16 bits, we first convert the I transformation to 16 in number: 40000=0X9C40, as a signed integer (the sign bit of one number is about to occupy) and the sign bit is 1 (that is, negative)

So the compilation system will calculate it as a complement (i.e. negation plus 1, get -0x63c0), so the output after execution is 25536

Example 2: It is known that the initial data declaration within a program is: char* p= "ACD"; when the program runs to a point where the pointer points to the character ' C ' in the string, try to determine which of the following four C statements correctly obtains the character ' a ' in the string.

(A) *p–;         (B) *–p;         (C) *p-1; (D) * (P –)

Analysis: The address value is the same length, the same type of data, so this calculation will involve a multi-transformation of the priority policy (i.e. address change and address of the first access to the policy). When the compound takes place the address transformation and the inside operation, it is determined by the relative position of the respective operation symbol. It is therefore easy to judge (B) to be the correct statement. The key to identifying the correct solution to this problem is to correctly understand the order rules for indirect addressing and multivariate access.

Example 3: Try to explain the C statement "I+++J;" Calculation strategy.

Analysis: Compile scan from left to right, so judge the relation between operation object and operation should form "i++" and "J" two parts, and "i++" is a unary operation, so calculate first. If the compiler's scanning direction reverses, the first calculation of ++j is also correct. So there are different results for different compilers.

When you use different data types and data lengths, you should consider each other's (compile) transformation rules;

Example 4: Try to determine the following C program execution results (16-bit word-length machine):

Long s,l=5;                            unsigned int u=4;                      Char c=-3;/*0xfd*/                 s=l+u*c;/* expected value is -7*/printf ("%ld", s);  

First calculate u*c, in which C is converted to an unsigned integer (that is, 0XFFFD), and then complete the u*c calculation (multiply by 4 to the left two bits) and obtain intermediate results 0xfff4. Then, to calculate L and the intermediate result, the intermediate result of the unsigned integer is converted to the same signed double word as L (that is, 0X0000FFF4), then s=65529 (that is, 0x0000fff9.

The underlying reason for inconsistency with the expected value is that C has undergone a two-time conversion between symbols. This problem can be avoided if all the calculated measures are consistent before the calculation is started.

Both "S=L+U*C;" The statement changes to "s=l+ (long) u*c;" In recent years the compiler can omit coercion.


Starting in my blog www.xgezhang.com

A few interesting C + + topics

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.