You have to know 495 C language questions, learn to experience a

Source: Internet
Author: User

C language as an ancient language, its flexibility and error-prone people love and hate, the book "You Must know the 495 C language Problem", using the form of question and answer, tell the reader C language use of all aspects of knowledge, including some cold knowledge and so on. Below, I would like to extract and collate some of the more important knowledge I think to share.

Knowledge One: Declaration, definition and initialization

1. With respect to int and long, it is well known that the C language standard does not specify the size of the standard type, especially for the int type, on very many 16-bit machines, the int type is actually 16 bits, and on the 32-bit machine, int is 32 bits, in fact, The int type represents the natural word length of the machine, which is of course the choice of multi-shaper variables, and the standard type names int16_t and int32_t are defined in the standard header file, respectively, representing the two word lengths. The long is always 32 bits long, which is 64 bits. However, in the 64-bit machine era, the long type has become 64 bits, and the int itself still maintains the word length of 32 bits.

2. Declaration of Distinction and definition, declaration or Definetion

The explanations in the book are as follows: first, although a global variable or function can be "declared" multiple times (in multiple compilation units), the definition can occur at most once, and for global variables, the definition is a declaration that really allocates space and assigns an initial value (if any), and for a function, the definition is a "declaration" of the function body

When you want to share variables or functions in multiple source files, you need to ensure that the definitions and declarations are consistent, and the best arrangements are defined in a related. c file and then externally declared in. h. When you need to use it, just include the corresponding header file.

It's a good idea to define a global variable in. h

3. Storage type

    • Auto
      Outdated keywords, now c++11 in the new, of course, this is something.
    • Static: Magic One, written interpretation is static, not development, change the meaning of this keyword I used a long time to figure out what exactly is the meaning of a lot of written answer is very high-end, rote answer, in fact, is very sad. It, we can explain from the storage location and use location:

      • Modifying a global variable or function inside a. c file, stating that its modified variables or functions are valid only in this file, and that external files cannot be called, which prevents conflicts with the names of other compilation units, where they are stored in the static storage area
      • In the body of the function, the variable is not affected by the stack and stack of functions, it can keep its value unchanged, if it is manipulated in the function, the effect will persist, so it is worthwhile to modify it only within the body of the function.
      • Appears in C + + before the member variables and member functions of the class.
    • Const
      Magic Two, remember is not the meaning of the constant, can be understood as read-only , you can see volatile and const comprehensive analysis

4. Complex declarations

This is the source of the pain of C people.
such as declaring char * (* (*a[n]) ()) ();

What the hell is this?
Below, please follow the "from inside to outside" understanding way to understand:

Remember: [] and () higher than * priority

    • First layer: A[n] An array of N elements
    • Second layer: *a[n] Element type is a pointer
    • Layer three: (*a[n]) () The pointer is a function pointer
    • Layer Fourth: (* (*a[n]) ()) () The function returns a pointer to a function
    • Layer Fifth: char * (* (*a[n]) ()) () The type of the pointer is a function, and the return value of the function is char*

Then look at our common function pointer type, it is very simple, such as:

char * (*PF) (double* dd,int N);

    • First layer: *PF a pointer
    • Second layer: (*PF) (double* dd,int N) function pointer for double*dd, int n
    • Third layer: The return value of this function is char *

Visible, the final type of a declaration is specified by the type of the first layer. Now if you can't figure out the function pointer and pointer function, it's not justified.

So, we're going to parse a complex statement, how do we declare it ourselves?
Here, I recommend using typedef

Or the top example, how do we use typedef to make statements?

typedefchar* pc; // declares a pointer to the char* typedef pc FPC (); // declares a function with a return value of char* typedef FPC *PFPC; // declares a pointer to the function that returns a value of typedef PFPC FPFPC (); // declares a function typedef FPFPC *PFPFPC with a return value of the pointer above ; // declare more than one type of function pointer pfpfpc a[n]; // declares an array whose type is the above function pointer

In actual coding, it is not possible to have so many layers of declaration, in general, we are both the declaration of two layers, or a function pointer example, we can declare:

Typedefchar* (*PF) (double*dd,int  n);p f REPF;

5. Initialization

In the actual encoding, forgetting the initialization, or initializing the error caused the problem is still very serious.
It is recommended that all the variables in the zone are explicitly initialized manually, so don't be lazy.

You have to know 495 C language questions, learn to experience a

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.