C Programming 2nd-basic data types, operators, and expressions

Source: Internet
Author: User
Tags custom name
C Programming 2nd --- basic data types, operators, and expressions

2.1 variable name
Variables and constants are the main objects of C Programs. Therefore, their names affect the readability and maintainability of programs.
C Language32 keywords, Also known as reserved words, that is, the custom name cannot be the same as the 32;
Auto: Declares automatic variables.
Short: declare short integer variables or functions
INT: Declares integer variables or functions.
Long: declares a long integer variable or function.
Float: Declares floating point variables or functions.
Double: Declares double-precision variables or functions.
CHAR: declare struct variables or functions
Struct: Declares struct variables or functions.
Union: Declares the shared data type.
Enum: Declares the enumeration type.
Typedef: Used to get an alias for the Data Type
Const: declare Read-Only variables
Unsigned: Declares unsigned type variables or functions.
Signed: Declares signed variables or functions.
Extern: declared variables are declared in other files
Register: Declares register variables.
Static: declare static variables
Volatile: indicates that variables can be implicitly changed during program execution.
Void: declares that a function has no return value or no parameter and has no type pointer.
If: Condition Statement
Else: The Condition Statement denies the branch (used with if)
Switch: used for switch statement case: switch statement Branch
For: a loop statement.
Do: the body of the loop statement.
While: the loop condition of the loop statement
Goto: unconditional jump statement
Continue: ends the current cycle and starts the next cycle.
Break: jump out of the current loop
Default: "other" branch in the switch statement
Sizeof: calculates the length of the data type.
Return: a subprogram Return Statement (which can contain parameters or not.
It is recommended that the names of variables and constants comply with their attributes and objectives, such as studentname and englishgrade.

2.2 Basic Data Types
In the big aspect, the basic data types are integer and floating point;
From an elementary point of view, there are four types: Char, Int, float, and double;
Note the following:
1) Both char and INT have signed and unsigned points, that is, there are symbols and unsigned. Signed is omitted by default, that is, there is a symbol; unsigned, the value can be suffixed with U/u, such as 1234u;
2) int types include short, Int, and long;
The value range of the basic data type. Int Is supported by different machines and compilers;
CHAR: one byte (8 bits );
Short: generally 16 bits;
INT: the actual integer of the responding machine, 16-bit or 32-bit;
Long: no less than 32 bits;
Float: 4 bytes (32-bit), with a valid number of 6;
Double: 8 bytes (64-bit) with a valid digit of 15;
So the three digits of INT: Short <= int <= long;
Integer Range Calculation: signed (2 ^ (n-1 )~ 2 ^ (n-1)-1), unsigned (0-2 ^ N-1), where N is the number of digits;
The range of basic data is defined in <limits. h> and <float. h>.

2.3 Constants
1) Numeric (literal) Constant
① Int constant, such as 1234. The general Suffix of Long is 123456781234l/L, and the suffix of unsigned long is 123456781234ul/ul;
② Float constant, because float type is usually double by default, float values are usually suffixed with F/F to represent the float constant values. In addition, there are scientific notation, such as 1.234e/e +/-3;
③ Other integer representation methods, such as octal and hexadecimal; octal, starting with 0, for example, 023 indicates 19; hexadecimal, starting with 0x/0x, for example, 0x23 indicates 35;
Tip: integer data can be expressed in binary format. octal values are in a group of three digits, and hexadecimal values are in a group of four digits. octal values (, 2... 7); hexadecimal (0, 1... 9, ,... f ).
2) character constants
① For example, 'A' and 'B' are single characters;
② For example, '\ t' and' \ n' are escape characters;
\ A bell (BEL)
\ B backspace (BS), move the current position to the previous column
\ F New Page (ff), move the current position to the beginning of the next page
\ N line feed (LF), move the current position to the beginning of the next line
\ R press enter (CR) to move the current position to the beginning of the line
\ T horizontal tabulation (HT) (jump to the next tab)
\ V vertical tabulation (VT)
\ Represents a backslash character ''\'
\ 'Represents a single quotation mark (Marker) character
\ "Represents a double quotation mark character
\ 0 null character (null)
\ DDD 1-3 random characters
\ Xhh any character in hexadecimal notation between 1 and 2.
The following definitions are usually used:
# Define vtab '\ 013'/* ASCII vertical tab */
# Define Bell '\ 007'/* ASCII Bell character */
3) constant expression
Generally, at the beginning of the code file, # define xxx yyy is used to represent the xxx expression that appears in the program instead of YYY, for example:
# Define PI 3.1415926535
# Define transfer sion 1e-5
# Define max (A, B) (a)> (B ))? (A): (B)
4) string constants
For example, "Hello, world" is a String constant, that is, a single "a" is also a string, and a String constant is stored in the read-only memory zone (a pointer usually points to this string constant, an error occurred while attempting to modify its content ).
Note: string constants are assigned to character arrays. If the array space is sufficient, the character array ends with '\ 0.
5) Enumeration Constants
An enumeration is composed of a series of integer constant values, such:
Enum months {Jan = 1, Feb, MAR, APR, May, Jun,
Jul, Aug, SEP, Oct, Nov, Dec };
The values of each enumerated object are unique, increasing from top to bottom and decreasing from bottom to bottom.

