Keywords in C Language

Source: Internet
Author: User
I. There are 32 keywords in C language, which can be divided into four types based on their functions: Data Type keywords, control statement keywords, storage type keywords, and other keywords. 1. Data Type keywords (12): (1) Char: declare a variable or function (2) double: declare a double variable or function (3) Enum: declare an enumeration type (4) float: declare floating point variables or functions (5) int: declare integer variables or functions (6) LONG: declare long integer variables or functions (7) short: declare short integer variables or functions (8) Signed: declare signed type variables or functions (9) struct: declare struct variables or functions (10) Union: declare shared bodies (union) data Type (11) unsigned: Declares unsigned type variables or functions (12) void: Declares functions without return values or parameters, and declares no type pointers (basically these three functions) 2 control statement keywords (12): a loop Statement (1)) while: loop condition of the loop Statement (4) Break: jump out of the current loop (5) Continue: end the current Loop, start next loop B Condition Statement (1) if: Condition Statement (2) else: Condition Statement negative Branch (used with if) (3) Goto: unconditional jump Statement C switch statement (1) Switch: Used in switch statement (2) Case: switch statement Branch (3) Default: "other" branch in switch statement d Return Statement return: subroutine Return Statement (which can contain parameters or without parameters) 3 storage type keywords (4) (1) Auto: declare automatic variables generally do not use (2) extern: declaration variables are declared in other files (or can be considered as reference variables) (3) Register: Declaration of deposit variable (4) static: Declaration of static variables 4 other keywords (4) (1) const: declaring a read-only variable (2) sizeof: calculating the length of the Data Type (3) typedef: Used to get an alias for the data type (of course, there are other functions (4) Volatile: it indicates that the variable can be implicitly changed during program execution. 2. Control the GOTO statement in 9 of C language: unconditional redirection; I F statement: Judgment statement; while loop statement; Do-while statement: First executes the loop body and then determines whether the loop condition is true. then continue the loop; for statement: loop, which can replace the while statement; but the usage is different; the break statement jumps out of the loop at the current layer; (only jumps out of the loop containing this statement) The continue statement: continue (generally put it in a loop statement. If you do not execute the statements below it, You can directly jump to the judgment statement example: for statement, and then directly jump to the second semicolon, while statement, jump directly to the parentheses of while (); switch statement: multi-phase selection; Return Statement: return; first Keyword: Auto is used to declare automatic variables. You can explicitly declare variables as automatic variables. As long as the variable is not declared before all functions, even if the auto keyword is not added, it is also an automatic variable by default. And only valid in the declared function. And after use, its value is automatically restored to the original value. Assign values to automatic variables because they contain unknown values. Example: auto int name = 1; second keyword: static is used to declare static variables. You can explicitly declare variables as static variables. It is also a local variable. Valid only in the declared function. Its life cycle starts from the beginning of the program to the end of the program. And even after use, its value is still not restored. It is automatically initialized to 0 even if no value is assigned to the static variable. for example, static int name = 1. there are three obvious roles: 1 ). in the function body, a variable declared as static remains unchanged when the function is called. 2) within a module (but in the external body of the function), a variable declared as static can be accessed by the function used in the module, but not by other functions outside the module. It is a local global variable. 3) in a module, a function declared as static can only be called by other functions in the module. That is, this function is restricted to use within the local scope of the module that declares it.

Differences between static global variables and common global variables static local variables and common local variables differences between static functions and common functions

The description of global variables (external variables) is preceded by static to form a static global variable. Global variables are static storage, and static global variables are also static storage. The two are not different in storage methods. The difference between the two lies in that the scope of non-static global variables is the entire source program. When a source program is composed of multiple source files, non-static global variables are valid in each source file. The static global variable limits its scope, that is, it is valid only in the source file defining the variable, and cannot be used in other source files of the same source program. Because the scope of static global variables is limited to one source file, they can only be shared by functions in the source file. Therefore, errors in other source files can be avoided.

From the above analysis, we can see that after a local variable is changed to a static variable, its storage mode is changed, that is, its survival time is changed. After changing a global variable to a static variable, it changes its scope and limits its scope of use.

Static functions have different scopes than normal functions. Only in this file. Only functions used in the current source file should be declared as internal functions (static). Internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, it should be described in a header file that the source file to use these functions must contain this header file.

 

Static global variables and common

For a standard data type, its address only needs to be an integer multiple of its length, while the non-standard data type is aligned as follows:

 

  
Array: Alignment Based on the basic data type. The first alignment is followed by the natural alignment.
Union: Alignment Based on the Data Type with the maximum length.

 

Struct: each data type in the struct must be aligned.

What is the difference between global variables? STATIC global variables are only made once to prevent being referenced in other file units. Static global variables limit their scope, that is, it is valid only in the source file defining the variable and cannot be used in other source files of the same source program.

 

