2. Basic data types and variables

1.Data type

This time we mainly understand the basic data types and Boolean types
Insert image description here

Note: [ ] means you can omit not write
1.1 characters
char
[signed] char
unsigned char

1.2 Integer
short integer type
short [int]
[signed] short [int]
unsigned short [int]

整型
int
[signed] int
unsigned int

⻓整型
long [int]
[signed] long [int]
unsigned long [int]

Longer integer types (introduced in C99)
long long [int]
[signed] long long [int]
unsigned long long [int]

1.3 Floating point type
float Single precision floating point type
double Double precision floating point type
long double

1.4. Boolean type (introduced since C99)
1. The Boolean type is represented by _Bool
2. The use of the Boolean type requires referencing the header file <stdbool.h>
3. The value of the Boolean type variable is true or false

//布尔类型
#include <stdio.h>
#include <stdbool.h>
int main() {
    
    
	_Bool flag = true;
	if (flag) {
    
    
		printf("i'm true");
	}
	else{
    
    
		printf("i'm false");
	}
	return 0;
}

Insert image description here

2.signed和unsigned

C language uses signed and unsigned keywords to modify character and integer types.
The signed keyword indicates that a type has a sign and contains negative values;
the unsigned keyword indicates that the type does not have a sign and can only represent zero and positive integers (non-negative).

Therefore, when introducing data types above, if the data type is required to represent only non-negative integers, it must be modified with the unsigned keyword; otherwise, it can be modified with the signed keyword, but most data type ranges themselves include positive integers. Negative, so signed can basically be omitted.

example:

//定义整型变量a
signed int a;
int a;//signed可以省略

//定义非负整型变量a
unsigned int a;

Taking the integer type as an example, what are the benefits of declaring an int variable unsigned?
The advantage is that the maximum integer value that can be represented by the same memory length is twice as large.
For example:
The value range of 16-bit signed short int is: -32768~32767, the maximum value is 32767;
while the value range of unsigned short int is: 0~65535, the maximum value is 65535

3. Value range of data type

The limits.h file describes the value range of the integer type.
The float.h file describes the value range of the floating point type.

Storage size and value range of integer types
Insert image description here

Storage size and value range of floating point types
Insert image description here


Constants SCHAR_MIN and SCHAR_MAX for the limit values ​​of the integer type value range : the minimum and maximum values ​​of signed char.
SHRT_MIN, SHRT_MAX: The minimum and maximum values ​​of short.
INT_MIN, INT_MAX: The minimum and maximum value of int.
LONG_MIN, LONG_MAX: The minimum and maximum values ​​of long.
LLONG_MIN, LLONG_MAX: The minimum and maximum values ​​of long long.
UCHAR_MAX: The maximum value of unsigned char.
USHRT_MAX: The maximum value of unsigned short.
UINT_MAX: The maximum value of unsigned int.
ULONG_MAX: The maximum value of unsigned long.
ULLONG_MAX: The maximum value of unsigned long long.

code demo

#include <limits.h>
int main() {
    
    
//存储大小
	printf("char的存储大小:%d\n",sizeof(char));
	printf("short的存储大小:%d\n", sizeof(short));
	printf("int的存储大小:%d\n", sizeof(int));
	printf("long的存储大小:%d\n", sizeof(long));

	//short int和long int 就是 short和long 这定义类型可以省略int 
	printf("short int的存储大小:%d\n", sizeof(short int));
	printf("long int的存储大小:%d\n", sizeof(long int));

	printf("long long的存储大小:%d\n", sizeof(long long));
	printf("unsigned char的存储大小:%d\n",sizeof(unsigned char));
	printf("unsigned short的存储大小:%d\n",sizeof(unsigned short));
	printf("unsigned int的存储大小:%d\n",sizeof(unsigned int));
	printf("unsigned long的存储大小:%d\n",sizeof(unsigned long));
	printf("unsigned long long的存储大小:%d\n",sizeof(unsigned long long));

	printf("float的存储大小:%d\n", sizeof(float));
	printf("double的存储大小:%d\n", sizeof(double));
	printf("long double的存储大小:%d\n", sizeof(long double));//long double字节大小至少8字节,不同编译器得出的结果不同,但肯定大于等于double的8字节

	printf("--------------------------------------------\n");
	
//取值范围
	printf("char的取值范围:%d %d\n", SCHAR_MIN, SCHAR_MAX);
	printf("unsigned char的取值范围:%d\n", UCHAR_MAX);	

	printf("short的取值范围:%d %d\n", SHRT_MIN, SHRT_MAX);
	printf("unsigned short的取值范围:%d\n", USHRT_MAX);

	//%d 十进制有符号整数   
	printf("int的取值范围:%d %d\n", INT_MIN, INT_MAX);
	//%u (16位)十进制无符号整数(unsigned int的最大存储范围已经超出了%d所能表示的范围)
	printf("unsigned int的取值范围:%u\n", UINT_MAX);

	//%d=int  %ld=long  %lld=long long(long和long long的取值范围都已经超出了int的范围 所以对应的格式化占位符也要修改 否则会出错

	//%ld [32位]长整型数据类型
	printf("long的取值范围:%ld %ld\n", LONG_MIN, LONG_MAX);
	//%lu [32位]无符号长整型数据类型
	printf("unsigned long的取值范围:%lu\n", ULONG_MAX);

	//%lld 64位长整型数据类型
	printf("long long的取值范围:%lld %lld\n", LLONG_MIN, LLONG_MAX);
	//%llu 64位无符号长整型数据类型
	printf("unsigned long long的取值范围:%llu\n", ULLONG_MAX);

	return 0;
}

