Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Ask a question about c language printf and pointer
Ask a question about c language printf and pointer
Interesting discovery. It is not so much a bug of printf as a defect of C.

First of all, the basic basis is: 1, the order of C stack is from right to left, that is, the position of the previous parameter is relatively low; 2.c language stipulates that all float parameters are converted into double during transmission, which is probably due to historical reasons. 3. Regarding variable parameters, the compiler does not check and has no ability to check. It just stacks all the parameters, and whether it is correct or not depends on the programmer. Again, C thinks that programmers are always right; 4. Therefore, functions such as printf and scanf. You can't know exactly what parameters are passed to it, so you can't check whether the format string matches the parameters. So although printf("%f\n ",p) is illogical, even printf ("%f% f% f \ n", p) is only for people who look at the code. 5. The stack structure of 5.GCC is special. The GCC function directly leaves enough stack frame space at the entrance, so as long as the execution point is in the function, the position of the stack pointer actually remains unchanged. Turbo c is not like this. VC has never seen it. I never know whether GCC is an advantage or a disadvantage. 6. Depending on the Shenma machine, the pointer on a 32-bit machine is 4 bytes, the double floating point number is 8 bytes (twice the pointer), the important parts such as symbol index are in the high byte, and the low byte is the precision part.

To get to the point, this code is characterized by all printf calls, so compiling parameters equivalent to later calls in GCC will overlap and cover the positions of parameters called earlier. The expected parameters of each printf are (except the previous format string) two double*** 16 bytes, the second call gives 16 bytes, and the last three calls actually don't give enough bytes, so printf uses the upper 4 bytes of the floating-point number *p left by the previous one and covers the lower 4 bytes, which affects the points behind the decimal point. So the key is that some traces left by the last call have not been covered, and printf reused these traces when the next call was given the wrong parameters. LZ can try to insert some other function calls with at least three double parameters between printf statements and see the effect.