C values into basic types (integer: short, int, long; real: float, double, long double) and the character type char.
Integer
Integer is divided into short integer short, int int and long integer long. The number of bytes occupied by each type of bits associated with CPU
Types of | 16-bit CPU | # 32 | # 64 |
---|---|---|---|
int | 2 (btye) | 4 | 4 |
unsigned int | 2 (btye) | 4 | 4 |
short | 2 | 2 | 2 |
unsigned short | 2 (btye) | 2 | 2 |
long | 4 | 4 | 8 |
unsigned long | 4 | 4 | 8 |
Now generally used in 64-bit CPU.
unsigned unsigned, unsigned number is positive, a value ranging from zero.
For example:
int 64-bit CPU in the range of values represented [-2^31 , 2^31 - 1]
unsigned int 64-bit CPU in the range of value indicating [0, 2^32 - 1]
values are represented by the number 2 ^ 32
The number of different binary representations in C
In binary number 0b
, or 0B
(case insensitive) at the beginning, such as
int a = 0b101; //换算成十进制为 5
int b = -0b110010; //换算成十进制为 -50
int c = 0B100001; //换算成十进制为 33
复制代码
Octal number to 0
begin with, such as
int a = 015; //换算成十进制为 13
int b = -0101; //换算成十进制为 -65
int c = 0177777; //换算成十进制为 65535
复制代码
Hex digits 0x
or 0X
(case insensitive) at the beginning, such as
int a = 0X2A; //换算成十进制为 42
int b = -0XA0; //换算成十进制为 -160
int c = 0xffff; //换算成十进制为 65535
复制代码
On output, the value can not be binary output, only in decimal, octal, hexadecimal output. Output symbol
short | int | long | |
---|---|---|---|
Octal | % Ho | %O | %the |
Decimal | %hd | %d | %ld |
Hex | %hx 、%hX | %x、%X | %lx、%lX |
Real
Real divided, float float, double, double, long double long double. Computer storage finite decimal precision, can not save many, many decimal places
Types of | Occupancy bytes | effective number |
---|---|---|
float | 4 (btye) | 6-7 place |
double | 8 | 15-16 |
long double | 16 |
Output symbol %f
, the default is after six decimal places, setting %.2f
can be to two decimal places.
float a = 3.14;
printf("%f -- %.2f\n", c,c); //打印结果为 3.140000 -- 3.14
复制代码
Character Types
Char Character type, occupies only 1 byte, because the char variable is stored in memory corresponding to the ASCII value of the character, ASCII code value from 0 to 127, may be just a binary representation of the byte length. char type variable if the %c
output, according to the ASCII code table is converted to the corresponding characters; if the %d
output, or an integer.
char a = 'A';
int b = a;
printf("%c -- %d\n", a,b); //打印结果为 A -- 65
复制代码
- Byte long type conversion to short-byte type assignment, truncation will occur, such as int -> char when, will retain the low byte int, char length of the high byte exceeds the discard.
- Conversely expansion occurs, the under-byte complement, complement 0 positive, negative, up 1
Operation
When different types of operation, type conversion occurs
- Byte short line to long byte
- Signed in line to the unsigned
- Integer in line to float
- Line to the double-precision single-precision
First conversion type, and then calculated.
Increment, decrement
i++
And ++i
, i--
and --i
. After symbol, compared with the value before the operation. Previous symbol, taken after calculation, i.e.,
int i = 0 ;
int j = i++ ; // i = 1, j = 0
复制代码
int i = 0 ;
int j = ++i ; // i = 1, j = 1
复制代码
int i = 0 ;
int j = i--; // i = -1, j = 0
复制代码
int i = 0 ;
int j = --i; // i = -1, j = -1
复制代码
Increment decrement operation can only variables, constants can not operate, such as ++0
the like is wrong
Composite Operators
It refers to a compound operator *=
, +=
, -=
and the like of the operator, the right combination.
int i = 0;
i += 2; // i = i + 2;
i *= 3; // i = i * 3;
printf("%d\n",i); // 6
复制代码