Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Integer data overflow in C language
Integer data overflow in C language
For how to store the long form in the computer, please refer to:

/question/ 1 12869407 . html

Yyrryyrr2' s answer

-

printf("%d\n%ld ",a,b); middle

Printf pushes "%d\n%ld", a and b onto the stack.

The variable parameter decomposition function in printf gets data from the stack instead of %d,

According to your output, you used the 16-bit compiler.

Stack data from low address to high address is:

Address (hypothetical) data

0xf000 0x3000 (pointing to ("%d\n%ld")

0xf002 0x7fff (i.e. 32767)

0xf004 0x8000 (i.e. 32768)

0xf006 0x (here is the local variable of the main function)

Then printf first accepts an int type of 16 bits, takes out 0x7fff and calls _itoa to convert it into digital characters, replacing% d.

Then printf needs to fetch a 32-bit-long data from 0xf004, but you only gave a value of 16, so the fetched 32-bit value contains junk information (16 is junk information of local variables or main functions), and it is in the form of 0x8000, but in fact it only contains 0x0000 (you can call it from your second printf).

The analysis of the last two printf is similar.

Although the second call passes in two variable parameters, there are three %d, so after the first two fetches, they continue to fetch data from the stack and fetch the data 0x0000 of the stack frame of main.

The last printf call got more data.

-

The landlord likes to explore the root of the problem.

You can read my Baidu answer about c/c++, which will benefit you a lot.