Insert image description here

4.Variables

Variables are values ​​that can be changed in C language. When creating a variable, you need to define a data type for the variable.

Constants are values ​​that do not change in C language. For example, the value range of the integer type mentioned above is
INT_MIN, and INT_MAX represents the minimum and maximum value range of the int type. These two are constants.

Defining variables
is very simple, data type + variable name (the variable name should not conflict with keywords)

char a;//字符变量
int b;//整型变量
float c;//浮点型变量

Initialize a variable.
Give it an initial value when the variable is created.
The initial value must correspond to the data type.

int a = 1;
char b = '1'
float c = 1.1;

Do not initialize variables.
Integer variables (int, short, long, etc.): the default value is 0.
Floating point variables (float, double, etc.): The default value is 0.0.
Character variable (char): The default value is '\0', which is the null character.

5. Global variables and local variables

Global variables: Variables defined outside curly braces are global variables. Global variables
have a wide range of influence, involve the entire project, and can be referenced anywhere.

Local variables: Variables defined inside curly braces are local variables.
Local variables can only affect the local scope and can generally be referenced within their own curly braces.

#include <stdbool.h>
//全局变量和局部变量
int global = 1;//全局变量
int main() {
    
    
	int local = 2;//局部变量
	if (true){
    
    
		int local_in = 3;//局部变量
		printf("局部变量:%d\n", local_in)//这里就可以调用local_in局部变量
	}

	printf("全局变量:%d\n", global);
	printf("局部变量:%d\n", local);
	//printf("局部变量:%d\n", local_in); //无法调用局部变量local_in,报错
}

If the local variable being called is defined within curly braces that is more internal than the call point, the local variable cannot be called. Taking the
above int local_in = 3 as an example, when local_in is called by external printing, the definition of local_in that is more internal than itself cannot be found. So the call fails and an error is reported.

If the local variable and global variable have the same variable name

#include <stdbool.h>
//全局变量和局部变量
int global = 1;//全局变量
int main() {
    
    
	int global = 2;//局部变量
	printf("全局变量:%d\n", global);
}

Debug result
Insert image description here
Reason: The internal local variable overwrites the value of the global variable. (The overwriting here does not refer to modifying the int global = 1 of the global variable, but overwriting the global value in the printing step, and the originally defined values ​​remain unchanged)

Where local variables and global variables are stored in memory (understand)
1. Local variables are stored in the stack area of ​​​​the memory
2. Global variables are stored in the static area of ​​​​the memory

6. Arithmetic operators (+, -, *, /, %)

**+ - * / % **These arithmetic operators are all binary operators.
Note: Binary operator means that the operator has two operands, that is, there is a number at each end of the operator.

//算数运算符
int main() {
    
    
	//加 减 乘(+ - *)
	int a = 1, b = 2, c = 3;
	int add = a + b + c;
	int sub = c - b - a;
	int mul = a * b * c;
	printf("add=%d  sub=%d  mul=%d\n", add, sub, mul);

	return 0;
}

Insert image description here



Addition, subtraction, and multiplication are very regular. Let’s discuss division separately! ! !

int main() {
    
    
	// 除(/)
	int a = 1, b = 2, c = 3;
	float bf = 2, cf = 3;

	int div_i = c / b;
	float div_f1 = c / b;
	float div_f2= cf / bf;

	printf("div_i=%d\n", div_i);//1
	printf("div_f1=%f\n", div_f1);//1.000000
	printf("div_f2=%f\n", div_f2);//1.500000

	return 0;
}

Insert image description here
Three different results appear and are discussed one by one.
Tip: Integer division in C language is an integer division. It will only return the integer part and discard the decimal part.

1. printf(“div_i=%d\n”, div_i);//1
formula int div_i = c / b The result variable div_i type is int, and the operands c and b at both ends of the operator are both int type
. The process is:
divide two int variables c and b, the result is 1, assign it to the variable div_i
and because div_i is an int type, so div_i=1
and then use the placeholder %d to output and print

