C Programming Language (2nd edition • New version) Chapter 2nd type, operator, and expression

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators logical operators alphanumeric characters

Type:Determines the set of desirable values for an object and the operations that can be performed; operatorSpecify the operation; An expressionCombine variables and constants to generate new values. ANSI C Patching:All integral types include signed and unsigned two, floating-point operations can be single-precision or long double type, strings can be concatenated at compile time, enumeration types are supported, const types, and automatic coercion type conversion rules for extended arithmetic types 2.1 Variable nameThe underscore is considered a letter, and the variable name is a sequence of alphanumeric characters beginning with a letter; library function names usually begin with an underscore, do not use; case-sensitive; Traditionally, variable names are lowercase, symbolic constant names are all uppercase, local variables (especially cyclic control variables) are short names, and external variable names are longer 2.2 Data type and length Basic:Char: Character type, occupies one byte, can save one character in local character set;int: Integral type, usually reflecting the most natural length of integers in the machine used;float: single-precision floating-point typedouble: dual-precision floating-pointint can be added qualifier short or long, at which time int can be omitted; The compiler chooses the appropriate length according to the hardware characteristics (short is not longer than int,int), usually short is 16 bits, int is 16 or 32 bits, long is 32 bits; qualifiers signed and unsigned can be added to any of the above integer types and before char. 8-bit char, unsigned range 0~255,signed range -128~127 (on a two-complement machine); Char with no qualifier is signed depending on the machine, but the printable character is always positive;float, double, long double can represent the same length can also represent two or three different lengths;symbolic constants that define the type length above and other machine and compiler related properties can be found in <limits.h> and <float.h>; 2.3 Constants1, 2, 3 and other integer constants are of type int; a large integer outside the int range is treated as long, and the long type ends with L or L, such as 123456789l;ul or UL, indicating unsigned long; decimal 31 = octal 037 = hexadecimal 0x1f or 0x1f; they can also be suffixed with u, L, etc.;123.4, 1.5e-2, etc. are double, plus F or f is float, plus l or L is a long double; (it:float equivalent to short,double equals int, long double equals long)A character constant such as ' X ' is an integer whose value is a number in the machine's character set, which can be compared with other characters and can participate in numeric operations as integers;   escape characters:' \v ', ' \013 ', ' \xb ' (octal (\ooo) and hexadecimal (\xhh) of the Vertical tab), the null character (null) is ' \ s ' and its value is 0, but its property is a character;   constant expression: only constants, such as Char line[2+max+3 after # define MAX 1000, are evaluated at compile time and not at runtime (PS: The same is true for a const variable in a blog)   String constants: sequences of characters, such as "", "u r naive", and so on, enclosed in double quotation marks. It uses \ "to denote double quotation marks; compile time to concatenate multiple string constants, such as" Hello, "" World "equivalent to" Hello, world "; string constant is actually a string array, the internal representation of the" plus "end, so the number of physical storage units more than 1 This also indicates that the length of the string in C is unlimited, and that the length of the string must be scanned; the library function strlen (s) (in) can be the length of s (without ' n ');   Enumeration constants: A list of constant integer values that facilitate the establishment of a constant value with a name, which can be generated automatically (#define not). Example:  Enum Boolean {NO, YES};//is automatically assigned from 0, no=0, Yes=1enum Escapes {BELL = ' \a ', BACKSPACE = ' \b ', TAB = ' \ t '}:enum months {JAN = 1, FEB, MAR, APR};//unspecified value will increment the value;Enum variables can then be defined enum months amonth; then AMonth equals Jan, and the meaning of the Feb is obvious, and other integer values can be assigned. 2.4 Statementvariables must be declared in order to be used (some can be implicitly declared by context), a table of variables can be declared, and expressions are initialized ;a non-automatic variable can only be initialized once and is a constant expression;the external variable and the static variable are initialized to 0 by default;An automatic variable that is explicitly initialized is reinitialized (can be any expression) each time, and an automatic variable value that is not explicitly initialized is undefined (that is, an invalid value);The Const can qualify any variable declaration, the value of the specified variable cannot be modified, and all elements cannot be modified; The following function cannot modify the value of an array element: int strlen (const char[])   2.5 arithmetic Operators+-*/%Two dollars,where the modulo operator% cannot be used for floating-point numbers, with negative operands, the result depends on the machine; 2.6 relational operators and logical operators> >= < <= = =! =lower priority of the following two;&& above | | ;! logical non, generally with if (!valid) without if (valid = = 0); 2.7 Type ConversionsIn General, the automatic conversion of the operand "widened" without loss of information, meaningless expression will be an error (such as floating point to do subscript); An expression that may lose information (such as assigning a long integer value to a short integer) warns but is legal;<ctype.h> defines a set of test and conversion functions independent of the character set (such as ToLower (c), IsDigit (c), etc.);C does not specify whether Char is signed (but the printable character is guaranteed to be non-negative), and the conversion to int is related to the machine;if, while, the test part of for, "true" and "not 0" equivalent;There are many implicit arithmetic type conversions in C: the "lower" type is promoted to the "higher" type when the two operands are different, the unsigned type is cumbersome, the machine is related, and the right value is shifted to the left type when the value is assigned; Char, short is always converted to int, Therefore, the mutual assignment may lose information; float to int will intercept; double turn float intercept or round depends on the machine; Coercion type conversion : (type name) expression, which is a unary operator with the same precedence as the other unary operators; the function call will automatically force type conversion;   2.8 Self-increment, auto-decrement operator++n:n value increased by 1 after use, n++:n value is used after 1;--similar; 2.9 Bitwise Operators6, can be used for the integral type, namely signed or unsigned;& | ^ << >> ~commonly used to build Shield code (It: operands are counted as binary operations)。Shift left 0;shift right to unsigned 0, signed depending on the machine ("Arithmetic shift" of the complement sign bit and "logical shift" of the zero complement) 2.10 Assignment operators and expressionsop=, where op can be + - * / % << >> & ^ | An assignment expression has a value, and the type and value are the result of the left operand; 2.11-Bar expressionexpr1? EXPR2:EXPR3 2.12 Operator Precedence and evaluation order1 brackets, Access structure: () [], 2 front:! ~ + +--+-* & (Type) sizeof3 arithmetic-multiplication modulo: */%4 arithmetic-plus minus: +-5 shift: << >>6 Relationship-inequality : < <= > >= 7 Relationship-equality: = =!=8 bitwise-With: &9 bitwise-XOR: ^10 bitwise-OR: |11 logic-With: & Amp;&12 logic-or: | | 13-piece expression:?: 14 Assignment expression: = + = = *=/=%= &= ^= |= <<= >>=15 comma expression:, where:2 and 13 of the binding is from right to left, others are from left to right;the four operators (&& | |?:) have a specified sequence of operations, other sequence of operations and the Order of evaluation of function parameters are not specified, depending on the compiler, because the optimal order of evaluation with the machine structure has a great relationship; Recommendation: Avoid special implementations unless you know the machine;

C Programming Language (2nd edition • New version) 2nd Chapter type, operator, and expression

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.