1. What are the output results 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 in the right-to-left compression stack.
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 is to resolve the value of an in-memory cell from another type to an int type and create a temporary object. (int &) A is to tell C + + memory to be an int type and return a reference object.
(int&) A = = Static_cast<int&> (a) (int) &a = = reinterpret_cast<int> (&a);(int&) A is not converted, Directly get a in memory unit value (int) A A in memory value converted to int type float type in memory is stored in the form of the sign bit exponent mantissa by 754 standard: The order code uses the increment code (the complement of the inverse symbol), the mantissa uses the original code so 1.0f in memory form 0011 1111 1000 0000 0000 0000 0000 0000 So the output is the storage form of 0x3f8000000 in memory 0000 0000 0000 0000 0000 0000 0000 0000 (int&) a casts a into shaping reference types (int) &a to cast the address of a to an integer type
(int&) A is equivalent to
* (int*) &a
* (int*) (&a)
* ((int*) &a)
The float type is interpreted as an int type.
3. What is the output 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 the X86 series of machines, the storage of data is" small-end storage ", the small-end storage means, for a multi-byte data, its low position in the lower address unit, its high position in the higher address unit. For example, an int type of data ox12345678, if stored in the 0x00000000,0x00000001,0x00000002,0x00000003 four memory units, then ox00000000 is placed in the Low ox78, and ox00000003 in the high 0x12, and so on.
With the above understanding, we can continue to analyze why the above program output FFFFFFF7:
char* b = (char*) &a; What did you do? As a matter of fact, &a can be thought of as a pointer to unsigned int type data, and (char *) &a casts the &a into a char * type pointer, and this time a truncation occurs! After truncation, pointer b only points to oxf7 this data, and because the pointer b is char * Type, is a signed number, so the signed number 0xf7 in the role of printf () output fffffff7 (in fact, the process of parameter type promotion default Argumentpromotions). %x represents the output of the 16 binary integer type.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + Programmer classic Face question (2)