C-language note types, operators, and expressions

Source: Internet
Author: User
Tags bitwise operators integer division



C-language note types, operators, and expressions

General:

Variables and constants are the two basic data objects that a program handles. Declaration statements describe the name and type of a variable, or you can specify the initial value of a variable. operator to specify the action to be made. Expressions combine variables with constants to generate new values. The type of the object determines the collection of desirable values for the object and the operations that can be performed on the object.


First, the name of a variable and a constant
1, the name is a sequence of letters, underscores and numbers, the first character must be a letter, the underscore "_" is considered a letter, but generally do not start with the underscore "_".
2. The names are case-sensitive and the variable names use lowercase letters. Symbolic constant names all use uppercase letters, and the words are separated by underscores.

Experience : Choose a variable name to be able to literally express the purpose of the variable. Local variables typically use shorter variable names (especially loop control variables), and external variables use longer names.


Ii. Types of data
The C language provides the following 4 basic data types:

Char           //character, occupies one byte int            //integer, usually reflects the natural length of integers in the machine used float//          single-precision floating-point double         //double-precision floating-point type

Data type qualifier:
1, short, and long two qualifiers are used to qualify integers, and the keyword int can be omitted, and is usually used to do so.

Short sh;          Short int sh;long  counts;      Long  int counts;

2. The type qualifier signed and unsigned can only be used to qualify char type and any integral type.
3, the long qualifier can be used for double-precision floating-point types, and a long double indicates high-precision floating-point numbers.


Three, constant
1, Integral type constant
An integer constant similar to 120 is of type int;
Constants of type long end with the letter L or L, such as 120680L;
Unsigned constants end With the letter U or U.


2, floating-point constant
A floating-point constant contains either a small number (such as 380.5) or an exponent (3.805e+2), or both. A floating-point constant with no suffix is a double type. The suffix f or f represents the float type, and the prefix L or L indicates a long double type.

Note: integers, in addition to decimal notation, can also be represented in octal or hexadecimal notation, with prefix 0 representing octal 037 (such as decimal 31), with a prefix of 0x or 0X representing a hexadecimal 0x1f (such as decimal 31).


3, character constant amount
A character constant is an integer that enclose characters a word in single quotation marks (such as ' 0 ') when writing. The number of characters in the machine character set is the character constant value, such as ' 0 ' in the machine character set of the value is 48, it does not matter with the number 0; Again, the value of ' A ' in the machine character set is 65.

Note: character constants are generally used to compare with other characters, but can also participate in numeric operations like other integers. The character constant ' \ s ' represents a value of 0, which is the null character (NULL). We usually replace 0 with the form ' \ ' to emphasize the character properties of some expressions, but with a numeric value of 0.


4. String constants
String constants are also called string literals, which are sequences of characters consisting of 0 or more characters enclosed in double quotation marks.

A string constant is an array of characters, and the internal representation of the string uses a null character, '% ', as the end of the string. Therefore, the number of physical storage cells that store a string is one more than the number of characters enclosed in double quotes.

Note: The C language has no limit to the length of the string, and the program scans to the '% ' terminator to indicate that the string is complete. A string constant stores a static storage area.

The standard library function strlen (char *str) returns the length of the string Str (that is, the number of characters before a null terminator).
A version of the Strlen function is designed below:

int strlen (const char s[]) {     int i = 0;     while (s[i]! = ' + ')     {  ++i;     }     return i;}

5. Enumeration constants
An enumeration is a list of constant integer values.

