495 C language problems that must be understood

Source: Internet
Author: User

1.1 how can I determine the integer type?


If you need a large value (greater than 32,767 or less than half 32,767), use the long type. Otherwise, use the short type if the space is very important (such as a large array or many structures. In addition, int type is used. If strictly defined overflow features are important and negative values do not matter, or you want to avoid symbol extension problems when operating binary bitwise AND node, use the corresponding unsigned type. However, be sure to mix signed and unsigned values in the expression.


Although the character type (especially the unsigned character type) can be used as a "small" integer, this may not be worth the candle due to unpredictable symbol extension and code increase. It is helpful to use the unsigned attention type. For more information, see question 12.1.


There is a similar trade-off between the floating point type and the double precision floating point type. However, if the pointer of a variable must be of a specific type, the above rules are no longer applicable.


If for some reason you need to declare a variable with a strict size, make sure that this option is encapsulated with a proper typedef like <inttypes. h> of C99. Generally, the only good reason for doing so is to try to comply with an externally imposed storage solution. See question 20.3.


If you need to operate super large variables that are supported by the built-in types of C, see question 18.17.


Reference: [K & R1, Sec. 2.2 p. 34]; [K & R2, Sec. 2.2 p. 36, Sec. a4.2 pp.195-6, Sec. b11 p. 257]; [ISO, Sec. 5.2.4.2.1, Sec. 6.1.2.5]; [H & S, Secs. 5.1, 5.2 pp.110-114].


1.2 What is the 64-bit type on a 64-bit machine?

The C99 standard defines the long type, which can be at least 64 bits in length. It takes some time to implement this type in Some compilers. Other compilers implement extensions similar to longlong. On the other hand, 16-bit short integer, 32-bit integer, and 64-bit long integer can also be implemented. Some compilers just do this.

 

See question 18.17.


Reference: [C9X, Sec. 5.2.4.2.1, Sec. 6.1.2.5]


1.3 How is it best to define and declare global variables and functions?


First, although a global variable or function can (in multiple compilation units) have multiple "declarations", the "Definition" can only appear once. Definition is the declaration of allocating space and assigning initial values (if any. It is best to define it in a related. c file, and then make an external declaration in the header file (. h). When necessary, you only need to include the corresponding header file. The. c file that defines variables should also contain this header file so that the compiler can check the consistency between definitions and declarations.


This rule provides a high degree of portability: it is consistent with the ansi c standard and compatible with most compilers and connectors before ANSI. Unix compilers and connectors generally use the "general mode" to allow multiple definitions, as long as they can be initialized at most one. ansi c calls this behavior a "public extension ", there is no meaning of double-pass.


You can use preprocessing techniques to make statements similar to DEFINE (int, I); appear only once in a header file, then convert a macro into a definition or statement as needed. But it is unclear whether such troubles are worth it.


If you want the compiler to check the Declaration consistency, you must put the global declaration in the header file. In particular, never put the prototype of an external function in the. c file: Usually the consistency between the external function and the definition cannot be checked, and the conflicting prototype is worse than not.


See question 10.4 and question 18.6.


Reference: [K & R1, Sec. 4.5 pp. 76-7]; [K & R2, Sec. 4.4 pp. 80-1]; [ISO, Sec.6.1.2.2, Sec. 6.7, Sec. 6.7.2, Sec. g.5.11]; [Rationale, Sec. 3.1.2.2]; [H & S, Sec. 4.8pp. 101-104, Sec. 9.2.3 p. 267]; [CT & P, Sec. 4.2 pp. 54-56].


1.4 What does extern mean in function declaration?

 

It can be used as a format prompt to indicate that the function definition may be in another source file, but there is no substantial difference between extern int f (); and int f.


References: [ISO, Sec. 6.1.2.2, Sec. 6.5.1]; [Rationale, Sec. 3.1.2.2]; [H & S, Secs. 4.3, 4.3.1 pp. 75-6].


1.5 What is the purpose of the keyword auto?


Useless; it is out of date. See question 20.32.


Reference: [K & R1, Sec. a8.1 p. 193]; [ISO, Sec. 6.1.2.4, Sec. 6.5.1;]; [H & S, Sec. 4.3 p. 75, Sec. 4.3.1 p. 76].


1.6 it seems that I cannot successfully define a linked list. I tried typedef struct {char * item; NODEPTR next;} * NODEPTR; but the compiler reported an error. Can a structure in C Language contain pointers to itself?


The structure in C can include pointers to itself. The discussion and examples in [K & R2, section 6.5] show this. The problem with the NODEPTR example is that typedef is not defined when the next field is declared. To solve this problem, first assign the structure a label ("struct node "). Then, declare the "next" field as "struct node *", or separate the typedef definition and structure definition, or both adopt them. The following is a modified version:
Struct node {
Char * item;
Struct node * next;
};
Typedef struct node * NODEPTR;


There are at least three ways to solve this problem.


Similar problems may occur when two structures referenced by each other are defined using typedef, which can be solved in the same way.


See question 2.1.


Reference: [K & R1, Sec. 6.5 p. 101]; [K & R2, Sec. 6.5 p. 139]; [ISO, Sec.6.5.2, Sec. 6.5.2.3]; [H & S, Sec. 5.6.1 pp. 132-3].


1.7 How to establish and understand complex statements? For example, define an array containing N pointers to the functions that return pointers to characters?


There are at least three answers to this question:


1. char * (* a [N]) ();


2. Use typedef to gradually complete the Declaration:
Typedef char * pc;/* character pointer */
Typedef pc fpc ();/* function that returns the character pointer */
Typedef fpc * pfpc;/* pointer of the above function */
Typedef pfpc fpfpc ();/* function that returns the function pointer */
Typedef fpfpc * pfpfpc;/* pointer of the above function */
Pfpfpc a [N];/* array of pointer above */


3. Use the cdecl program, which can translate English into C or C into English:
Cdecl> declare a as array of pointer to function returning

Pointer to function returning pointer to char

Char * (* a []) ()
Through type conversion, cdecl can also be used to explain complex declarations and indicate which pair of parentheses the parameter should enter (as in the preceding complex function definition ). See question 18.1.

A good C language book will explain how to "from inside to outside" interpret and understand such a complex C language statement ("simulate declaration use ").


The function pointer declaration in the preceding example does not include parameter type information. If the parameters have complex types, the Declaration will become truly confusing. The modern cdecl version can help.

 

Reference: [K & R2, Sec. 5.12 p. 122]; [ISO, Sec. 6.5ff (esp. sec. 6.5.4)]; [H & S, Sec. 4.5 pp. 85-92, Sec. 5.10.1 pp. 149-50].


1.8 The function is defined only once and called once, but the compiler prompts that the definition is invalid.


The function called without declaration within the range (probably before the function definition for the first time) is considered to return an integer (int) (and there is no parameter type information ), if a function is declared or defined as another type later, a conflict may occur. All functions (non-integer functions must) must be declared before calling. Another possible cause is that the function has the same name as another function declared in a header file.


See questions 11.4 and 15.1


Reference: [K & R1, Sec. 4.2 p. 70]; [K & R2, Sec. 4.2 p. 72]; [ISO, Sec. 6.3.2.2]; [H & S, Sec. 4.7 p. 101].


1.9 what is the correct definition of main? Is void main () correct?


Refer to Question 11.11 to question 11.16. (This definition is incorrect ).


1.10 What can be assumed for the initial values of variables without initialization? If the initial value of a global variable is "zero", can it be used as a null pointer or floating point zero?


Uninitialized variables with "static" lifetime (that is, variables declared outside the function and variables with static storage type) can ensure that the initial value is zero, just as a programmer typed "= 0. Therefore, if these variables are pointers, they will be initialized to the correct NULL pointer, and if they are floating point, they will be initialized to 0.0 (or the correct type, see Chapter 5th ).


Variables with "automatic" lifetime (that is, local variables without static storage) that are not explicitly initialized include junk content. No assumptions can be made for junk content. These rules also apply to arrays and structures (referred to as "aggregates"); for initialization, arrays and structures are considered as "variables ".


The memory dynamically allocated with malloc () and realloc () may also contain junk data, so it must be correctly initialized by the caller. The memory obtained using calloc () is completely zero, but this is not necessarily useful for pointers and floating point values (see Chapter 7.26 and Chapter 5th ).


Reference: [K & R1, Sec. 4.9 pp. 82-4]; [K & R2, Sec. 4.9 pp. 85-86]; [ISO, Sec.6.5.7, Sec. 7.10.3.1, Sec. 7.10.5.3]; [H & S, Sec. 4.2.8 pp. 72-3, Sec. 4.6 pp. 92-3, Sec. 4.6.2 pp. 94-5, Sec. 4.6.3 p. 96, Sec. 16.1 p. 386.].

1.11 code int f () {char a [] = "Hello, world! ";} Cannot be compiled.


You may be using an ANSI compiler and do not support initialization of "automatic aggregation" (non-static local array, structure, and union. See question 11.28.


1.12 what is the problem with initialization? Char * p = malloc (10); the compiler prompts "invalid initialization" cloud.


Is this statement a static or non-local variable? Function calls can only appear in the initial formula of automatic variables (I .e. local non-static variables.


What is the difference between initialization for less than 1.13? Char a [] = "string literal"; char * p = "string literal"; when I assigned a value to p [I], my program crashed.


There are two slightly different usage of string constants. Used as an array initial value (as in the declaration of char a []), which specifies the initial value of Characters in the array. In other cases, it is converted to an unnamed static character array, which may be stored in read-only memory, which may cause it to be modified. In an expression environment, an array is usually converted to a pointer immediately (See Chapter 1). Therefore, the second declaration initializes p to the first element pointing to an unknown array.
To compile the old Code, some compilers have a toggle that controls whether a string can be written.


See questions 1.11, 6.1, 6.2, and 6.6.


Reference: [K & R2, Sec. 5.5 p. 104]; [ISO, Sec. 6.1.4, Sec. 6.5.7]; [Rationale, Sec. 3.1.4]; [H & S, Sec. 2.7.4 pp. 31-2].


1.14 I finally cleared the declaration method of the function pointer, but how can I initialize it?


Use the following code
Extern int func ();
Int (* fp) () = func;


When a function name appears in such an expression, it is "transformed" into a pointer (that is, the address is implicitly taken out), which is similar to the behavior of the array name.


The display declaration of a function usually needs to be known in advance (maybe in a header file ). Because there is no implicit external function declaration here (in the initial formula, the number of letters is not part of a function call ).


See question 1.8 and question 4.8.

 

More detailed "must understand the 495 C language problem" http://share.eepw.com.cn/share/download/id/89420

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.