Scanf traps in C

Source: Internet
Author: User

From http://westsoftware.blog.163.com/blog/static/2609410920091953456841/

I have been reading books written by Andrew Koening recently. It can be said that C/C ++ or people engaged in such development are worth reading, here I recommend you read "C traps and defects" and "C/C ++ meditation".

Let's take a look at the code section "C traps and defects.

# Include <stdio. h>
Int main ()
{
Int I;
Char C;
// Printf ("I address: % LD/N", & I );
// Printf ("c Address: % LD/N", & C );
For (I = 0; I <5; I ++)
{
Scanf ("% d", & C );
Printf ("% d", I );
}
Return 0;
}

============

With such a small piece of code, you can see what the running result will be? C is a character variable.

At the beginning, I didn't quite understand what he was talking about. To make it clearer, I analyzed the memory structure.

Because scanf points to an integer pointer, C receives character input at this time. Therefore, some memory addresses of I are overwritten when inputting data to C, how to cover it? In fc6, I will overwrite the low-end address of I to the high-end address of C. I don't know if I can understand it? That is to say, C actually receives an integer variable, but C cannot store it. What should I do if there is more? It overwrites the high-end address bit of C into the low-end address bit of I, so the I value is always 0, so running this program is an endless loop.

Comment out the two sections above to print the addresses of C and I. The printed Address here is continuous.

But in vc6, it can run normally. That is, the memory address overwrite method I mentioned is different. In other words, how to overwrite is determined by the compiler? I guess so here, when the VC overwrites the memory, it is the left-side memory of c instead of the memory address of I. Therefore, it can be normal in VC.

This is also a conjecture. In FC, "to the right" is used for overwriting.

Therefore, during development, you must note that once this trap is entered, it will be difficult to debug, and sometimes there is a kind of "coincidentally" to avoid this problem, making it even harder to check errors. I hope you can have some reference for future development.

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.