Read the C programming language (4)

Source: Internet
Author: User

Chapter 2: types, operators, and expressions. The following are some enlightening points:

  • With regard to type, the author makes a concise summary in one sentence: "The type of an object determines the set of values it can have and what operations can be placed med on it. "the type of an object determines the set of values that can be taken and the operations that can be performed on it.
  • For variable names, many programming standards for Class C advanced languages recommend that the names of class fields start with an underscore (_), but in C, "_" is not recommended for all variable names. The author said, "don't begin variable names with underscore, however, since Library Routines often use such names." Because the library program uses such a name.
  • Integer types include short, Int, and long. How can they be distinguished by their lengths? The author pointed out that "the intent is that short and long shoshould provide different lengths of integers where practical; int will normally be the natural size for a particle machine. "So on the current 32-bit machine, the length of the int type is 32. "Where practical" means that the length of short and long is determined by the compiler, As long as short is no longer than int, Int Is no longer than long. On a 16-bit machine, both short and INT are 16-bit long. Similar to shaping, float, double, and long double are also dependent on specific machines. They can represent one, two, or three different lengths.
  • The prefix and suffix of a constant. The prefixes include 0 (octal) and 0x (hexadecimal), and The suffixes include U (unsigned), L (long), and F (float ). Prefix and suffix can be added at the same time, such as oxful.
  • For example, if '0' is replaced by '48 'instead of an integer constant, the author explains, "if we write '0' instead of a numeric value like 48 that depends on the character set, the program is independent of the participating value and easier to read. "It can be seen that although the constant contains an integer value internally, this value depends on the machine's character set. Character constants can be used to make characters independent from specific character sets and increase program readability.
  • Use the octal and sixteen mechanisms to represent characters in the following format: '/ooo' and'/xhh '. They are all one character and the value is an integer.
  • For '/0', the author mentioned, "the character constant'/0' represents the character with value zero, the null character. '/0' is often written instead of 0 to emphasize the character nature of some expressions, but the numeric value is just 0 ". The value of '/0' is 0, which is written to emphasize its character attribute.
  • The floating point number can also be assigned a value like this: Float EPS = 1.0e-5;, which has never been noticed before.
  • Non-local variables can only be initialized once, and only one constant expression can be initialized. External variables and static variables are initialized to 0 by default, while uninitialized local variables have undefined values.
  • Const can be used on array parameters to prevent changes to the function called by the array. For example, int strlen (const char []);
  • This getbits function is very well written and can be inspired by bit operations:
    /* Getbits: Get N bits from position P */
    Unsigned getbits (unsigned X, int P, int N)
    {
    Return (x> (p + 1-N ))&~ (~ 0 <n );
    }
    Use the inverse operator ~ The program is portable because it is independent of the characters of a specific machine.

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.