What is the difference between static local variables and common local variables: static local variables are initialized only once, and the next time is based on the previous result value;

What is the difference between a static function and a common function: a static function has only one copy in the memory, and a normal function maintains one copy in each call.

Local variables of the program exist in (stack), global variables exist in (static zone), and dynamic application data exist in (HEAP.

The third Keyword: extern is used to declare global variables. The variables declared before the main function are also called global variables. It can be used anywhere in the program. It always exists during the program running. The global variable is also initialized to 0. For example: extern int name; Fourth Keyword: register is used to declare as a register variable. It is also a local variable and only valid in the declared function. It is stored in registers. The speed is much faster. Using it to declare variables that need to be frequently used increases the program running speed. Example: Register int name = 1;

Declare a variable Resident Register to improve the program running efficiency. This is especially common in embedded development. However, it is a statement of creation. The Compiler determines whether the application is successful Based on the program.

Fifth Keyword: int is used to declare the type of the variable. Int is an integer. Note that the range of the 16-bit and 32-bit systems is different. The 16-bit occupies 2 bytes. The 32-bit occupies 4 bytes. It can also be explicitly declared as unsigned or signed: Unsigned int signed Int. The difference between signed and unsigned is that the symbol bit is also stored as a digit. You can also declare short and long as short integers or long rows. Example: int num; sixth Keyword: float is used to declare the type of a variable. Float is a float type, also called a real type. Its range is fixed to 4 bytes. The six digits are decimal places. Others are integer digits. For example: Float name; the seventh Keyword: Double is used to declare it as a double-precision type. The value range is 8 bytes. The 14 digits are decimal places. You can also use a long double with a higher precision, which has a larger range of 10 bytes. For example, double name; the eighth Keyword: struct is used to declare the struct type. Struct can contain various types of quantities. For example, variable types such as integer and struct can be declared in the same struct type, and can be directly called using struct variables when used. Ninth Keyword: Char is used to define it as a variable of the character type. Its range is usually 1 byte. It is represented by ASC | MA in the memory. Therefore, it can also be used with integer operations. You can also use unsigned or signed definitions. Sigened char unsigned char example: Char C; tenth Keyword: Break is used to indicate interruption. It is generally used in a loop. Determine whether the conditions are met and then interrupt the current loop. For example, break; 11th keywords: Continue is used to skip the statement after the current statement and continue the next loop. Example: continue; 12th keywords: Long is used to declare the type of the long type. For example, long int long double. 13th keywords: If judgment statement, used to determine whether the statement meets the conditions. 14th keywords: Switch condition selection statement, which is often used to determine the conditions selected by the user to execute specific statements. 15th keywords: Use Case with switch. The example is the same as above. 16th keywords: Enum is used to declare enumeration variables... For example, Enum day {one, two, three, four, five, six, seven}; key sub-Statement of 17th: Type redefinition of typedef .. type can be redefined, for example, typedef unsigned int u_int; // defines the unsigned integer as u_int. 18th keywords: return; Return Statement. Returns a value. When we define a function as a function with a returned value, we must return a value. 19th keywords: Unio defines the joint object. The usage is the same as that of struct. The difference is that all Members of the shared object share the bucket. 20th keywords: const is defined as a constant .. for example, const int A; // The value of variable A cannot be changed. what is the main function of cons? (1) const constants can be defined and are non-mutable. For example: const int max = 100; int array [Max]; (2) it is easy to perform type check, so that the compiler can learn more about the processing content and eliminate some hidden risks. Example: void F (const int I ){.........} the compiler will know that I is a constant and cannot be modified. (3) it can avoid the appearance of numbers with fuzzy meanings and easily adjust and modify parameters. Same as macro definition, it can be changed as long as it remains unchanged! For example, if you want to modify the content of Max in (1), you only need to: const int max = you want! (4) It can protect modified things, prevent accidental modifications, and enhance program robustness. In the above example, if I is modified in the function body, the compiler will report an error. For example: void F (const int I) {I = 10; // error!} (5) provides a reference for function overloading. Class {...... void F (int I ){......} // void F (int I) const {......} // overload of the previous function ......}; (6) It can save space and avoid unnecessary memory allocation. For example: # define PI 3.14159 // constant macro const doulbe Pi = 3.14159; // pi is not put into ROM at this time ...... double I = PI; // at this time, the PI is allocated with memory and will not be allocated later! Double I = PI; // macro replacement during compilation, allocating Memory double J = PI; // no memory allocation double J = PI; // then macro replacement, allocate memory again! Const defines constants. From the assembly point of view, only the corresponding memory address is given, rather than the number of immediate values as # define. Therefore, the const-defined constants have only one copy during the program running, while the # define-defined constants have several copies in the memory. (7) improved efficiency. Generally, the compiler does not allocate storage space for common const constants, but stores them in the symbol table, which makes it a constant during compilation without the operation of storage and read memory, this makes it highly efficient. (1) modify a constant. A common constant is a constant of a simple type. When defining such constants, the modifier const can be used before or after the type specifier. For example, int const x = 2; or const int x = 2; (2) modifies the definition of a regular array or indicates that a regular array can be in the following format: int const A [5] = {1, 2, 3, 4, 5}; const int A [5] = {1, 2, 3, 4, 5 }; (3) modifying a constant object is an object constant. The definition format is as follows: Class A; const A; a const A; Initialization is also required when defining a constant object, the object cannot be updated again. The modifier const can be placed after the class name or before the class name. (4) modify the constant pointer const int * A; // The const modifier points to the object, a variable, and a points to the object immutable int const *; // const modifies the object to which a points. A is variable. The object to which a points is immutable int * const A. // const modifies the pointer A and A is immutable, A points to the object variable const int * const A; // pointer A and A points to the object are not variable (5) modifier often reference using the const modifier can also indicate reference, the referenced object cannot be updated. The definition format is as follows: const double & V; (6) the const modifier of the modifier can also modify the passing parameters of the function. The format is as follows: void fun (const int var ); tells the compiler that VaR cannot be changed in the function body, thus preventing unintentional or incorrect modifications. (7) modifier return value: the const modifier can also modify the return value of the function. The return value cannot be changed. The format is as follows: const int fun1 (); const myclass fun2 (); (8) modifier class member functions: the const modifier can also modify the class member functions in the following format: Class classname {public: int fun () const ;.....}; in this way, you cannot modify the data in the class when calling the function fun (9) reference the const constant extern const int I in another connection file; // correct reference extern const Int J = 10; // error! A constant cannot be assigned another value. In addition, it must be noted that the constant must be initialized! For example: const int I = 5; 21st keywords: Unsigned is defined as an unsigned variable .. all the default variables are signed. unless it is declared as unsigned. 22nd keywords: For Loop statement. you can specify how many times the program loops. for (INT I = 0; I <5; I ++) {printf ("the program will output 5 times! ");} 23rd keywords: signed declares the variable as a signed variable. The default variable is signed. Generally, this parameter can be omitted. 24th keywords: void null type .. It is generally used to declare that a function has no return value or has no parameters. 25th keywords: default is used in the switch statement. Define the default processing. For the usage, see switch. 26th keywords: goto unconditional jump statement 27th key subwords: sizeof is used to get the storage space size of the variable. for example: int A, B; B = sizeof (a); 28th keywords: Volatile declares variables as variable. remind the compiler that the variables defined after it may change at any time. Therefore, each time the compiled program needs to store or read this variable, it will directly read data from the variable address. If there is no volatile keyword, the compiler may optimize reading and storage, and may temporarily use the value in the register. If this variable is updated by another program, it will be inconsistent. Generally, volatile is used in the following aspects: 1. Variables modified in the interrupt service program for other programs to be detected must be added with volatile; 2. Volatile should be added to the labels shared among all tasks in a multi-task environment; 3. Volatile should also be added to the memory-mapped hardware registers, because each read/write to it may have different meanings; in addition, in the above situations, we often need to consider data integrity at the same time (some of the correlated symbols have been read in half and are interrupted for rewriting). In 1, we can achieve this through Guanzhong disconnection, task Scheduling can be disabled in 2, and 3 can only rely on the good design of hardware. Volatile is always related to optimization. the compiler has a technology called data stream analysis, which analyzes where variables are assigned, where they are used, and where they are invalid. The analysis results can be used for constant merging, constant propagation optimization, further eliminating code. However, sometimes these optimizations are not required by the program. In this case, you can use the volatile keyword to disable these optimizations. the literal meaning of volatile is variable and has the following functions: 1. The volatile variable is not cached in the register between two operations. In a multi-task, interrupted, or even setjmp environment, variables may be changed by other programs, and the compiler itself cannot know. Volatile tells the compiler this situation. 2. Constant merging and constant propagation are not optimized, so the following code: Volatile int I = 1; if (I> 0)... if conditions are not considered unconditional. 3. read/write of volatile variables will not be optimized. If you assign values to a variable but do not use them later, the compiler can often omit the assignment operation. However, the processing of memory mapped Io cannot be optimized like this. 29th keywords: Do is generally used in combination with the while statement. The form is constructed, such as do while (

First execute and then judge) or while do (

First judge and then execute) 30th keywords: while loop control statement. As long as the expression is true, it will keep repeating. 31st keywords: else is often used together with if. 32nd keywords: Short is used to declare a short integer variable: for example, short int;

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.