I also want to learn C language-Chapter 6: Address and layout of variables in memory

Source: Internet
Author: User

I learned these two functions early this morning. printf is used to format the output to a standard output device (usually a screen). scanf is a standard input device (generally a keyboard) enter a group of data. In fact, it is not difficult to master these two functions after several hours of practice! I found a problem, that is, why is there an & (get address) before the parameter variable in the backend of scanf )!!! But not before the variables in printf ?! Here is an example:

#include <stdio.h>

int main(void)
{
int x, y;
y = (int)&x;

scanf("%d,%d", &x, y);
    printf("%d,%08x", x, y);
return (0);
}
Output result: 123,321
321,001 2ff44Why does this happen! Because! The parameter after scanf requires an address. I take x as the address and forcibly interpret this address as an integer and then assign it to y! In this way, y places the x address, and scanf treats y as the address by default. In this case, & x and y represent the same address. In this way, if you enter 123,321, The result x is the second 321,001 2ff44, which is the address of x. Similarly, this program also writes:
#include <stdio.h>

int main(void)
{
int x, y;
y = (int)&x;

scanf("%d,%d", 0x0012ff44, y);
printf("%d,%08x", x, y);

return (0);
}
//123,321
//321,0012ff44
Then let's look at this program:
#include <stdio.h>

int main(void)
{
int x, y;
y = (int)&x - (int)&y;

scanf("%d,%d", 0x0012ff44, (int)&y+y);
printf("%d,%08x", x, y);

return (0);
}
How can this program get the same result by inputting 123,321! Okay! The output result is:
123,321
321,00000004

What is the reason! (Int) & x-(int) & y indicates the difference between the two addresses x and y (because x is at the high address and y is at the low address ). 0x0012ff44 is the address of X (this address is not fixed), (int) & y + y indicates that the address of y is different from the address of x, so the result is the address of x. So you enter 123,321 and the result is, 0004.

Ii. Summary

1: The parameters after scanf must use the address value. Why do I need the address value? I don't know yet. What do I think! In the future, I will certainly understand what I will understand at a certain time as I study it. (In this book, I have probably understood a little bit, because the C language function uses value transfer by default. If it is not for the address, the input here will be input to the copy. What do I think! A copy is equivalent to a clone! Clone yourself! It looks the same! How can someone beat the clone, but you won't hurt yourself, but the address is unique, so you can find yourself through the address. Instead of finding the clone, hey! This is the case. I will understand more in the future ).

2: In vc6.0, the layout of applying variables in the memory in the main function is: Apply for a high address first, and then apply for a low address, the two variables applied for consecutively are arranged in the memory. The knowledge here can be used to debug and debug through the debugger of VC6.0! I think the debugger is really good!

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.