--- C language summary

basis

  • Program structure is three types: sequential structure, the selection structure (branched structures), cyclic structure.
  • Program must read from the main () entry, then read sequentially from the top down (do encounter loop cycle, choose to encounter selection), and only one main function.
  • Computer data is stored in binary form in the computer data storage position is his address.
  • bit is bit 0 or 1 refers. It refers byte byte, an eight-bit byte =.

Often test

  • Preprocessor is not part of the C language, does not account for the running time , do not add a semicolon .
    C language compiled program is called source code, which is stored in ASCII values in a text file.
  • PI 3.1415926 the DEFINE ; this wording is wrong , and must not semicolon .
  • Every C language program main function is the one and only one.
  • Defined in the function can no longer function.
  • Algorithms: can not enter, but must be output.
  • cyclic structures may be used to break and switch statements.
  • The lowest level comma operator, the assignment of the penultimate level.
  • int * p the difference * p and p: it simply is * p value , p is the address !
    • * p : can be used as variables to use; * action is taken after the address value p inside.
    • the p- : is used as an address to use. It may be used in the function scanf: scanf ( "% d", p);
  • * p ++ and the difference between (* p) ++ of: (written emphasis)
    • * p ++: is the address change.
      Formulas: take the current value, then the mobile address!
    • (* p) ++: is the value will be changed.
      Formulas: take the current value, then the value is incremented by 1 so.

      Example: int * P, A [] = {1,3,5,7,9};
      P = A;
      Will * p ++ and values (* p) ++ are respectively?
      * p ++: The value of this in itself is one. Since the address will be incremented by one, so the pointer to the value 3.
      (* p) ++: The value of this in itself is one. Since ++ indicates value increases, the pointer does not move, but because the value 1 becomes a self-imposed 2.

  • Two pointers:
    • * p: a pointer: storage address of the variable.
    • ** q: two pointers: a pointer to the storage address.

      Often test subject: int = 7 the X-; int * = the p-& the X-, the p-** = q;
      ask you: * p is how much? * q How much? ** q How much?
      * p = 7, * q = p, ** q = 7
      ask: ** q = & x can be written it? No, only two pointer address for storing a pointer.

  • When the program is compiled, not to form the parameter between assignment storage wear. Only when it is called , formal parameters only temporarily occupy storage space . Formal parameters with the keyword auto when making statements store category, keyword "auto" can be omitted, do not write the auto implied identified as "automatic storage class," which is the dynamic storage.
  • Storage type function is a function defined above in front of the function name storage type data type, default should be: extern , genus external function indicates that the function (i.e., can be a function of other C source file outside the present document C call )
    • extern:
      for the whole project visible, other files can be used directly after extern external declaration. That other documents can not define a variable of the same name with its (otherwise the compiler will think that they are the same variable)
    • static: the representative of static global
      only to the current file visible, other files inaccessible, other variables can be defined files with the same name, both independently of each other.

Emphasis

  • and the difference between sizeof strlen (emphasis):
    • the sizeof: (seeking actual storage space )
      is equivalent to the same thing macro, since it is only one operator , rather than a function , compile-time constant is expanded, the translation of each variable definition table, sizeof variable determined by the look-up table occupied space, which is before allocating memory to the process to be determined.
      In fact, it can be simply understood sizeof is for the "type" of, rather than "variable" , but can not see it that way at this time, such as: sizeof ( "HELLO"); in parentheses const char *, but a "string" 5. Therefore, as a result, but the size of:
    char *ps = "HELLO";
    sizeof(ps) = 4  //只是指针的大小,即 地址(整数类型占4个字节)
    sizeof(*ps) = 1 //*ps+0代表数组第一个元素的大小,即ps[0]
    
    char as[8];
    sizeof(as) = 8  //因为as的类型为 char [8],这个大小的确是8
    sizeof(*as) = 1 //*as+0代表数组第一个元素的大小,即as[0]
    
    char aa[8][9];
    sizeof((char *)aa) = 4  //还是 char *
    
    char arr[100] = "HELLO";
    sizeof(arr) = 100   //和赋什么值没什么关系,关键是"类型"是什么
    
    int func(char p[100]) {
        sizeof(p) = 4;
        /*C/C++中不能传数组,只能传指针,所以任何数组都会隐式转成指针形式进行操作。*/
    }
    • strlen: (required string length , i.e. the number of characters, not including the terminator)
      which is a function of the parameter is const char *, clear its realization is met '\ 0' end (string, counting stops , but it does not include '\ 0'). So it's not looking but look at the type of variable, depending on what the value of the variable assigned.
  • Recursive function certainly do.
  • Two important array length:
    char A [] = { 'A', 'B', 'C'}; array of length 3, a string of indefinite length (because there is no '\ 0' terminator). sizeof (a) is 3.
    char a [5] = { ' a', 'b', 'c'}; length of the array 5, a string of three. sizeof (a) 5.
    char a [] = { 'a ', 'b', 'c'}; this is a character array , representing 3 bytes .
    char a [] = "abc" is different, it is a string , and finally there is a '\ 0' terminator , representing four bytes .

  • scanf and gets the difference:
    if the input is good good study!
    • scanf ( "% s", a
      ); receive only good. Test sites: Receive disabled spaces. (Encounter a space or carriage return to terminate)
    • gets (a);
      receive good good study test sites: receiving spaces can!. (Encountered Enter termination)
  • Pointer test sites:
    char CH [] = "iamhandsome";
    char * the p-CH =;
    ask you: * Results (p + 2) and * p + 2 is the number?
    * (p + 2) = ' m' * p + 2 = 'k'

    Analysis:
    The first one is the address of +2, so take m;
    the second value is +2, i.e. ASCII value + 2, ijk, so take k.

  • String assignment:
    C language string variable is not so stored string arrays and pointers:
    1. char ch [10] = { " abcdefgh"}; for
    2. char ch [10] = "abcdefgh "; for
    3. char ch[10]={‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’};
    4. char * p = "abcdefgh"; for
    5. char * p; to
    6. ch = "abcdefgh"; wrong ! Array names can not be assigned! (Only one cycle assignment)
    7. char * p = { "abcdefgh" }; wrong ! Can not appear braces!

Macro definition defind

Problem: The difference between x * x (x * x) and #define f (x) #define f ( x).

define a macro definition keyword in C language, which is defined in the following format:

#define [MacroName] [MacroValue] divided into ordinary macro definition macro definition macro definitions and parameters

  • Common macro definition: #define the PI (3.1415926)

  • Parameters define macro (macro function): #define max (A, B) ((A)> (B) (A), (B)?)
    Note: in the macro variable enclosed in braces .
    Because, in the C language define macros at compile time, will be expanded, a "fool" replacement, also known as "literal" replace , if there is no parentheses may be misleading.

    Such as:

    int a,b,c,d,e;
    a=1;b=2;c=6;d=4;
    e=f(a+b) * f(c+d) ;  //理论值e=9*100=900
    
    #define f(x)(x*x)
    替换结果为:e=(a+b*a+b)*(c+d*c+d) = 5*34=170
    
    #define f(x) x*x 
    替换结果为:e=a+b*a+b*c+d*c+d=1+2+12+24+4=43
    
    #define f(x) ((x)*(x))
    替换结果为:e=( (a+b)*(a+b)*(c+d)*(c+d) )=3*3*10*10=900

    This is the result we want!

Guess you like

Origin www.cnblogs.com/blknemo/p/12174011.html