2.4 statement
A variable must have been declared before use, such as char name [20];
One format (one row of multiple variables of the same type): int lower, upper, step;
Another format (one row for each variable) <recommended>:
Int lower;
Int upper;
Int step;
Sometimes the Declaration can be initialized by the way, that is, the initial value is assigned; for example: flaot EPS = 1e-5;
The keyword 'const' indicates the self-read variable, but the value of the variable is not a constant. It is often used for Parameter Modification of the function, indicating that the parameter cannot be modified.

2.5 type conversion
Operations between different data types must involve conversions of different data types;
For example, double value1 = 9.65; int value2 = 35; double addresult = value1 + value2;
Keep in mind the conversion principle: Convert from Char to double to the direction with a wide range of values;
Convert char and short to int. If long is encountered, convert all to long;
If one of the numbers is float and double, all are converted to float and double.

2.6 Operator
① Arithmetic operators: +,-, *,/, and % (remainder)
A) % is not applicable to float and double; % is based on the machine;
B) + and-have the same priority. The latter three have higher priority than the first two, but lower than the unit operation method (+ ,-)
② Relational operators:>, >=, <, <=, =, and ,! =
A) the first four have the same priority, but only higher than the latter two;
③ Logical operators: &, |
A) The former has a higher priority than the latter.
B) A & B. If a is true, the values of B are calculated sequentially. If B is true, the entire result is true; otherwise, the result is false. If a is false, B is not calculated. The result is false;
C) A | B. if A is true, B is not calculated, and the entire result is true. If a is false, B is calculated. If B is false, the entire result is false. Otherwise, the result is true.
④ Auto-increment and auto-increment operators: ++ and ,--
A) For prefix, such as ++/-- A, execute a = a + 1 first, and then go to the value of;
B) when the suffix is used, for example, a ++/--, the value of a is obtained first, and then a = a + 1 is executed.
⑤ Operators: &, |, ^, <,> ,~
Bitwise operation, that is, each operator is converted to a binary complement operation.
A) A & B, when the corresponding bits are the same, the result is the same value; otherwise, the result is 0;
B) A | B. When a single digit is 1, the corresponding digit | the result value is 1; otherwise, the value is 0;
C) A ^ B. If the value of the corresponding bit is the opposite, the comparison value is 1; otherwise, the comparison value is 0;
D) A> N, A shifts n bit values to the right, and 0 is left filled; A <n, a shifts n bit values to the left, and 0 is added to the right;
E )~ The bitwise values of a and A are reversed, that is, 1 is changed to 0, 0 is changed to 1;
⑥ Conditional expression: AAA? B: c
If the AAA expression is true, the value of expression B is returned; otherwise, the value of expression C is returned;
7. assignment operators: =, + =,-=, * =,/=, % =, >>=, <<=, & =, |=, ^ =
The above description is: I + = 1 is equivalent to I ++ and I = I + 1

2.7 calculation priority and Value Order
1) (), [],->,. [left to right]
2 )! ,~ , ++, --, + (Single),-(single), * (pointer), sizeof [right to left]
3) *,/, % [left to right]
4) +,-[left to right]
5) <,> [left to right]
6) <, <=,>,> = [left to right]
7) = ,! = [Left to right]
8) & [left to right]
9) ^ [left to right]
10) | [left to right]
11) & [left to right]
12) | [left to right]
13 )? : [Right to left]
14) =, + =,-=, * =,/=, % =, >=, <=, & =, |=, ^ =
[Right to left]
15), [left to right]
The priority does not need to be memorized. Remember that each operation is included in () to reduce errors.

[Disclaimer:

1) This content may come from the Internet, or I have sorted it out by myself. It only represents the opinions and opinions of the Internet and individuals!
2) This content is for reference only. Any reference to this content will cause any consequences and will be irrelevant to the original author and the author of this blog !]

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.