Start to look at C language defects and traps, and write down notes

Source: Internet
Author: User
I only came back to school in Shanghai this morning. I am tired and may be tired. I don't want to go to bed. I just want to read the electronic documents from the computer. In fact, I seldom read books on the computer, the general situation is the game. In fact, programming is the same as being a human. Even if the C language experts are familiar with this book, it is necessary to study the content. as the author said at the beginning of the book, this book is neither a criticism of the C language nor a tutorial of the C language. The author assumes that we have a bit of knowledge and skills in the C language .! To put it bluntly, write my reading experience in the order of books. These are only suitable for birds and birds. Experts should not fight against my enthusiasm. First, it means = and =, & and |. I want everyone to know what these mean. = is a value, & and | is a bitwise AND or, = is to judge equals, and & | is a logical operator number. therefore, the following statement seems to check whether X is equal to Y:

 

If (x = y) Foo ();

 

In fact, it is to set X to Y and check whether the result is non-zero. Consider the following loop to skip spaces, tabs, and line breaks:

 

While (C = ''| C = '/T' | C ='/N') C = GETC (f );

 

In the comparison with '/t', the programmer mistakenly uses = instead of =. This "comparison" actually assigns '/t' to C, and then judges whether the (new) value of C is zero. Because '/T' is not zero, this "comparison" will always be true, so this loop will eat the entire file. What will happen afterwards depends on whether a specific implementation allows a program to read the part beyond the end of the file. If allowed, this loop will continue to run. For example, in Linux or UNIX, the open method is often used to open files. This function is not fopen. It returns the file descriptor, fopen returns a pointer to the file structure. The former is an integer, the failure is negative, the success is 0 or a positive integer, or a (File *) pointer. knowing the role of open, let's look at this program in the reading, if (filds = open ("/user/AA. C ", 0) <0) // filds is file descriptor symbol Foo (). If open fails, <0 can occur, but =, no matter whether filds is initialized or not, so that the content on the left of <0 is 0 or 1, Foo () will never be executed. for &, we can call a program, for example: If (10 & 01) // The logic used here to judge Foo (); // here, foo can call the function, but the steps are the same for bitwise judgment:

If (10 & 01) // The bitwise judgment Foo () is used here; // here, foo calls a function and cannot be executed. Note that, that is, the logical operator number and the bitwise operator number do not change the calculation object. to be continued ^

In fact, you can find these problems through GDB debugging or using single-step execution in windows. PS. Here I have a C language defect and trap for Word documents, and you can leave a mailbox.

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.