C language programming - modern methods (note 2)

Chapter Seven basic types

1, integer type (7.1)

  If the signed integer is positive or zero, then the leftmost bit (sign bit) is 0; if it is negative, the sign bit is 1.

  By default, C language integer variable is signed.

  Decimal constant contains the numbers 0-9, but can not begin with zero; octal constant contains only 0-7, we must begin with zero; hexadecimal constant contains 0-9 and letters af, and always start with 0x.

  In order to force the compiler to handle long integer constant as required by the letters L (or l) in the back, for example: 15L, 0x7fffL.

  To indicate unsigned constant to be the letters U (or u) behind a constant, for example: 15U, 0x7fffU.

  Signed integer overflow, program behavior is undefined; unsigned integer overflow, the result is defined: generally 0.

2, floating-point type (7.2)

  C language provides three floating-point type: float (single-precision floating point), double (double precision floating point), long double (extended precision floating point number). float and double are good enough for most applications.

  float 6 digits precision, double precision of 15 digits. It must contain a decimal point or floating-point constant index, for example: 57.0,5.7e + 1.

3, character type (7.3)

  In C, the association between characters and integers are very strong, in fact, character constants of type int rather than char types.

  Signed (Signed) characters in the range: -128-127; unsigned (unsigned) Character Value range: 0-255.

  Portability Tip: Do not assume that the default char type is signed or unsigned. If there are differences, instead of signed char or unsigned char by char ..

  There are two escape sequences: character escape sequences and digital escape sequences. Character escape sequences, for example: \ a, \ n, \ t like. Escape sequence number, for example: \ x1b, \ 033 and the like.

  Character handling functions: lowercase to uppercase characters, ch = toupper (ch), need to include #include <ctype.h>

  With scanf and printf function can also read / write characters:

do {
  scanf("%c",&ch);
} while (ch != '\n')

  getchar () returns a value of type int, char type value instead. getchar () idiom. The first line break is not for detecting, for detecting the second space.

while (getchar() != '\n')
    ;

while ((ch = getchar()) == ' ')
    ;

4, the type of conversion (7.4)

  When the arithmetic expression, or the number of types of logical operation expressions are not the same; and the left side when the type of the variable type of the assignment operator does not match the expression on the right; the function call when the corresponding argument type parameter when the type of mismatch; when the expression in the return statement type and the type of the function return value does not match.

  Any type of a floating point operand type, needs to be promoted to Double float; two operand types are not floating point type, needs to be promoted to unsigned int int or long int.

  C language will assign the right type of operations expression into the variable on the left.

  Cast: (float) dividend / divisior; variable dividend cast float, the compiler will convert divisior to float.

5, type definition (7.5)

  Using typedef type definition, e.g., typedef int Bool;

  Type Definition easier to understand, we can better define the types of programs written to be portable.

  You can transplant techniques: For greater portability, consider using typedef to define a new integer type name.

6, sizeof operator (7.6)

  sizeof (type name), the return value is an unsigned integer value representing the number of bytes required to store the type names.

  sizeof can also be used constants, variables and expressions, such as sizeof (i + j)

Chapter VIII array  

1, one-dimensional array (8.1)

  An array is a data structure comprising a plurality of data values, each data value and having the same data type.

  An array of common operations 

for (I = 0 ; I <N; I ++ ) 
    A [I] = 0 ;                     // initialize array 

for (I = 0 ; I <N; I ++ ) 
    Scanf ( " % D " , & A [I]);         // read data into an array 

for (I = 0 ; I <N; I ++ ) 
    SUM + = A [I];                  // array of summing elements

  Array initialization: int a [10] = {0}; illegal initialized completely empty, so to place a 0 in braces, other values ​​will be initialized to 0 by default.

  C99 specified initialization: int a [15] = {[2] = 29, [9] = 7, [14] = 48}

  Sizeof can calculate the size of array elements, such as the array is a [10], with the size divided by the size of the array element of the array: sizeof (a) / sizeof (a [0])

  Array assignments except that the following method can also be used memcpy function assignment need string.h header file contains typically more efficient memcpy.

for (i = 0; i < N; i++)
  a[i] = b[i];

2, multi-dimensional arrays (8.2)

  C language arrays are stored in row major order, i.e. from the zero line, then the first row, and so on.

  Multidimensional arrays do not recommend braces inner omitted.

  Constant array (before the statement added const) benefits: First, indicating that the program does not change the array (so people can read the program), and help compiler errors are found (to tell the compiler does not modify).

  Variable-length array advantages: programmers do not have a configured length given array, the program can calculate the number of elements required in the implementation.

  Variable-length arrays restrictions: no static storage limit is not initialized.

The ninth chapter function

Guess you like

Origin www.cnblogs.com/chengabc/p/12076814.html