Reserved business type enum rtbpub_resvype{     resvype_ib        = 0,     //Consolidated account     Resvype_oce       = 1,     //online billing     RESVYPE_HB        = 2,     //offline billing     Resvype_ocecharge = 5      //online billing recharge};

(1) An enum is a collection in which the elements (enumeration members) are named integer constants, separated by commas "," between the elements.
(2) Rtbpub_resvype is an identifier that can be regarded as the name of the collection, an optional, or optional item.
(3) If the enumeration is not assigned a value, the default value of the first enumeration member is 0 of the integer type, and the value of the subsequent enumeration member is added 1 on the previous member.
(4) You can set the value of an enumeration member to customize an integer within a range.
(5) Enum type is an alternative to the pre-processing directive # define.


6. Constant expression
A constant expression is an expression that contains only constants, which are evaluated at compile time rather than at run time.

#define MAXLINE 1000char G_szline[maxline + 1];


Iv. Declaration
A variable must be declared before it is used, and a declaration specifies a variable type. A variable can be initialized at the same time, in a declaration, if the variable name is immediately followed by an equal sign and an expression, which acts as an initialization expression that initializes the variable.

int i = 0;int Ilen = MAX + 100;

Note: The declaration statement describes the name and type of the variable, or it can specify the initial value (initialization) of the variable.
The declaration of any variable can be qualified with the Const qualifier, which specifies that the value of the variable cannot be modified, and that the const qualifier specifies that all element values of the array cannot be modified.

const int PI = 3.14;const char msg[] = "00000"; int strlen (const char s[]);

Five, arithmetic operators
Binary arithmetic operators include: +,-,*,/,% (modulo operator).

Note: integer division intercepts the fractional part of the result. The result of the expression x% y is the remainder of x divided by Y, when x can be divisible by Y, its value is 0, and the modulo operator% cannot be applied to the float and double types.


Vi. relational operators and logical operators
1. Relational operators include the following operators:
> >= < <=
Note: They have the same priority.

There are two equal-sex operators (special relational operators) = = =, which is second only to relational operators.
Note: relational operators have lower precedence than arithmetic operations.


2. The logical operation consists of the following two operators:
&& | |
There are some special attributes of logical operations, && and | | The concatenated expression is evaluated from left to right, and stops the calculation immediately after knowing that the result value is true or false.
Precedence ratio of operator && | | Has a high priority, but both are lower than the precedence of relational operators and equal operators.

Note: by definition, in a relational expression or logical expression, if the relationship is true, the value of the result value of the expression is 1, and if False, the value of the result is 0. Logical non-operator! is to convert a non-0 operand to 0 and an operand of 0 to 1.

 VII. Type conversion
When several operand types of an operator are different, it is necessary to convert them to some common type through some rules.
In general, auto-conversion refers to converting a "narrower" operand to a "relatively wide" operand without losing the conversion of the information.
1. Because the char type is a smaller integer, the variable of type char can be used freely in the arithmetic expression, and the character type will be converted to an integral type.
/* Atoi Function: Converts the string s to the corresponding integer number */int atoi (const char s[]) {    int i;    int n = 0;    for (i=0; s[i]>= ' 0 ' && s[i]<= ' 9 '; i++)    {n = ten * n + (s[i]-' 0 ');    }    return n;}

expression s[i]-' 0 ' can calculate the numeric value of the character stored in s[i] because the ASCII table character is connected.
in 2, C, in many cases, implicit arithmetic type conversions are performed.
In general, if the two operations of the two-tuple operator have different types, then the "lower" type is promoted to the "high" type before the operation.
Rules:
If one of the operands is of type long double, the other operand is converted to a long double type.
If one of the operands is of type double, the other operand is converted to a double type.
If one of the operands is of type float, the other operand is converted to the float type.
If one of the operands is of type long, the other operand is converted to a long type.
Converts the operands of char and type short to the int type. Note: in general, mathematical operations use a double-precision variable of type.
3, the assignment is also a type conversion, the value of the right of the assignment operator needs to be converted to the type of the left variable, the type of the left variable is the type of the assignment expression result.
4. Any expression can use a unary operator called coercion type conversion to force an explicit type conversion. 
(Type-name) expression;

Eight, self-increment and decrement operators
The C language provides two special operators for variable increment and decrement, and the increment operator + + increments its operand by 1;the decrement operator--its operand is decremented by 1. + + and--these two operators are special in that they can be used either as prefix operators (used in front of variables, such as ++n) or as postfix operators (used behind variables, such as n++), in which case the effect is to add 1 to the value of the variable N. However, there is a difference between them, and the expression ++n now increments the value of N by 1 and then uses the value of the variable n, while the expression n++ uses the value of the variable n and then increments the value of N by 1.
/* strcat function: string concatenation */void strcat (char s[], char t[]) {int i = 0;int j = 0;while (s[i]! = ' + ') {i++;} while ((s[i++] = t[j++])! = ' + ')/  * copy */{;}}

Note: The self-increment and decrement operators can only be used for variables.
Nine, bitwise operators
The C language provides 6 bit manipulation operators that can only be used for integer operands, that is, only for signed or unsigned char, short, int, long.
& Bitwise AND (and)
| bitwise OR (OR)
^ Bitwise XOR (XOR)
<< left Shift
>> Right Shift
~ Bitwise negation (unary operator)1, Bitwise AND operator & is often used to mask certain bits.
2, bitwise, OR operator | is often used to place some binary positions at 1.
3, Bitwise XOR operator ^ when the corresponding bit of two operands does not set the bit to 1 at the same time, the bit is set to 0.
4, the displacement operator << and >> are used to move the left operand of the operation left and right, the number of bits moved is specified by the right operand (the value of the right operand must be non-negative).
5, unary operator ~ is used to find the binary inverse of integers, that is, the operands of each bits on the 1 to 0, 0 into 1.
Ten, assignment operators and expressions
In an assignment expression, if the variable to the left of the expression appears repeatedly to the right of the expression.
Such as:
i = i + 2;
It can be abbreviated as:
i + = 2;+ = is called an assignment operator.
Most two-dollar operators (that is, operators with about two operands, such as +) have a corresponding assignment operation op=, where op can be one of these operators:
+-*/% << >> & ^ |Note: In all such expressions, the type of the assignment expression is the type of its left operand, and its value is the value after the assignment operation completes.
Xi. conditional expressions
Conditional expressions (using the ternary operator "?) : ") Expr1?   EXPR2:EXPR3; In an expression, EXPR1 is evaluated first, and if its value is not equal to 0 (true), the value of EXPR2 is computed and the value is used as the value of the conditional expression, otherwise the value of EXPR3 is computed and the value is used as the value of the conditional expression.
z = (a > B)? A:B; /*z = Max (A, b) */Note: Only one expression in EXPR2 and EXPR3 is evaluated.  

C-language note types, operators, and expressions

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.