Variables and operations

Source: Internet
Author: User
Tags control characters integer division keyword list

One Variable

1. variables are the names of some specific units in the program's memory.

 

2. keyword list:

Auto Break Case

Char

Const Continue
Default Do Double
Else Enum Extern
Float For Goto
If Int Long
Register Return Short
Signed Sizeof Static
Struct Switch Typedef
Union Unsigned Void
Volatile While  

 

3. Variable type

Type Storage Unit (in C compiler) Range
Bytes Bits
Symbol bit Data bit
Int 4 1 31 -32,768 ~ 32767
Char 1 1 7 -128 ~ 127
Float 4 1 8-digit Index 23-digit ending number -3.4e-38 ~ 3.4e + 38
Double 8 1 11-digit Index 52-digit ending number -1.7e-308 ~ 1.7e + 308

 

Register Type: Let the compiler store variables in registers (resident in the CPU itself). The speed of the compiler to access this value is greatly improved, so the program runs faster.

Usage: register type name variable name;

Register Type identifiers should be used for frequently accessed variables to improve program performance.

 

You can use the typedef statement to create your own type: typedef system type name custom type name;

 

4. Assign the octal value or hexadecimal value:

Octal: starts with "0", for example, int octal_value = 0627;

Hexadecimal: starts with "0x", for example, int hex_value = 0xff0;

 

5. escape characters

Escape characters are a special form of characters in C. Generally, escape characters are used to indicate non-printable control characters in the ASCII character set and special function characters. For example, a single marker (') is used to represent a character constant ('), it is used to represent the double apostrophes (") and backslash (/) of string constants. Escape characters are represented by a backslash (/) followed by a character or an octal or hexadecimal number.

Escape Character meaning ASCII code value (decimal)
/A bell (BEL) 007
/B backspace (BS) 008
/F form feed (FF) 012
/N line feed (LF) 010
/R press enter (CR) 013
/T horizontal tabulation (HT) 009
/V vertical tabulation (VT) 011
// Backslash 092
/? Question mark character 063
/'Single quotes character 039
/"Double quotation mark character 034
/0 null character (null) 000
/DDD any character three octal characters
/Xhh any character two hexadecimal

 

6. format the output.

(1). d output a decimal integer
A. % d:
B. % MD: Specify the output width. The number of data digits is smaller than m, left side fill space; greater than m, output by actual number of digits.
A = 123; B = 12345;
Printf ("% 4D, % 4D", a, B );
Output result: _ 123,12345
C. % ld: Output long integer data.
Long A = 123456;
Printf ("% lD", a); % d, error.
Printf ("% 9ld", a); output: ___ 123456

D. % 0ld: 0 fill integer output (replace the space above with 0)
(2). O output Octal numbers
(3). x outputs The hexadecimal number.
(4). U output unsigned data
(5). c Outputs a character
(6). S outputs a string
A. % s printf ("% s". "How ");
B. % Ms
C. %-MS
D. % m. NS
E. %-M. NS
(7). F outputs real numbers in decimal form
A. % F
B. % m. NF
C. %-M. NF
(8). E outputs real numbers in exponential form
A. % E
B. % m. ne
C. %-M. ne

(9) printf uses the right-aligned output by default. If you need the left-aligned output, place a minus sign (-) after %.

 

7. When a local variable with the same name as the global variable is used, the original local variable attributes remain unchanged. This local variable with the same name will temporarily block this global variable.

 

Binary Calculation

1. Operator table

Operators are arranged from top to bottom according to their priority values. operators in the same row have the same priority. The second line is all unary operators.
 

Operator Explanation Combination Method
() []->. Parentheses (functions, etc.), arrays, two types of structure member access From left to right
! ~ ++ -- +-

* & (Type) sizeof

Negative, bitwise negative, incremental, reduced, positive and negative,

Indirect, address fetch, type conversion, and Size Estimation

From right to left
*/% Multiplication, division, modulo From left to right
+- Add, subtract From left to right
<> Left shift, right shift From left to right
<<=>=> Less than, less than or equal to, greater than or equal to, greater From left to right
=! = Equal to, not equal From left to right
& Bitwise AND From left to right
^ Bitwise OR From left to right
| By bit or From left to right
&& Logic and From left to right
| Logic or From left to right
? : Condition From right to left
= + =-= * =/=

<=>>=

Various assignments From right to left
, Comma (Order) From left to right

 

Auto-increment and auto-increment:It remains unchanged after the change!

 

Bitwise operation:

(1) Displacement Operator

>>: This operation is called the bitwise right shift operator. It shifts the binary number on the left to the specified number of digits on the right.

<: This operation is called the bitwise left shift operator. It shifts the binary value of the number on the left to the specified number of digits on the left.

The displacement operator acts on the variables on the left. The value of the expression on the right is the number of digits to be moved, and the calculation result is the result of the variable to be moved.

For example:

B = A <3. The value of a shifts three places to B, but the value of A does not change.

Shift left to fill 0 in the low position, and shift right to fill 0 in the high position. When the right shift is performed, the result symbol bit can be maintained, that is, when the right shift is performed, if the highest bit is 1, it is a sign bit, then 1 is supplemented instead of 0.

The right-shift operator is often used for integer division, and the left-shift operator is used for integer multiplication. The factor used for multiplication and division must be the power of 2.

 

(2) bitwise logical operators

&: It is called bitwise AND operator. If both bits are 1, the result is 1. Otherwise, the result is 0.

# Include <stdio. h> <br/> void main () <br/> {<br/> printf ("0 & 0 is % d/N", 0 & 0 ); <br/> printf ("0 & 1 is % d/N", 0 & 1); <br/> printf ("1 & 1 is % d/N ", 1 & 1); <br/> printf ("1 & 2 is % d/N", 1 & 2 ); <br/> printf ("15 & 127 is % d/N", 15 & 127); <br/>}

 

Output result:

0 & 0 is 0

0 & 1 is 0

1 & 1 is 1

1 & 2 is 0

15 & 127 is 15

 

 

|: It is called a bitwise OR operator. If one of them is 1, the result is 1. Otherwise, it is 0.

# Include <stdio. h> <br/> void main () <br/> {<br/> printf ("0 | 0 is % d/N", 0 | 0 ); <br/> printf ("0 | 1 is % d/N", 0 | 1); <br/> printf ("1 | 1 is % d/N ", 1 | 1); <br/> printf ("1 | 2 is % d/N", 1 | 2 ); <br/> printf ("128 | 127 is % d/N", 128 | 127); <br/>}

 

Output result:

0 | 0 is 0

0 | 1 is 1

1 | 1 is 1

1 | 2 is 3

128 | 127 is 255

 

^: Bitwise XOR or operator. If two bits are not the same, the result is 1. Otherwise, the result is 0.

# Include <stdio. h> <br/> void main () <br/> {<br/> printf ("0 ^ 0 is % d/N", 0 ^ 0 ); <br/> printf ("0 ^ 1 is % d/N", 0 ^ 1); <br/> printf ("1 ^ 1 is % d/N ", 1 ^ 1); <br/> printf ("1 ^ 2 is % d/N", 1 ^ 2 ); <br/> printf ("15 ^ 127 is % d/N", 15 ^ 127); <br/>}

 

Output result:

0 ^ 0 is 0

0 ^ 1 is 0

1 ^ 1 is 1

1 ^ 2 is 3

15 ^ 127 is 112

 

~ : Bitwise Inverse Operator. This is a single object operator. If it is 1, the result is 0. If it is 0, the result is 1.

 

Length (sizeof) Operator: returns the number of bytes required for a variable or type.

 

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.