Summary of basic issues in C Language

Source: Internet
Author: User

1. printf format output function if the number of format control items is greater than the number of output columns, error data is output. If the number of output columns is greater than the number of format control items, the number of output columns is not output. % Md, m indicates the width of the output field. If the number of digits of the output field is smaller than m, the left end is filled with spaces. If the number is greater than m, the output is based on the actual number of digits. %-Md, basically the same as above, except that the space on the right side can be a constant, variable or expression, and the value is calculated from the right to the left in VC ++ 6.0, output from left to right, for example, int x = 5; printf ("% 4d % 4d % 4d", x, ++ x, ++ x); outputs 7, 7, 6. instead of 5, 6, and 7, note that different compilers may output different results and convert the gcc compilation results to 7, 7, 72, and 0-9 numbers to m, then m + '0' is m character form 'M' 3, lowercase letters into uppercase letters char c; c is lowercase letters, c-'A' + 'A' is the corresponding uppercase letter 4. If the switch finds a matched case entry, it executes the following statement. After the statement is executed, it does not exit like an if statement. if a break statement is not encountered, all the subsequent case statements will be executed one by one without any conditional judgment. The statement after the case entry can be either a sentence or multiple sentences without braces. 5. character array storage string when char str [5] = new {"china"};, the program will have a problem, and garbled characters will appear behind china in the output, this is because there is another '\ 0' behind the china string, so we should add one more character to the str array. That is, char str [6] = new {"china"}; and '\ 0' only indicates the end of the string and will not be output. Scanf ("% s", str); cannot be saved as a space because it indicates the end of the string. Gets (str); spaces can be added to the string. Puts (str); after the output string, line feed is automatically added. 6. String operation function: strcy (str1, str2); copy str2 to str1. String concatenation function: strcat (str1, str2); connects str2 together with '\ 0' to the end of the last character (not' \ 0') of str1, and the result is placed in str1. String comparison function: strcmp (str1, str2); compares the sizes of str1 and str2. If str1 = str2, 0 is returned. If str1> str2, a positive integer is returned; if str1 <str2, a negative integer is returned. String Length function: strlen (str); returns the actual length of str, excluding '\ 0' at the end '. 7. function parameters and one-way value passing function parameters are divided into real parameters and form parameters. The form parameter appears in the function definition and is used in the entire function body. It cannot be used without the function body. Real parameters appear in the main function. After a function is called, real parameters cannot be used. The system allocates memory units only when the parameters are called. At the end of the call, the compilation system immediately releases the allocated memory units. Therefore, the parameters are valid only within the function, after a function call ends, it is returned to the primary function and cannot be used any more. One-way value passing: only the values of real parameters can be passed to the form parameter, and the values of the form parameter cannot be passed back to the real parameter, which is called one-way value passing. Therefore, when a function is called, the value of the form parameter is changed, and the value of the real parameter is not changed. 8. The array name as the parameter array can be used as the real parameter of the function. In this case, the form parameter can be an array or a pointer. The length is not specified when the parameter is a one-dimensional array. When the parameter is a two-dimensional array, the size of the first dimension can be omitted. You must specify the size of the Second-dimensional array. 9. Storage of variables (1) local variables during each function call, the system will re-allocate memory units for them in the dynamic storage area of the memory. With the frequent calls of functions, the storage location of a variable will change with the running of the program, so the value of the unassigned local variable is uncertain. Partial Variables in a function cannot be returned because they are recycled after the function is completed. (2) static variables of the static type are allocated memory and assigned initial values only once during compilation. If the initial value is not assigned to a static variable, the system automatically assigns the initial value 0 (or '0 '). Static variables occupy a fixed memory unit in the memory static storage area. Even if the function call ends, the storage unit will not be released and its value will be retained. This value will continue to be used in the next call. Static variables include static local variables and static global variables. Static global variables are static variables defined in the function body, and static local variables are static variables defined in the function body. As follows: # include <stdio. h> void f () {static int a = 0; // The initial value is assigned during compilation, and only the initial value is assigned once +; printf ("%-2d", a);} main () {f ();} the output result of the above program is 1 2 3 because the initial value assigned to the static variable is completed during compilation and only assigned once. After that, the initial value assignment operation will not be executed when the function is called, therefore, output 1 2 3. If the static keyword is removed, the result will be changed to 1 1 1. As a result, if the function is called multiple times, the local variable will be assigned an initial value each time, static variables only assign initial values when they are called for the first time. Note: in java, there are no static local variables. Only static global variables for classes are available. (3) register Type Definition form: register data type variable name; register Type local variables have the same scope and lifetime as local variables. The number of registers is limited, and the number of data records stored in registers is limited. Therefore, there cannot be too many register-type variables, and Integer Variables and register-type variables can be defined as register-type local variables. The current optimization system can automatically identify and store relevant variables in registers. (4) differences between external variables 10, compilation preprocessing # include <File Name>, and # include "file name" are as follows: When angle brackets are used, compile the Preprocessing Program only in the folder specified by the system. Use double quotation marks to compile the Preprocessing Program first in the folder where the current file is located. If the prefix program cannot be found, in the folder specified by the system. 11. Both & and * priorities belong to Level 2. Operations 12, *, ++, and -- from right to left all belong to level 2, from the right to the left, * p ++ is equivalent to * (p ++) * ++ p is equivalent to * (++ p) 13. [] and * [] have a higher priority than * 13. The row address and column address of a two-dimensional array are int a [2] [2] = {1, 2, 3, 4}. Then a is the first address, the first address of the first line; * The address a + 1 of the first element of the first line is the address of the second line * (a + 1) it is the first element address of the second row, and a [1] is also the first element address of the second row 14. The pointer variable pointing to the array (array pointer) int * (p) [4]; indicates a pointer to an array containing four int elements. (1) p points to the first line of the one-dimensional array address main () {int a [2] = {}; int (* p) [2]; p = &; // p is the pointer to the array, that is, the row pointer. Therefore, use & a to assign a value to it, and & a is the array address printf ("% d \ n", ** p ); // take the first element, where p is the row address, * p is the element address, and ** p is the element value printf ("% d \ n ", * (* p + 1); // obtain the second element. // You can also set the value to printf ("% d \ n", (* p) [0]). // (* p) can replace a printf ("% d \ n", (* p) [1]);} (2) p points to the first line of the Two-dimensional array address main () {int a [2] [2] = {1, 2, 3, 4}; int (* p) [2]; p =; // p points to the first line address of the Two-dimensional array printf ("% d \ n", ** p); // * p is the element address, ** p indicates the element content printf ("% d \ n", * (p + 1) + 1);} 15. pointer array in T * p [4]; indicates that an array contains four int pointers. Char * p [4]; indicates that an array contains four char-type arrays, or the addresses of four strings 16 and two-dimensional arrays. int a [2] [3]; then a is the first line address, * a is the first element address of the first line. ** the value of a + 1 is the second line address, * (a + 1) is the address of the first element in the second row. ** (a + 1) is the value of the first element in the second row * (a + 1). It is equivalent to a [1: for example, main () {int a [2] [2] = {1, 2, 3, 4 }; printf ("a = % d \ n", a); printf ("* a = % d \ n", * ); printf ("** a = % d \ n", ** a); printf ("* a + 1 = % d \ n", * a + 1 ); printf ("a + 1 = % d \ n", a + 1); printf ("a [1] = % d \ n", a [1]); printf ("a [1] + 1 = % d \ n", a [1] + 1); printf ("* a [1] = % d \ n ", * a [1 ]);} 17. When the character pointer variable accepts the input string, it must first open up the bucket char * cp; scanf ("% s", cp); The above is incorrect. Can be changed to: char cp [20]; scanf ("% s", cp); or char * cp, str [20]; cp = str; scanf ("% s ", cp); in short, you must first open up space and then accept the return values in string 18, c, and c ++ cannot be arrays, java return values can be arrays 19, pointer functions, and function pointer functions: int * function (); function pointer: int (* p) (); int max (int, int B); p = max; int a = (* p) (2, 3); 20. The local variables defined in the Life Cycle Function of variables cannot be returned, because after the function is completed, local variables are recycled. 21. A struct can be nested, but not itself. The definition of the member struct must be prior to the primary struct. 22. The mallocmalloc function is located in stdlib. in h, the function prototype is void * malloc (unsigned size); eg: struct student * p = (struct student *) malloc (sizeof (struct student )); because malloc returns a void pointer, forced conversion is required. 23. free this function is prototype void free (void * ptr) to release the dynamically allocated memory space directed by the pointer ptr. To ensure the effective use of a dynamic storage area, a storage space should be released in time when it is no longer used. 24. struct and shared body (1) struct can be used as function parameters and return values. Struct values can be directly assigned in the form of braces {} Only during initialization. When the struct is declared first and then assigned values, values can be assigned only to a single element and cannot be used in the form of braces. This is similar to assigning values to arrays. Example: struct student {int bh; char * name ;}; struct student stu = {1, 'typ '}; // It is correct, but the following is the wrong struct student stu; stu = {1, "typ"}; // It is wrong. In this case, this can be stu. bh = 1; stu. name = "typ" (2) The shared object cannot be used as a function parameter and return value. The shared object cannot be stored at the same time, and only one member can be stored at each time, the last stored member is a valid member. The size of the shared body is the size occupied by the largest element. The shared body can appear in the struct type, struct can also appear in the shared body type 25. Enumeration type enum color {red, green, blue}; enum color c = red; int I = red; // The value is 026. The type identifier is redefined. in C language, the keyword typedef is used to declare the new type name typedef int INTEGER; INTEGER x, y; equivalent to int x, y; for example, struct definition: typedef struct {int num; char name [10]; float score;} student; student stu1, stu2, * s; in addition, typedef only performs type redefinition, only name an alias for this type, and do not generate new data types 27. bitwise operations include (same, Or, different, or inverse ). During bitwise operation, the number is calculated in the form of a complement code, and the symbol bit is involved in the operation. Exclusive or: 0 for the same value, 1a ^ a = 0; a ^ 0 = a; a ^ ~ A = 1; here we can use an exclusive or to exchange two numbers. a = a ^ B; B = B ^ a; a = a ^ B; this avoids the introduction of temporary variable 28, shift operation (1) a <B, indicating that the binary value of a shifts left B (2) a> B, it indicates that the binary value of a is shifted to the right, and the B displacement bit operation has three forms: (1) cyclic shift: The migrated bit is equal to the removed bit (2) logical shift: The removed bit is lost, 0 (3) arithmetic shift: The removed bits are lost, the left shifted bits are 0, and the right shifted bits are signed bits, the shift operation of the symbol bit remains unchanged in C language is related to the specific C compilation system. For example, VC ++ 6.0 uses arithmetic shift. Note: The Shift operation does not change the value of the original operand. For example, after the> 2 operation, the value of a remains unchanged unless a = a> 2 is assigned to change the value of. 29. File (1) the file in C language is a byte stream file. (2) In C, the User-Defined FILE type is FILE, the FILE type is struct type, and the FILE structure is a structure defined by the keyword typedef. Struct _ iobuf {char * _ ptr; int _ cnt; char * base; int _ flag; int _ file ;.........}; typedef struct _ iobuf FILE; (3) Open and Close a FILE pointer = fopen ("FILE path \ FILE name", "FILE Operation Method"); operations can be divided into r, w, a, r +, w +, a + If fopen fails to be opened, NULL is returned. If the buffer zone is less than B, data is not written to the disk. If the program ends abnormally, data in the buffer zone is lost, the file is incomplete. Only when you close an opened file can you forcibly write less than B of data in the buffer to a disk file to ensure file integrity. The fclose function is used to close the file fclose (File pointer). The returned value is an integer. If it is 0, the file is closed normally. Otherwise, the file cannot be closed normally. (4) the input and output of a file read and write a character: char fgetc (File pointer); EOF fputc (character, file pointer) read and write a string: fgets (string s, number of characters read n, file pointer) ---> \ n or EOF stop in the middle, read n-1 characters, add '\ 0' at the end; fputs (string, file pointer) ---> the end mark of the string will not be written into the file. Format: fscanf (fp, "% d % s", & I, s) ---> read data from the file and save it to the variable; fprintf (fp, "% d % c", j, c) ---> write data to the file in the specified format into blocks for read/write: fread (buffer, size, count, fp) and fwrite (buffer, size, count, fp) buffer is a pointer, fread () indicates the first address of the variable that stores "input data", fwrite () the first address size of the variable that stores "output data" indicates the number of bytes of the data block. count indicates the number of data blocks. fp indicates the return value of the file pointer. All values are count (4). the feof (fp) function of other file operations judges the end mark of the file and returns 1 at the end. Otherwise, 0 rewind (fp) is returned for locating, is the start of the file returned by the position pointer of the file. Fseek (fp, offset, base) is used to control pointer movement within a file. Base is the benchmark for location movement. Offset is the offset ftell (fp) used to obtain the position of the position pointer, relative to the beginning of the file.

Related Article

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.