C language NOTE 1 -- type, operator, and expression

Source: Internet
Author: User
Tags integer division integer numbers

C language NOTE 1 -- type, operator, and expression
Bytes

C language NOTE 1 -- type, operator, and expression

Overview:

Variables and constants are two basic data objects processed by a program. The declaration statement describes the name and type of the variable, and can also specify the initial value of the variable. Specifies the operation to be performed. The expression combines variables and constants to generate new values. The object type determines the set of optional values of the object and the operations that can be performed on the object.


1. Names of variables and constants
1. The name is a sequence of letters, underscores, and numbers. The first character must be a letter. The underscore (_) is considered a letter, however, do not start.
2. The name is case-sensitive, and the variable name uses lowercase letters. All symbolic constant names use uppercase letters, and words are separated by underscores.

Experience: The selected variable name should be able to literally express the purpose of the variable as much as possible. Local variables generally use short variable names (especially loop control variables), and external variables use long names.


Ii. Data Types
The C language provides the following four basic data types:

char // Character type, takes up one byte
int // Integer, usually reflects the natural length of the integer in the machine used
float // Single precision floating point
double // Double precision floating point type
Data type qualifier:
1. The two qualifiers short and long are used to qualify integer types; the keyword int can be omitted, and it is usually used to do so.

short sh; // short int sh;
long counts; // long int counts;
2. The type qualifiers signed and unsigned can only be used to qualify char types and any integer types.
3. The long qualifier can be used for double-precision floating-point types, and the long double type represents high-precision floating-point numbers.


Three, constant
1. Integer constant
Integer constants like 120 are of type int;
Constants of type long end with the letters l or L, such as 120680L;
Unsigned constants end with the letters u or U.


2. Floating-point constant
Floating-point constants contain a decimal point (such as 380.5) or an exponent (3.805e + 2), or both. The floating-point constant without suffix is of type double. The suffix f or F indicates the float type, and the suffix l or L indicates the long double type.

Note: In addition to decimal numbers, integer numbers can also be represented in octal or hexadecimal. The prefix 0 means octal 037 (such as decimal 31), and the prefix 0x or 0X means hexadecimal 0x1f (such as decimal 31).


3. Character constants
A character constant is an integer. When writing, enclose a character in single quotes (such as ‘0’). The value of the character in the machine character set is the character constant value. For example, the value of ‘0’ in the machine character set is 48, which is irrelevant to the number 0; and the value of ‘A’ in the machine character set is 65.

Note: Character constants are generally used for comparison with other characters, but they can also participate in numerical operations like other integers. The character constant ‘\ 0’ represents a character with a value of 0, which is a null character (null). We usually use ‘\ 0’ instead of 0 to emphasize the character properties of certain expressions, but the numeric value is 0.


4. String constants
A string constant is also called a string literal, and is a sequence of characters consisting of 0 or more characters enclosed in double quotes.

The string constant is an array of characters. The internal representation of the string uses a null character ‘\ 0’ as the end of the string. Therefore, the number of physical storage units that store character strings is one more than the number of characters enclosed in double quotation marks.

Note: There is no limit to the length of the string in C language. The program scans to the "\ 0" end character to indicate the end of the string. String constants are stored in static storage.

The standard library function strlen (char * str) returns the length of the string str (that is, the number of characters before the null terminator).
The following design a version of strlen function:
int strlen (const char s [])
{
     int i = 0;
     while (s [i]! = '\ 0')
     {
++ i;
     }
     return i;
}
5. Enumeration constants
Enumeration is a list of constant integer values.

// Reserved business type
enum RTBPUB_RESVYPE
{
     RESVYPE_IB = 0, // Comprehensive accounting
     RESVYPE_OCE = 1, // Online billing
     RESVYPE_HB = 2, // Offline billing
     RESVYPE_OCECHARGE = 5 // Online billing and recharge
};
(1) An enumerated type is a collection. The elements (enumeration members) in the collection are named integer constants. The elements are separated by a comma ",".
(2) RTBPUB_RESVYPE is an identifier, which can be regarded as the name of this collection, is an option, that is, an optional item.
(3) If no value is assigned to the enumeration, the default value of the first enumeration member is an integer of 0, and the value of subsequent enumeration members adds 1 to the previous member.
(4) You can set the value of an enumeration member to customize an integer within a certain range.
(5) The enumerated type is a replacement for the #define preprocessing directive.

6. Constant expressions
Constant expressions are expressions that contain only constants. Such expressions are evaluated at compile time, not at runtime.

#define MAXLINE 1000
char g_szline [MAXLINE + 1];

4. Statement
Variables must be declared before use. A declaration specifies a variable type. The variable can be initialized at the same time as the declaration. In the declaration, if the variable name is followed by an equal sign and an expression, the expression acts as an initialization expression for initializing the variable.

