Explanation of keywords in C Language
Compared with other languages, C language has fewer keywords. In C98, there are only 32 access keys in total. Let's analyze the unique role of each keyword in the C language.
1. AboutData TypeKeyword
(1) char: declare a struct variable or function
(2) double: Declares double-precision variables or functions.
(3) enum: Declares the enumeration type.
The enum type can increase readability and portability. Each object defined in enum starts from 0 by default, and can also be customized. As follows:
enum Color{RED,BLACK,WHITE};enum Number{ONE=1,TWO,THREE};
In Color, RED = 0, BLACK = 1, WHITE = 2;
In Number, ONE = 1, TWO = 2, THREE = 3;
(4) float: Declares floating point variables or functions.
(5) int: Declares integer variables or functions.
(6) long: declares long integer variables or functions.
(7) short: declare short integer variables or functions
(8) signed: Declares signed variables or functions.
(9) struct: Declares struct variables or functions.
The role of struct has been discussed in the previous blog, which can be used to implement C language encapsulation, inheritance, polymorphism, and so on.
See http://www.cnblogs.com/whc-uestc/p/4677414.html
(10) union: Declares the data type of the shared object (union ).
The union type can be used to increase memory usage as follows:
Int main () {union Unoin {int a; float B; char * c ;}; union Unoin p; p. a = 100;/* execute Statement 1 */p. B = 10.0;/* execute Statement 2 */p. c = "hello world! ";/* Execution statement 3 */return 0 ;}
If union is not used, we need to define int, float, and cahr * respectively, which occupy 12 bytes of memory space. However, when union is used, it only takes 4 bytes; however, when int a is required in statement 2 or 3 in the code above, union cannot be used. int a must be defined separately; otherwise, the read a value will be incorrect.
(11) unsigned: Declares unsigned type variables or functions
(12) void: declares that the function has no return value or no parameter, and declares that there is no type pointer (basically these three functions)
2. AboutControl statementKeyword
Loop statement
(13) for: a loop Statement (which may be obscure)
(14) do: the loop body of the loop statement
(15) while: the loop condition of the loop statement
Condition judgment statement
(16) if: Condition Statement
(17) else: The Condition Statement denies the branch (used with if)
(18) switch: Used for the switch statement
(19) case: switch statement Branch
(20) default: "other" branch in the switch statement
In case... in a switch statement, when a condition is entered, it starts to run from the case statement that meets the condition until it encounters a jump command (break; return; goto; contine ;), therefore, we recommend that you add a break next to each case statement unless you do not.
Jump statement
(21) goto: unconditional jump statement
The goto statement can be used to ensure that the program has a unique exit, avoiding overly large if nesting, however, the arbitrary use of the goto statement poses a great risk to the program (the initialization of variables and important computing statements may be skipped), affecting the robustness and readability of the Code. Therefore, it is not recommended to use it too much.
(22) continue: ends the current cycle and starts the next cycle.
(23) break: jump out of the current loop
(24) return: subprogram return Statement (either with or without parameters)
After a return statement, all the commands in the function are not executed. Therefore, make sure that the necessary commands are executed before the return statement.
3. AboutStorage TypeKeyword
(25) auto: automatic variable declaration is generally not used, because when we declare a local variable, the default value is auto.
(26) extern, global variables and functions both use the default extern attribute.
(27) register: Declares register variables. variables declared as register are stored in the CPU registers, so reading speed is very fast, but the number is limited, when multiple register variables are defined, multiple register variables in the compiler are converted to auto variables.
(28) static: declare static variables
A. When we declare a global variable as static: only the scope of its function is changed to the source file, that is, the attribute is changed from external to internal, and the others remain unchanged;
B. When we declare a function as static: its scope of action changes to the source file, that is, the attribute changes from external to internal;
C. When we declare a local variable as static: the default initialization value is 0 and is initialized only when it is defined for the first time. The memory storage area is not a stack but a static storage area; the life cycle is no longer the function, but the whole process; others remain unchanged.
4. Other keywords
(29) const: Declares Read-Only variables
Variables declared by const must be initialized at definition. As follows:
Const int num = 10; // initialization at the definition, and the variable value cannot be changed.
Since the value of a variable cannot be changed, what is the use of this variable definition? Haha, it's very useful. First, when we define an array, the size of the array can be expressed by a constant defined by const. This is the same as # define, but it is type-safe, # define is a pre-processing command that only replaces simple characters, while the compiler checks the types of variables defined by const. Secondly, when we need a variable that is no longer changed, you can use const, for example, to define the gender of a person. Since you were born, your gender has been determined. Without an accident, it will not change in your life, so we define it as read-only. Of course, some people also think it is okay not to define it as const, as long as they do not change it, but if so, human control is needed. What should I do if I forget to change it one day? Therefore, it is best to use const to define read-only or constants.
When we put const and pointer variables together, the problem becomes complicated. For example, the definition is as follows:
const int *p1;int const *p2;int * const p3;int const * const p4;
Pointer variable p1: const modifies the object pointed to by p1 before the data type. Therefore, the value of the object pointed to by p1 is constant read-only and cannot be changed, but p1 itself can be changed;
Pointer variable p2: const before *, which is the same as p1;
Pointer variable p3: After the const variable *, the variable p3 is modified, so the variable p3 itself is a constant read-only, and the object pointed to by p3 can be changed;
Pointer variable p4: Two const are used to modify the objects directed to p4 and p4 respectively. Therefore, the objects directed to p4 and p4 are read-only constants and cannot be changed.
In fact, it is easy to remember that as long as the const is before or after *, the object pointed to by the pointer is modified before *. After, it modifies the pointer itself.
The following is a simple example:
Int main () {int num1 = 0; int num2 = 1; int num3 = 2; int num4 = 3; const int * p1; int const * p2; // int * const p3; // error (1) int * const p3 = & num3; // int const * const p4; // error (2) int const * const p4 = & num4; p1 = & num1; // * p1 = 100; // error (3) num1 = 100; // * p = 1 at this time; // p3 = p4; // error (4) * p3 = 100; // p4 = p3; // error (5) return 0 ;}
In the above Code, error (1) and error (2) are easy to understand. Because const is after *, the pointer p3 and p4 are read-only and must be initialized during definition. Error (3) is because for the p1 pointer, const is before *, so the object pointed to by p1 cannot be changed. Error (4) and error (5) are caused by a const after * for p3, p4, so the pointer itself is read-only and cannot be changed after initialization.
(30) sizeof: calculates the Data Type length.
Many people do not understand the difference between sizeof and strlen: sizeof is an operator, while strlen is a function. sizeof calculates the size of the Data Type, while strlen calculates the length of the string; the sizeof parameter can be either a data type or a variable, while the strlen parameter can only be char * and must end with a null character; the sizeof return value type is unsigned, the strlen return value is signed, because it needs to return a negative number to indicate an error.
(31) typedef: Used to get an alias for the Data Type
Typedef is useful in programming. When a data type is very long (for example, a function pointer), we can use typedef to choose a suitable name to replace it; when int, float, and double types are used, you can use your favorite and intuitive names to redefine them, when we need to replace the float type in the project with the double type in the future, we can replace the float type with the double type directly on typedef, instead of changing each float in all code to double.
(32) volatile: indicates that variables can be implicitly changed during program execution.
Variables modified by volatile do not allow the compiler to optimize the operations related to it. variables defined by volatile may be changed outside the program, so they must be read from the memory each time, it cannot be used repeatedly in the cache or register. It is generally used in the following areas:
A. Hardware registers of parallel devices (for example, Status Registers)
B. Non-automatic variables accessed in an interrupt service subroutine)
C. variables shared by several tasks in multi-threaded applications
The above conclusions are only personal opinions and suggestions. If the above statements are incorrect or you may have different opinions, please correct and discuss them.