Recalling the C language learning a second back: basic Grammar 2

C, the specific characters, when they are preceded by a backslash, they have a special meaning, such as is used to indicate newline (\ n) or tabs, the following table lists some of these escape serial number :

Escape Sequences meaning
    \\               \ Character                                                                          
     \' 'Character
    \" "Character
    \? ? Character
    \a Alarm bell
    \b Backspace
    \f Page breaks
    \n Newline
    \r Enter
    \t Horizontal tab
    \ v Vertical tab
    \ooo One to three octal number
    \ Socialization. . . One or more digits of hexadecimal digits

 

Storage Class C

Storage class definitions in the C program variables / functions range (visibility) and life cycle. These keywords before the type to be described. C program storage classes are available:

  • auto: all local variables default storage class,
  • register: a register for storing declare a local variable instead of in RAM. This means that a maximum size equal to the size of the variable register (typically a word), and it is not a unary '&' operator (because it has no memory location). Variable registers need for fast access, the definition of 'register' means that the variable is not stored in the register, it means that the variable may be stored in the register
  • static: instructs the compiler to keep the existence of local variables within the lifetime of the program, without the need to be created and destroyed every time it goes out of scope and, therefore, the use of static local variable declarations can be maintained between function calls local variables. static modifier can be applied to global variables. When the static modification of global variables, variables or method scope will be limited to declare it within the file, it can be present in any file functions and methods call.
  • extern: to provide a reference to a global variable, the global variable declaration for all the program files are visible. For extern  uninitialized variables declared, we will point to a previously defined variable names over the storage location.

 

Operator C

Arithmetic Operators

+ (Plus), - (minus), * (multiplication), / (divisible),% (remainder), ++ (increment), - (decrement)

For the + and - when used in conjunction with an assignment expression, the right to re-assign the variable increment or decrement, the variable assignment and then left the first increment or decrement

Relational Operators

== (equal) ,! = (Not equal),> (greater than), <(less than),> = (greater than or equal), <= (less than or equal)

Logical Operators

&& (logical AND), || (logical OR),! (Logical negation)

Bitwise Operators

Bitwise operators acting on the bit, and perform bitwise operations.

& (Bitwise and), | (bitwise OR), ^ (bitwise exclusive or) ~ (bitwise), << (bitwise left shift operator), >> (bit right shift operator)

Assignment Operators

= (Assignment)

Miscellaneous Operators

sizeof () (return variable size), & (the return address of a variable), * (pointing to a variable), A? b: c (A true compared with b, otherwise c).

Operator Precedence

The following table will be listed in descending operator precedence each operator, having a higher priority operator appear in the table above, having the following lower priority operators appear in the table. In the expression, a higher priority is preferentially operators calculated

category  Operators  Associativity 
suffix                     () [] -> . ++ - -                                                    From left to right 
Unitary  + - ! ~ ++ - - (type)* & sizeof  From right to left 
Multiplication and division  * / %  From left to right 
Addition and subtraction  + -  From left to right 
Displacement  << >>  From left to right 
relationship  < <= > >=  From left to right 
equal  == !=  From left to right 
Bits and AND  From left to right 
Bit XOR XOR  From left to right 
Bit or OR  From left to right 
Logical AND AND  &&  From left to right 
Logical OR or  ||  From left to right 
condition  ?:  From right to left 
Assignment  = += -= *= /= %=>>= <<= &= ^= |=  From right to left 
comma  From left to right          

 

C judgment

C language to any non-zero and non-null value is assumed to be  to true , the zero or  null  is assumed  to false .

if...else if...else 和 switch

