2: Data types and variables
1. Introduction to data types:
Built-in data types (in the C language itself): Character - char
; Integer type - int
; Floating point type - float
; Boolean type - _Bool
Custom data types (types created by yourself): array; structure- struct
; enumeration- enum
; union- a>union
(1) Character type: Character types are divided into three categories
1. char //字符类型
2. signed char //有符号的字符类型
3. unsigned char //无符号的字符类型
(2) Integer type: Integer type is divided into four categories
//整型
1. int //整数类型
2. [signed] int //有符号的整型
3. unsigned [int] //无符号的整型
//短整型
1. short [int] //短整型
2. [signed] short [int] //有符号的短整型
3. unsigned short [int] //无符号的短整型
//长整型
1. long [int] //长整型
2. [signed] long [int] //有符号的长整型
3. [unsigned] long [int] //无符号的长整型
//更长的整型
1. long long [int] //长长整型
2. [signed] long long [int] //有符号的长长整型
3. unsigned long long [int] //无符号的长长整型
Note: The above content wrapped with [ ] can be omitted when writing code
(3) Floating point type (decimal): Floating point type is divided into three categories
1. float //单精度浮点型
2. double //双精度浮点型(精度比较高)
3. long double //精度更高的浮点型
(4) Boolean type: At the beginning of the C language, used 0 to represent false, and non-0 represented true , The Boolean type was later introduced in theC99 standard
When using Boolean types, also include the header file<stdbool.h>
There are only two values for Boolean type variables:true
or false
1. _Bool //布尔类型
#include <stdio.h>
#include <stdbool.h>
int main()
{
_Bool flag = true;
if(flag)
printf("I Like You\n");
return 0;
}
2.signed 和 unsigned:
- In C language, the two keywords
signed
andunsigned
are used to modify character types and integers -
signed
keyword, indicating a type with a positive and negative sign, including negative values -
unsigned
keyword, indicating that this type does not have a sign and can only represent 0 and positive integers - For the
int
type, the default is signed, that is,int
is equivalent tosigned int
, sosigned
can be omitted
int main()
{
signed int temperature1 = 30; //temperature表示温度,而温度会有负数的可能性,所以用signed修饰
signed int temperature2 = -30;
unsigned int age = 20; //age表示年龄,而年龄未来只有正数的可能性,所以用unsigned去修饰
return 0;
}
Note: 1. When representing a non-negative integer, unsigned
must be used to declare Variables
2. The advantage of declaring an integer variable as unsigned
is that the same length of memory can represent that the maximum integer value has doubled
3. The value range of the integer type can refer to the definition given in limits.h
//字符类型的取值范围
1. signed char a; //取值范围为 -128~127
2. unsigned char a; //取值范围为 0~255
Note: C language stipulates that char
whether the type has a positive or negative sign is determined by the current system , that is to say, char
is not equivalent to signed char
, it may be signed char
, or it may be unsigned char
; This is different from int
3. Value range of data type:
Each data type has its own value range, which is the interval between the maximum and minimum values of the stored value.
View the values of different data types on the current system:
limits.h This header file describes the value range of the integer type.
float.h This header file describes the value range of the floating point type.
For the sake of code portability, when you need to know the limit value of a certain data type, try to use these constants:
constant | describe |
---|---|
SCHAR_MIN;SCHAR_MAX |
Minimum and maximum value of signed char |
SHRT_MIN;SHRT_MAX |
Minimum and maximum values of short |
INT_MIN;INT_MAX |
Minimum and maximum value of int |
LONG_MIN;LONG_MAX |
Minimum and maximum values of long |
LLONG_MIN;LLONG_MAX |
Minimum and maximum values of long long |
UCHAR_MAX |
Maximum value of unsigned char |
USHAR_MAX |
Maximum value of unsigned short |
UINT_MAX |
Maximum value of unsigned int |
ULONG_MAX |
Maximum value of unsigned long |
ULLONG_MAX |
Maximum value of unsigned long long |
*Note:* Remember to include the header file when using these constants
Operator
sizeof()
: Calculates the memory size occupied by variables created by the type, the unit is bytes
size_t
is a type specially set forsizeof()
, and the essence ofsize_t
isunsigned int
Note: size_t
type data should be printed in the format %zd
int main()
{
printf("%zd\n",sizeof(char));
printf("%zd\n",sizeof(short));
printf("%zd\n",sizeof(int));
printf("%zd\n",sizeof(long long));
printf("%zd\n",sizeof(float));
printf("%zd\n",sizeof(double));
return 0;
}
//C语言标准规定:sizeof(long) >= sizeof(int)
Note: Common units in computers: (sorted from small to large)
bit – bit – the space required when storing a binary bit (1 or 0) is a bit
Byte – 1Byte == 8bit
KB – 1KB == 1024Byte
MB – 1MB == 1024KB
GB – 1GB == 1024MB
TB – 1TB == 1024GB
PB – 1PB == 1024TB
4. Variables:
(1) Creation of variables
In C language, values that change frequently are called variables, and values that do not change are called constants. Types are used to create variables.
Syntax format for creating variables: data type variable name;
date_type name;
//变量的创建
#include <stdio.h>
int main()
{
int age; //整型变量
char str; //字符变量
float heigh; //浮点型变量
return 0;
}
Variables are given an initial value when they are created, which is called initialization.
//变量的初始化
#include <stdio.h>
int main()
{
int age = 18;
char str = "你猜";
float heigh = 3.14159; //编译器会自动把它识别成double类型的数字,容易出现警告
float heigh = 3.14159f; //此时在值的后面加上一个f,编译器会自动把它识别成float类型的数字
return 0;
}
(2) Classification of variables
Global variables: Variables defined outside curly braces are global variables. Global variables have a wide range of use and are considered throughout the project. There are ways to use them
Local variables: Variables defined inside curly braces are local variables. The scope of use of local variables is relatively limited and can only be used in their own Used within the local scope
#include <stdio.h>
float a = 3.1234f; //全局变量,这个a可以在整个项目工程中使用
int main()
{
int b = 10; //局部变量,这个b仅限于main()函数内部使用
{
char c = "你猜" //这个c仅限于这个小括号中使用
}
return 0;
}
Note: When global variables and local variables have the same name, the local variable will be used first
Note: 1. Local variables are placed in the stack area of memory
2. Global variables are placed in the static area of memory
3. The heap area is used for dynamic memory management
5. Arithmetic operators:
Arithmetic operators, because there is an operand on the left and right ends of the operator, are also called binary operators, and operators are also called operators.
(1) Addition +
Harmonic method -
#include <stdio.h>
int main()
{
int a = 34 + 78;
float b = 45.7 - 30.3;
int c = a + b; //变量之间也可以相加减
printf("%d\n",a);
printf("%d\n",b);
printf("%d\n",c);
reyurn 0;
}
(2) Multiplication*
#include <stdio.h>
int main()
{
int a = 23 * 78;
printf("%d\n",a);
return 0;
}
(3) Division (integer division, as long as the integer does not require a remainder)/
#include <stdio.h>
int main()
{
int a = 7 / 2; //结果为3
printf("%d\n",a);
return 0;
}
Note: 1. If both ends of the division sign are integers, integer division will be performed, and the result will be an integer, discarding the remainder.
2. As long as there are floating point numbers on both ends of the division sign, floating point number division will be performed.
#include <stdio.h>
int main()
{
int a = 7.0f / 2;
printf("%d\n",a);
return 0;
}
(4) Torimo%
#include <stdio.h>
int main()
{
int a = 23 % 78;
printf("%d\n",a);`
return 0;
}
Note: 1. Modulo returns the remainder of the division of two integers, and modulo can only be used for integers. Cannot be used with floating point numbers
2. When taking modulo a negative number, the sign of the result is determined by the sign of the first operand.
#include <stdio.h>
int main()
{
int a = -23 % 78; //此时结果是负数
int b = 8 % -2; //此时结果是正数
printf("%d\n",a);
printf("%d\n",b);
return 0;
}
6. Assignment operator:
When a variable is created, giving a value is called initialization; after the variable is created, giving a value is called assignment, and the assignment symbol is:=
#include <stdio.h>
int main()
{
int a = 20; //初始化
a = 40; //赋值
return 0;
}
(1) Continuous assignment:
#include <stdio.h>
int main()
{
int a = 20; //初始化
int b = 40;
int c = 78;
c = b = c + a + 4; //连续赋值,从右到左依次赋值
printf("%d\n",c);
return 0;
}
(2) Compound assignment (self-increasing; self-decreasing):
#include <stdio.h>
int main()
{
int a = 20; //初始化
a = a + 3; //可以将其替换成 a += 3;
a = a - 4; //可以将其替换成 a -= 2;
printf("%d\n",a);
return 0;
}
7. Unary operator:
Some operators in C language have only one operand, and these operators are called unary operators.
(1)++
sum--
++
is an operator that increases by 1 and is divided into prefixed++
and postfixed++
--
is a self-decreasing 1 operator, which is divided into prefixed--
and postfixed--
//前置++或--:先++或--,后使用
#include <stdio.h>
int main()
{
int a = 29;
int b = ++a; //先 a = a + 1;再 b = a
printf("%d\n",a); //结果为30
printf("%d\n",b); //结果为30
return 0;
}
#include <stdio.h>
int main()
{
int a = 29;
int b = --a; //先 a = a - 1;再 b = a
printf("%d\n",a); //结果为28
printf("%d\n",b); //结果为28
return 0;
}
//后置++或--:先使用,再进行++或--
#include <stdio.h>
int main()
{
int a = 29;
int b = a++; //先 b = a;再 a = a + 1
printf("%d\n",a); //结果为30
printf("%d\n",b); //结果为29
return 0;
}
#include <stdio.h>
int main()
{
int a = 29;
int b = a--; //先 b = a;再 a = a - 1
printf("%d\n",a); //结果为28
printf("%d\n",b); //结果为29
return 0;
}
(2)+
sum-
Here are the plus sign and the minus sign, both unary operators.
Operator +
has no effect on positive and negative values and is a symbol that can be omitted;
Operator-
is used to change the sign of a value. Adding -
before a negative number will result in a positive number; adding before a positive number -
will get a negative number
#include <stdio.h>
int main()
{
int a = +10; //等同于 int a = 10;
int b = -20;
int c = -b; //等同于 c = 20;
printf("%d\n",c);
return 0;
}
8. Forced type conversion:
Coercion is a special type of operator among operators
Syntax format: (Type) – Just write the type in brackets to the type you want to force conversion to.
#include <stdio.h>
int main()
{
int a = (int)3.1415; //结果为3,将double类型强制转换成int类型
printf("%d\n",a);
return 0;
}
Note: A tough melon is not sweet