C programming FAQ Summary

Source: Internet
Author: User

1. forget the annotation Terminator code a = B;/* The bugc = d appears;/* c = d will not be executed */2. unexpected value assignment/unexpected bool Expression Code: if (a = B) c;/* a is always equal to B, only when B! If (0 <a <5) c;/* Boolean expression is always true */the bool expression in the above Code is always true, since the value of 0 <a is 0 or 1, it is always less than 5. Of course, there is no bool expression in C. Here we only assume 3. defective macro code: # define assign (a, B) a = (char) bassign (x, y> 8) to x = (char) y> 8/* may not be your goal */4. unmatched header file code: // foo. h: struct foo {BOOL a}; // F1.c # define BOOL char # include "foo. h "// F2.c # define BOOL int # include" foo. h "F1 and F2 have different definitions of the BOOL type in the structure foo. If the interaction occurs, error 5 is displayed. you may write the following code for erratic return values: int foo (a) {if (a) return (1) ;}/* Bug, because sometimes no value is returned */6. for an unpredictable struct, see the following bit Package Structure: struct eeh_type {uint16 size: 10;/* 10 bits */uint16 code: 6;/* 6 bits */}; depending on which C compiler is used and the size of your machine is used, this code is implemented as follows: <10-bits> <6-bits> or <6-bits> <10-bits> depends on the C compiler, machine architecture, and incredible priority settings, these items may be aligned to the nearest 8, 16, 32, or 64 bits. 7. uncertain order of evaluation foo (pointer-> member, pointer = & buffer [0]); different compilers have different order of evaluation for function parameters, gcc is the order of evaluation from left to right, and some compilers are 8 from right to left. code of block scope that is easy to change: if (...) foo (); else bar (); Debugging output: if (...) foo (); else printf ("Calling bar ()");/* Note! Else stops here */bar ();/* Note! Bar will always be executed */9. unsafe return value code: char * f () {char result [80]; sprintf (result, "anything will do"); return (result);/* Note! Result is allocated to the stack. */} int g () {char * p; p = f (); printf ("f () returns: % s \ n", p);} 10. side effects is not defined. Even if a simple expression is used, C does not define the order of side effects. The result depends on your compiler. I/I ++ may be equal to 0 or 1, see the following code: # include <stdio. h> int foo (int n) {printf ("Foo got % d \ n", n); return (0);} int bar (int n) {printf ("Bar got % d \ n", n); return (0);} int main (int argc, char * argv []) {int m = 0; int (* (fun_array [3]) (); int I = 1; int ii = I/++ I; printf ("I/++ I = % D, ", ii); fun_array [1] = foo; fun_array [2] = bar; (fun_array [++ m]) (++ m); return 0 ;} 11. uninitialized local variables are not so well-known in fact, but they won't be lost to other bugs in the event of severity. See the following code: void foo (a) {int B; if (B) {/* bug! B is not initialized */} modern compiler will issue an error warning, read the following code: void foo (int a) {BYTE * B; if () B = Malloc (a); if (B) {/* BUG! B may not be initialized */* B = a;} 12. During the messy compilation environment, the environment generates hundreds of thousands of compilation information. We do not know much about this. Some dangerous common names are difficult to find: # include <stdio. h> # define BUFFSIZE 2048 long foo [BUFSIZ]; // note the spelling: BUFSIZ! = The BUFFSIZE compiler will not report an error because the BUFSIZ is already in stdio. h defines 13. in C, the number of BITs starts from 0. If no '8' or '9' appears in the number, the compiler will not warn int numbers [] = {001,010, // 8 instead of 10 014}; // 12 instead of 14. there may be various errors in the signed char in the signed character C. For example, 128 is a negative number. In addition, you must be very careful when using low-precision integers, C makes it easy to ignore char s = 127; unsigned char u = 127; s ++;/* the result is a negative number */if (s <u) {/* true! */} If (s> 127) {/* may never be true */} if (u <0) {/* may never be true */} 15. bad "standard library" code: {int a = 1, B = 2; char buf [10]; scanf ("% d", a, B ); // & a, & B? Sprintf (buf, "this is the result: % d"); // overflow}

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.