switch  statement must follow the following rules:

  • switch  statement  expression  is a constant expression, it must be an integral or enumeration type.
  • In a switch you can have any number of case statements. Each case is followed by a value to be compared and a colon.
  • case of the  constant-expression  must have the same data type variable in the switch, and must be a constant or literal.
  • When the constant case of the variable being tested is equal to, followed by the case of statements will be executed until the encounter  break  statement so far.
  • When faced  break  when the statement, the termination switch, control flow will jump to the next line after the switch statement.
  • Not every case needs to contain  BREAK . If the case statement does not contain a  break , control flow will  continue to  follow the case, until a break is encountered so far.
  • switch  statement can have an optional  default  Case, appears at the end of the switch. default case can be used in all of the above case is not true when performing a task. default case in the  break  statement is not required.

 

C cycle

while loop (when a given condition is true, repeat the statement or group of statements.), for circulation, break (termination loop or  switch  statement), continue keyword (immediately stop the current loop iteration, re-start the next iteration of the loop.)

for ( init; condition; increment )
{
   statement(s);
}

 The control flow for loop:

  1. init  will be executed first, and only once. This step allows you to declare and initialize any loop control variable. You can also write any statement that is not here, as long as there is a semicolon can occur.
  2. Next, will judge  for condition Condition . If true, the loop body is executed. If false, the loop body is not executed, and the control flow will jump to immediately for the next statement cycle.
  3. After the main loop for execution, control flow jumps back to the top of the  increment  statements. This statement allows you to update the loop control variable. The statement can be left blank, as long as there is a semicolon after the condition can occur.
  4. Condition is determined again. If true, the loop is executed, the process will be repeated (the loop body, and then increased by the step value, and then re-determination condition). When the condition becomes false, for loop terminates.

Use for (;;) or while {} structure to represent an infinite loop. When the conditional expression does not exist, it is assumed to be true. Press Ctrl + C key to terminate an infinite loop.


C function

Defined Functions

return_type function_name( parameter list )
{
   body of the function
}
  • Return type: a function can return a value. return_type  is the data type of the value returned by the function. Some functions perform the required operations without the return value, in this case, the keyword return_type  void .
  • Function name: This is the actual name of the function. Function name and parameter list together constitute the function signature.
  • Parameters: parameter like a placeholder. When the function is called, you pass a value to the parameter, this value is called the actual parameters. Parameter list of function parameters include the type, order, quantity. Parameter is optional, that is, the function may not contain parameters.
  • Main function: function body contains a set of functions defined mission statement.

Declare functions and function calls

return_type function_name( parameter list );

In the function declaration, the name of the parameter is not important, only the type of the parameter is required.

When you define a function in a source file and call a function in another file, the function declaration is required. In this case, the function should be declared at the top caller in the file is located.

The function is called, passing the required parameters, if the function returns a value, the return value may be stored.

Function Arguments

Call-by: passing a parameter to a function call-by- method, the copying the actual value of the parameter to the formal parameters of the function. In this case, the parameter modifications do not affect the function of the actual parameter.

A reference: the way passed by reference, parameter argument a pointer to the address, when the address pointed to by the operating parameter, is equivalent to the operation of argument itself, i.e., the argument changes.

C scope rules

Variable scope is defined in the program area is present over the variable region can not be accessed. C language, there are three places you can declare variables:

  1. In function block inside or local variables
  2. In all external functions global variables
  3. In the form of function parameters define the parameters of

Local variables

In the variable declared inside a function or block called local variables. They can only be used or the function inside the block statement. Local variables outside the function is unknown.

Global Variables

Global variables are defined outside the function . Global variables throughout the application life cycle are valid, within any function can access the global variables.

Within a function, if the names of local variables and global variables of the same name, will use the value of local variables, global variables will not be used.

Global variables and local variables in memory difference :

  • Global variables stored in memory in the global memory area, occupied by the static memory cell;
  • Local variables stored in the stack, only the dynamically allocated memory cell is where the variables when the function is called.

Local and global variables initialized

Local variables when defined, to initialize itself it will not be initialized before its use. The definition of global variables , the system will automatically initializes it. Global variables are initialized default values are as follows:

type of data                       Initialization defaults                 
int          0     
char '\0'
float 0
double 0
pointer NULL
Published 161 original articles · won praise 90 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_42415326/article/details/104017638