First, type C language
- Integer: char, short, int, long, longlong
- Float: float, double, long double
- Logic: bool
- pointer
- Custom Types
What different types
- Type name: int, long, double
- Input and output formats:% d,% ld,% lf
- Range expressed by number: char <short <int <float <double
- Occupied by memory size: 1 byte to 16 bytes
- Expression memory form: binary (complement) encoding
sizeof
Is an operator, or the type of a given variable number of bytes in the memory occupied,
Is a static operator that results in compile time decided not to do the operation in parentheses sizeof
- sizeof(int)
- sizeof(i)
#include <stdio.h>
int main()
{
int a;
a = 6;
printf("sizeof(int)=%ld\n", sizeof(int));
printf("sizeof(double)=%ld\n", sizeof(double));
printf("sizeof(a)=%ld\n", sizeof(a));
return 0;
}
//sizeof(int)=4
//sizeof(double)=8
//sizeof(a)=4
Second, the integer type
- char: 1 byte (8 bits)
- short: 2 bytes
- int: depending on the compiler (cpu), usually a word in the sense of
- long: depending on the compiler (cpu), usually a word in the sense of
- longlong: 8 bytes
Integer internal representation
Everything inside the computer binary
- 18 ------> 00010010
- 0 ------> 000000
- -18 ------> ?
- Negative expressed in two's complement form of the original code
Anti-code:
Positive anti same code and the original code, negative inverse of the number of code symbols in addition to the original sign bit inverted Members [negated each (except the sign bit)]. The inversion means: original 1, to obtain 0; 0 is the original to give 1. (1 to 0; 0 to 1)
Complement:
Anti-complement code = 1 +
Here for example with a sub--1
- Binary: 00000001 (1)
- Anti-code: 11111110
- Complement: 11111111
1 expression is so full binary 1 in the computer
to sum up:
- Positive anti-code and complement are the same as the original code
- Negative inverse of the code number of the original code except the sign bit inverted you
- Negative complement to everybody except the sign bit inverted the original code number, and then in the last plus 1
Range of integers
- For a byte (8 bits), may indicate: 00000000-11111111
- among them:
- 00000000 ------> 0
- 11111111 ~~~ 10000000 ------> -1 ~~ -128
- 00000001 ~~~ 01111111 ------> 1 ~ 127
- char: 1 byte: -128 to 127
- short: 2 bytes: -32768 to 32767
- int: depending on the compiler (cpu), the usual meaning of "one word"
- long: 4 bytes
- longlong: 8 bytes
unsigned
- If you want to express yourself is a constant unsigned, you can add a U or u later
- L or L represents a long
- unsigned original intention was not able to extend the range of the number of expression, but to do pure binary operations, primarily shift
#include <stdio.h>
int main()
{
unsigned char c = 255;
int i = 255;
printf("c=%d,i=%d\n", c , i);
// c=-1,i=255
// 当使用了unsigned之后 c=255,i=255
return 0;
}
Integer cross-border
Pure binary integer is calculated, so
- 11111111 + 1 ------> 100000000 ------> 0
- 01111111 + 1 ------> 10000000 ------> -128
- 10000000 - 1 ------> 01111111 ------> 127
I.e. in the computer 127 + 1 = -128, -128 -1 = 127
char a = 127;
char b = -128;
char c = a + 1;
char d = b - 1;
printf("%d, %d\n", c , d );
// -128, 127
As shown in FIG: Save clockwise, counterclockwise plus
Input and output integers
Only two forms: int or long long
- %d:int
- %u:unsigned
- %ld:long long
- %lu:unsigned long long
Octal and hexadecimal
- A number literal beginning with 0 are octal
- A number literal begins with 0x are hexadecimal
- % O for octal,% x for hexadecimal
- Octal and hexadecimal numbers just how a string expression, how to express numbers has nothing to do with the internal
char c = 012;
int i = 0x12;
printf("c=%d, i=%d\n", c,i);
printf("c=0%o, i=0x%x\n", c,i);
// c=10, i=18
// c=012, i=0x12
Select the type integer
There is no particular need to choose int
Third, the floating-point types
Types of | Word length | range | effective number |
---|---|---|---|
float | 32 | 7 | |
double | 64 | 15 |
Input and output
Types of | scanf | printf |
---|---|---|
float | %f | %f、%e |
double | %lf | %f、%e |
Wherein% e scientific notation
#include <stdio.h>
int main()
{
double a = 12.321;
printf("%e\n", a );
//1.232100e+001
return 0;
}
Output Accuracy
After adding .n you can specify the output f and a decimal point between several%, so the output is made round 4 of the 5
Out of range of floating-point numbers
- printf output out of range of floating-point represented inf: + ∞, -∞
- printf output nan floating-point representation does not exist
Precision floating-point operations
float a , b , c ;
a = 1.345f;
b = 1.123f;
if (c == 2.468)
{
printf("相等\n");
}else
{
printf("不相等\n");
}
//不相等
Literal with a decimal point is double instead of float
float need f or F suffix to indicate identity
Select the type of floating-point
No special need for direct use double
Character Types
- char is an integer, is a special type: Character
- Literal character represented by single quotes: 'a', '1'
- printf and scanf with% c in output to input characters
char c;
c = '1';
printf("%d\n", c); // 49 ascll
printf("%c\n", c); // 1
Escape character
Expression can not be used to print a special character or a control character, it is the beginning of a backslash back to keep the other characters, and a character composed of
printf("请输入身高和体重,如果输入\"170 80\"表示170cm和80kg\n");
- \ B: a fallback grid, \ t: on to the next table position
- \ N: line feed, \ r: Enter
- ": Double quotation marks, ': single quote
- \: The backslash itself
Automatic type conversion
- When both sides of the operation appears inconsistent with the type, the type of conversion occurs automatically, automatically converted to the larger type
- For printf, less than any type are converted to an int int, float will be converted into double
- But scanf not to enter short, needs% hd
Cast
(Type) value
For example: (int) 10.2
Cast precedence over four operators
bool
include <stdbool.h>
- Then you can use bool and true, false
#include <stdio.h>
#include <stdbool.h>
int main()
{
bool b = 6 > 5;
bool t = true;
printf("%d\n", b);
// 1
return 0;
}
Fourth, the logical operators
Logical operators are arithmetic logic, the result is only 0 or 1
Operators | description | Examples | result |
---|---|---|---|
! | Logical NOT | !a | |
&& | Logic and | a && b | |
|| | Logical or | a||b |
priority
as the picture shows:
Short circuit
Logical operators are carried out from left to right, if the result of the left has been able to determine the outcome, it will not do the calculation on the right
- For && left is not doing the right when false
- For ||, the left is not doing the right when true
Conditional operator
- count = (count > 20) ? count - 10 : count + 10;
- When the value of the condition, condition is satisfied, the value of the condition is not satisfied
Comma operator
int i,j;
i = 3+4,5+6;
j = (3+4,5+6);
printf("%d\n", i);
printf("%d\n", j);
// 7
// 11
When parentheses, is the second calculation expression