Lesson 1 C language concise tutorial
1 Preface: 1 compared with advanced languages such as Java and C #, C language is very simple, simple to learn, and easy to use, but also very important, so far, more than 90% of the kernel code of the operating system has been completed in C language. Therefore, learning C language is the foundation for learning computer well, especially when it comes to system programming. Today is my first class to review C language courses. I will record the basic knowledge of C language. This class involves the structure, variables, types, input and output, condition judgment, and cyclic knowledge of C language. 2 knowledge point: 2.1 the structure of C language 2.1.1 generally the C language program is composed of: 1. for related code comments, use/* · */to annotate single or multiple lines, or use // single line comments (C99 standard); 2. define related variables, global (static) variables or local (static) variables; 3. main Entry function with return value int; 4. if you use an external function, you need to introduce the header file; 5. other function methods are optional; 6. the code block is used to indicate the judgment of the program. Let's look at the code below; # include <stdio. h> # use this header file to call system functions/* entry function */int main (void) # main, at this time, the parameter value is void, which indicates that the parameter {# main entry function is not passed. int data starts at the beginning of the code block; # This data local variable is not initialized and is not assigned a value later. If the output value is an unknown static int cdata; # If the local static variable is not initialized, it is stored as 0 and bass, with the initial storage Stored in data char * p = "Hello World! \ N "; # define the local variable p and assign the value printf (p); # Call the system function, which is in stdio. in h, int flag = Show (p); # Call the Show (char * p) return 0 UDF with int type returned; # This method has the returned value int} # The End Of The main code block/* Show function parameter: char * p return value: int */int Show (char * p) {printf (p ); return 1 ;}, // The global variable int golbal_data; # The uninitialized variable (stored in bss) is 0, the initialized global variables are stored in the data 2.2 variables and the data Type 2.2.1. For example, [] brackets indicate that they are dispensable. The int type ranges from-2 ^ (32-1) to 2 ^ (32-1)-1, and so on. int, float, and double indicate the minimum number of digits, because they are related to the number of digits in the operating system, such as long int. In zai 32-bit operating systems, the number of bytes occupied by the int type is the same, but in 64-bit operating systems, long int Is 8 bytes. There are also some types, such as iso c99 extension type: int8_t is always 8 bits, and int16_t is always 16 bits. Use sizeof (int) and sizeof (long) to print the number of bytes occupied by the current platform. Generally, use the int type as much as possible: the efficiency is relatively high; double precision is enough; Use unsigned as little as possible. printf ("% d \ n", sizeof (int); printf ("% d \ n", sizeof (long); printf ("% d \ n ", sizeof (char); printf ("% d \ n", sizeof (float); printf ("% d \ n", sizeof (double )); 2.2.2 constants indicate that 2.2.2.1 constants are divided into numerical constants, character constants, string constants, and symbol constants. See the following code: const int I = 123; # Representing an integer constant const int I = 033; # const int I = 0x1 for an integer constant in octal format; # const float f = 1.2222 in hexadecimal format; # Single-precision floating point type with fconst double B = 123.1234567d by default; # dual-precision floating point Table d Const char c = 'a'; # const char cn = '\'; # escape by slash 2.2.2.1 see the following code: pirntf ("% d \ n ", 100); # The integer type directly indicates pirntf ("% o \ n", 100); # The octal type indicates pirntf ("% x \ n", 100 ); # indicates pirntf ("% f \ n", 1.1111f) in hexadecimal notation; # indicates a single precision. A single precision constant uses f to represent pirntf ("% f \ n", 1.111111l ); # The Double Precision indicates that only the six digits of the decimal point are displayed with a single precision. The constant is followed by an L. The double precision is 2.3. format the output 2.3.1 and the placeholder output is used. See the following code: int a = 100; float f = 1.123456f; double d = 1.12345678L; char c = 'a'; printf ("% d \ n", A); # output integer a using % d; % u: indicates the unsigned decimal type; % ld indicates a long integer.. The preceding number can be added. For example, % 2d indicates 2 bits; printf ("% f \ n", f); # % f indicates f; % 2f indicates 2 bits; % 2.3f indicates a two-digit integer with three decimal places. %-2.3f is the same as the preceding one but is aligned to the left. The decimal point is not equal to 0. Printf ("% c \ n", c); # Use % c to output the character c. For more information, see this article. 2.4 conditional judgment 2.4.1 operator 2.4.1.1 common operators: Level 1 (), [],., -> In the left-to-right order. Level 2:-(negative number), ++, --, * (value ),&,!, ~ The single object operator is right-to-left. Pay special attention to ++ and -- with distinction between front and rear; sizeof expression; Level 3: *,/, % is calculated from left to right; Level 4: +,-calculate by left to right; Level 5: <,> calculate by left to right; Level 6: >,>=, <, <= is calculated from left to right. Level 7: = ,! = It is calculated from left to right. Some of them are not listed here, such as composite operators. You can refer to this article for details. 2.4.2 condition judgment 2.4.2.1 common condition judgment: int a = 0; # single if judgment if (a = 0) {// when a = 0 operation} # if-else determines if (a = 0) {// operation when a is equal to 0} else {// operation when a is not equal to 0} # if-elseif-else judgment, multiple else if, the final else can also have no if (a = 0) {# operation when a is equal to 0} else if (a = 1) {# operation when a is equal to 1 }... Else {# The preceding condition does not meet the running condition, which is equivalent to the default operation} 2.4.2.3 logical judgment of the Creation type. For details, see: int a = 0; int B = 1; int c = 2; if (a = 0 | B = 2) # because the logic or short circuit is generated (a true result does not really judge whether B is equal to 2) {# operation} if (a = 0 & B = 2) # Because it is logical and it will also generate a short circuit (when one is false, the result will be false and no longer judge whether B is equal to 2) {# operation} if (a = 0 | B = 2 & c = 1) # because the & operator is relatively high, this is equivalent to a = 0 | 0 and the result is true {# operation} 2.4.2.3 switch condition switch (variable) {case A: // program code break; case B: // program code break; case C: // program code brea K; # This break statement will generally exist. Of course, case one and case two are not excluded... Default: # The final default must have a statement even if no statement is available; break;} # The variable cares about the most about the type. Here we will talk about int, char, byte, isn't short just a few of them? The answer is: # ANSI indicates all types. However, in some platforms, the error cannot be of all types, but data that can be converted to integer type can be returned, including enumeration. An error is reported when float is not used !!! 2.5 loop statement 2.5.1 for loop, see the following code: int a = 10; for (int B = 0; B <a; B ++) {printf ("% d \ n", B) ;}# the following can also be written: the following cycle B = 0 settings can be omitted int a = 10; int B = 0; for (B = 0; B <a; B ++) {printf ("% d \ n", B )} # generally, we use the first method 2.5.2 while and do-while (). See the following code: # The preceding for statement can rewrite int a = 10; int B = 0; while (B <a) {printf ("% d \ n", B) ;}# you can also use the do-while () statement, however, the results are different because the statement is first executed for judgment, so it is one more time than the previous loop: int a = 10; int B = 0; do {printf ("% d \ n", B);} while (B <)