2. printf(“div_f1=%f\n”, div_f1);//1.000000
formula float div_f1 = c / b The result variable div_f1 type is float, and the operands c and b at both ends of the operator are both int type
. The process is:
divide two int variables c and b, the result is 1, assign it to the variable div_f1
and because div_f1 is a float type, so div_f1=1.000000
and then use the placeholder %f to output and print

3. printf(“div_f2=%f\n”, div_f2);//1.500000
formula float div_f1 = cf / bf The result variable div_f2 type is float, and the operands c and b at both ends of the operator are both float types
Note: The variables here are float type divisions ( as long as at least one float variable participates in the operation, C language will perform floating point division ). If floating point numbers are divided, the division will not be an integer division.
According to the process:
divide two float variables c and b, the result is 1.500000, assign it to the variable div_f2
because div_f2 is a float type, so div_f2=1.500000
and then use the placeholder %f to output and print



The operator % represents the modulo operation, which returns the remainder of the division of two integers.
(This operator can only be used on integers, not floating point numbers)

//取模%
int main() {
    
    
	int b = 2, c = 3;
	int mod = c % b;
	printf("mod=%d", mod);//1
	return 0;
}

The rule for modulo negative numbers is that the sign of the result is determined by the sign of the first operand.

int main() {
    
    
	printf("%d\n", 11 % -5); // 1
	printf("%d\n", -11 % -5); // -1
	printf("%d\n", -11 % 5); // -1
	return 0;
}

7. Assignment operator (=)

When a variable is created, an initial value is initialized. After the variable is created, assigning a value is called assignment.

//赋值运算符
int main() {
    
    
	int a = 100;//初始化
	a = 101;//赋值
	return 0;
}



continuous assignment

//连续赋值
int main() {
    
    
	int a = 100;//初始化
	int b = 101;//初始化
	int c = 102;//初始化
	a = b = c + 98;//不推荐变量连接成一块,看着费劲
	
	//建议分开写,方便观察读写
	b = c + 98;
	a = b;
	printf("a=%d b=%d c=%d",a,b,c);//从右向左依次赋值的 200 200 102
	
	return 0;
}

Assigned values ​​from right to left

Insert image description here


compound assignment operator

//复合赋值
int main() {
    
    
	int a = 100;
	int b = 100;
	a += 3;//等同于 a = a + 3
	b -= 3;//等同于 b = b - 3

	printf("a=%d\n", a);//103
	printf("b=%d\n", b);//97

	return 0;
}

Commonly used compound assignment operators

*= /= %= /= %=
>>= <<= &= |= ^=

8. Unary operators (++, –, + (positive), - (negative))

The operators ++, - -, + (positive), and - (negative) all have only one operand, so they are unary operators.

+ (Positive) - (Negative)
I won’t introduce too much about the positive and negative signs. They are positive and negative in mathematics. The positive
sign (+) can basically be omitted. You can write it or not.
The negative sign (-) is to make the number negative. It’s also a very simple truth, but I won’t introduce it in too much detail.

++ and - -
++ increase themselves, - - decrease themselves. The function of ++ is to make the variable +1, and the function of - - is to make the variable -1.
++ and - - can be placed before or after the operand. Post-positioning and post-positioning will cause the order of +1 to be different.

example

int main() {
    
    
	int a = 1;
	int b = a++;
	printf("b=%d\n", b);//b=1
	printf("a=%d", a);//a=2
	return 0;
}

The above example is auto-increment followed by
int b = a++. First assign a=1 to b, so b=1
and then auto-increment a++ (equivalent to a = a+1), so a=2


int main() {
    
    
	int a = 1;
	int b = ++a;
	printf("b=%d\n", b);//b=2
	printf("a=%d", a);//a=2
	return 0;
}

The above example is an auto-increment prefix
int b = ++a. First auto-increment a++ (equivalent to a = a+1), so a=2
and then assign a=2 to b, so b=2

In the same way, the prefix and postfix of self-decrement can be obtained, so I won’t give an example^ _ ^

9. Forced type conversion

int main() {
    
    
	int a = 3.14;
	printf("a=%d", a);
	return 0;
}

Insert image description here
A warning will pop up, and some compilers will report an error directly. Converting floating point numbers to integers will sacrifice precision and lose the decimal part of the data.


In order to eliminate the warning, you need to use forced type conversion and add (type) before the value or type that needs to be converted.

//强制类型转换
int main() {
    
    
	int a = (int)3.14;//浮点数前加上  (强制转换的类型)
	printf("a=%d", a);
	return 0;
}

It runs successfully without error or warning, but still loses accuracy.

Guess you like

Origin blog.csdn.net/qq_45657848/article/details/131846662