int i = 0;
int ilen = MAX + 100;
Note: The declaration statement explains the name and type of the variable, and you can also specify the initial value (initialization) of the variable.
Any variable declaration can be qualified with the const qualifier, which specifies that the value of the variable cannot be modified; for arrays, 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 will intercept the fractional part of the result. The result of the expression x% y is the remainder of dividing x by y. When x is divisible by y, the value is 0. The modulus operator% cannot be applied to float and double types.


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

There are two equality operators (special relational operators) ==! =, Which are second only to relational operators.
Note: The priority of relational operators is lower than arithmetic operations.


2. Logical operations include the following two operators:
&& ||
Logical operations have some special properties. Expressions connected by && and || are evaluated in order from left to right, and the calculation stops immediately after knowing that the result value is true or false.
Operator && has higher priority than ||, but both have lower priority than relational and equality operators.

Note: According to the definition, in a relational expression or logical expression, if the relationship is true, the value of the result value of the expression is 1; if it is false, the value of the result value is 0. The purpose of the logical not operator! Is to convert non-zero operands to 0 and operand 0 to 1.
Seven types conversion
When several operand types of an operator are different, you need to convert them to some common type through some rules.
In general, automatic conversion refers to the conversion of "narrower" operands into "wider" operands without losing information.
1. Since the char type is a smaller integer type, variables of the char type can be freely used in arithmetic expressions, and the character type will be converted to an integer type.
/ * atoi function: convert the string S to the corresponding integer * /
int atoi (const char s [])
{
    int i;
    int n = 0;
    for (i = 0; s [i]> = '0' && s [i] <= '9'; i ++)
    {
n = 10 * n + (s [i]-'0');
    }
The
    return n;
The
}

The expression s [i]-'0' can calculate the numeric value corresponding to the characters stored in s [i], because the ASCII code table characters are all connected.
2. In C language, implicit arithmetic type conversion will be performed in many cases.
In general, if the two operations of the binary operator have different types, then the "lower" type must be promoted to the "higher" type before performing the operation.
rule:
If one of the operands is of type long double, the other operand is converted to long double type.
If one of the operands is of type double, the other operand is converted to double.
If one of the operands is of type float, the other operand is converted to float.
If the type of one of the operands is long, the other operand is converted to a long type.
Convert operands of type char and short to type int. Note: In general, mathematical operations use variables of type double.
3. Type assignment should also be performed. The value on the right side of the assignment operator needs to be converted to the type of the variable on the left. The type of the variable on the left is the type of the assignment expression result.
4. In any expression, you can use an unary operator called coercive type conversion to force explicit type conversion.
(Type name) expression;

Eight, self-increasing and self-decreasing operators
The C language provides two special operators for variable increment and decrement. The increment operator ++ increments its operand by 1; the decrement operator--decreases its operand 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 suffix operators (used after variables, Such as n ++), in both cases, the effect is to increase the value of the variable n by 1. However, there is a difference between them, the expression ++ n now increments the value of n, and then uses the value of the variable n, while the expression n ++ uses the value of the variable n first, and then increments the value of n 1.
/ * strcat function: concatenate strings * /
void strcat (char s [], char t [])
{
int i = 0;
int j = 0;
The
while (s [i]! = '\ 0')
{
i ++;
}
The
while ((s [i ++] = t [j ++])! = '\ 0') / * copy * /
{
;
}
The
}

Note: The increment and decrement operators can only be applied to variables.
Nine, bitwise operator
C language provides 6 bit operation operators, these operators can only act on integer operands, that is, only on signed or unsigned char, short, int, long.
& AND
| Bitwise OR (OR)
^ XOR
<< shift left
>> right
~ Bitwise negation (unary operator) 1. Bitwise AND operator & is often used to mask certain binary bits.
2. The bitwise OR operator | is often used to set certain binary positions to 1.
3. Bitwise XOR operator ^ Set the bit to 1 when the corresponding bits of the two operands are different, otherwise set the bit to 0.
4. The displacement operators << and >> are used to shift the left operand of the operation to the left and right respectively, and the number of shifted digits is specified by the right operand (the value of the right operand must be a non-negative value).
5. The unary operator ~ is used to find the two's complement of the integer, that is, change the 1 on each binary bit of the operand to 0, and 0 to 1.
Ten, assignment operators and expressions
In an assignment expression, if the variable on the left of the expression repeatedly appears on the right of the expression.
Such as:
i = i + 2;
It can be abbreviated as:
i + = 2; + = is called the assignment operator.
Most binary operators (that is, operators with two left and right operands, such as +) have a corresponding assignment 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 is completed.
11. Conditional expressions
Conditional expression (using the ternary operator "?:") Expr1? Expr2: expr3; In the expression, first calculate expr1, if its value is not equal to 0 (true), then calculate the value of expr2 As the value of the conditional expression, otherwise calculate the value of expr3 and use this value as the value of the conditional expression.
z = (a> b)? a: b; / * z = max (a, b) * / Note: Only one expression can be evaluated in expr2 and expr3.


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.