1. Data type, variable, constant, scope, life cycle

type of data

char	//字符数据类型 - 1byte
short	//短整型 - 2byte
int		//整型 - 4byte
long	//长整型 - 4byte
long long	//更长的整型 - 8byte
float	//单精度浮点数 - 4byte
double	//双精度浮点数 - 8byte
//C语言中有没有字符串类型?
  • Why such a type?

    Use computer language to write programs, solve problems in life, and must have the ability to describe problems in life. For example: shopping mall - price - 15.6 yuan - decimal.

    Note: There are so many types, in fact, to express various values ​​in life more abundantly

  • What is the size of each type?

#include <stdio.h>

int main()
{
    
    
	//sizeof - 关键字 - 操作符 - 计算类型或变量所占空间的大小
    //sizeof单位是什么? - 字节 byte
	printf("%d\n", sizeof(char));	//1byte
	printf("%d\n", sizeof(short));	//2byte
	printf("%d\n", sizeof(int));	//4byte
	printf("%d\n", sizeof(long));	//4byte
	printf("%d\n", sizeof(long long));	//8byte
	printf("%d\n", sizeof(float));	//4byte
	printf("%d\n", sizeof(double));	//8byte

	return 0;
}

Units in the computer:

bit - 比特位 
byte - 字节 = 8bit
KB - 1024byte
MB - 1024KB
GB - 1024MB
TB - 1024GB
PB - 1024TB

variable, constant

constant - a quantity that cannot be changed

variable - a quantity that can be changed

How to define variables

int age = 18;
float weight = 45.5f;
char ch = 'w';

Classification of variables

  • local variable
  • global variable
//全局变量 - {}外部定义的变量
int a = 100;

int main() {
    
    
	//局部变量 - {}内定义的变量
	//当局部变量和全局变量名字冲突的情况下,局部优先
	//不建议把全局变量和局部变量名字写成一样
	int a = 10;
	printf("%d", a);	//10

	return 0;
}

use of variables

scope

//作用域和生命周期
//局部变量的作用域:就是变量所在的局部范围
//全局变量的作用域:整个工程

int g_val = 2023;//全局变量
int main()
{
    
    
	printf("1:%d\n",g_val);
	{
    
    
		printf("2:%d\n", g_val);
		int a = 10;
		printf("a = %d\n", a);
	}
	printf("3:%d\n", g_val);

	return 0;
}

//执行结果如下:
1:2023
2:2023
a = 10
3:2023

lifetime - the period of time between the creation and destruction of a variable

The life cycle of local variables: enter the local scope life begins, exit the local scope life end

Life cycle of global variables: program life cycle

constant

Constants in C language are divided into the following categories:

  • literal constant
  • const modified constant
  • Identifier constants defined by #define
  • enumeration constant
#define MAX 1000
enum Sex
{
    
    
	//这种枚举类型的变量的未来可能取值
	//枚举常量
	MALE = 3,//赋初值
	FEMALE,
	SECRET
};

int main()
{
    
    
	//1.字面常量
	3.14;
	10;
	'a';
	"abcd";

	//2.const修饰的常量
	const int num = 10;//num就是常变量 - 具有常属性(不能被改变的属性)

	//3.#define 定义的标识符常量
	int n = MAX;

	//4.枚举常量
	//可以一一列举的常量
	enum Sex s = MALE;

	return 0;
}

Guess you like

Origin blog.csdn.net/Anakin01/article/details/130787062