Typical questions for c ++ programmers (2): Questions for programmers

Source: Internet
Author: User

Typical questions for c ++ programmers (2): Questions for programmers

1. What is the output result of the following program?

# Include <stdio. h>

Main (){

Int B = 3;

Int arr [] = {6, 7, 8, 9, 10 };

Int * ptr = arr;

* (Ptr ++) + = 123;

Printf ("% d, % d \ n", * ptr, * (++ ptr ));

}

Answer:

8, 8

In c, printf calculates the parameters from right to left.

2. What are the output results of the following program?

# Include <iostream>
Using namespace std;
Int main (){
Float a = 1.0f;
Cout <(int) a <endl;
Cout <& a <endl;
Cout <(int &) a <endl;
Cout <"boolalpha" <(int) a = (int &) a) <endl;
Cout <endl;
Cout <endl;
Float B = 0.0;
Cout <(int) B <endl;
Cout <& B <endl;
Cout <(int &) B <endl;
Cout <"boolalpha" <(int) B = (int &) B) <endl;
Getchar ();
Return 0;
}

Answer:


(Int) a resolves the unit value in memory from another type to the int type and creates a temporary object. (Int &) a indicates that the c ++ memory is an int type and returns a reference object.
(Int &) a = static_cast <int &> (a) (int) & a = reinterpret_cast <int> (& a); (int &) a is not converted, directly obtain the value of a in the memory unit (int) the value of a in the memory is converted to the int float Type stored in the memory. The ending number of the symbol bit index is 754: the order code uses an increase (the Anti-sign of the complement Code ), the ending number uses the original code, so the 1.0f format in the memory is 0011 1111 1000 0000 0000 0000 0000 0000 so the output is 0x3f8000000 in the memory storage format 0000 0000 0000 0000 0000 0000 0000 0000 (int &) a forcibly converts a to an integer reference type (int) & a forcibly converts a's address to an integer

(Int &) a is equivalent
* (Int *) &

* (Int *) (&)

* (Int *) &)

Consider the float type as the int type.
3. What is the output result of the following function?
#include<iostream>using namespace std;int main(){unsigned int a = 0xFFFFFFF7;unsigned char i = (unsigned char)a;char* b = (char*)&a;printf("%08x,%08x", i, *b);getchar();}
Answer:
 
char *b=(char *)&a
In X86 series machines, data is stored as "small-end storage", which means that for a data that spans multiple bytes, it is stored in a low-end unit, its high position is placed in the high address unit. For example, if an int-type data ox12345678 is stored in four memory units: 0 x memory, 0 x memory 0001,0x00000002, 0x00000003, ox00000000 is used in a low ox78, ox00000003 places 0x12 in the high position, and so on.
With the above understanding, we can continue to analyze why the above program outputs fffff7:
Char * B = (char *) & a; what did this sentence do? In fact, it is also simple. & a can be considered as a pointer to the unsigned int type data, right? (char *) & a forcibly converts & a to a char * type pointer, and a truncation occurred at this time! After truncation, pointer B only points to the oxf7 data, and because pointer B is char * type, it belongs to the number of symbols, so the number of symbols 0xf7 in printf () output fffffff7 (in this process, the parameter type is actually upgraded to default argumentpromotions ). % X indicates hexadecimal integer output.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.