常见关键字

auto , break , case , cha r, const , continue , default , do  , double , else , enum , extern

float , for , goto , int , long , register , return , short , signed , sizeof , static , struct , switch

typedef , funion , unsigned , void , volatile , while

//auto -自动
#include <stdio.h>
int main()
{
auto int a = 10; //局部变量 -自动变量 创建销毁
return 0
}


register 寄存器关键字

计算机存储数据:

寄存器

高速缓存

内存  8G/4G/16G

硬盘  500G

//signed  unsigned 无符号
#include <stdio.h>
int main()
{
//int 定义的变量是有符号的 signed int;
int a = 10;
a = -2;
unsigned int num = -1; //永远都是正数
return 0
}
//typedef -类型定义 -类型重定义
#include <stdio.h>
int main()
{
typedef unsigned int u_int;
unsigned int num = 20;
u_int num2 = 20;
return 0;
}


static

static修饰局部变量

局部变量的生命周期边长

#include <stdio.h>
void test()
{
static int a = 1; //a是一个静态的局部变量
a++;
printf("a = %d\n",a);
}
int main()
{
int i = 0;
while(i<5)
{
test();
i++;
}
return 0;
}


static修饰全局变量

改变了变量的作用域 -让静态的全局变量只能在自己所在的源文件内部使用,出了源文件就没法再使用了

static int g_val = 2021;
#include <stdio.h>
int main()
{
//extern -声明外部符号的
extern int g_val;
printf("g_val = %d\n",g_val);
return 0;
}


static修饰函数

也是改变了函数的作用域 -说法不准确

static修饰函数改变了函数的链接属性

外部链接属性 -->内部链接属性

static int Add(int x,int y)
{
int z = 0;
z = x + y;
return z;
}


#include <stdio.h>
extern int Add(int,int);
int main()
{
int a =10;
int b =20;
int sum =Add(a,b);
printf("sum = %d\n",sum);
return 0;
}



//#define 定义函数和宏
#include <stdio.h>
#define MAX(x,y) (x>y?x:y)
int main()
{
int a = 10;
int b = 20;
max = MAX(a,b);
printf("max = %d\n",max);
return 0;
}


指针

#include <stdio.h>
int main()
{
int a = 10; //4个字节
int* p = &a; //&取地址
//有一种变量是用来存放地址的 -指针变量
printf("%p\n",&a);
printf("%p\n",p);
*p = 20; //* -解引用操作符
printf("a = %d\n",a);
return 0;
}
#include <stdio.h>
int main()
{
char ch = 'w';
char* pc = &ch;
*pc = 'q';
printf("%c\n",ch);
return 0
}


指针变量的大小

指针大小在32位平台上是4个字节,64位平台上是8个字节

#include <stdio.h>
int main()
{
int a = 10; //申请了四个字节的空间
printf("%p\n",&a);
int* p = &a; //p是一个变量 -指针变量
printf("%p\n",p);
*p = 20; //* -解引用操作符/间接访问操作符
printf("a = %d\n",a);
return 0;
}
#include <stdio.h>
int main()
{
double d = 3.14;
double* pd =&d;
*pd = 5.5;
printf("%lf\n",d);
printf("%lf\n",*pd);
return 0;
}


结构体

复杂对象 -结构体 -我们自己创造出来的一种类型

#include <stdio.h>
//创建一个结构体类型
struct Book
{
char name[20];
short price;
}; //用来结束这个类型定义

// . (结构体变量.成员)
#include <stdio.h>
int main()
{
//利用结构体类型 -创建一个该类型的结构体变量
struct Book b1 = {"C语言程序设计",55}
printf("书名:%s\n",b1.name);
printf("价格:%d\n",b1.price);
b1.price = 15;
printf("修改后的价格:%d\n",b1.price);
strcpy(b1.name,"C++"); //数组名本质上是个地址 strcpy -string copy -字符串拷贝 -库函数
printf("修改后的书名:%s\n",b1.name);
return 0;
}
#include <stdio.h>
//创建一个结构体类型
struct Book
{
char name[20];
short price;
}; //用来结束这个类型定义
//-> (结构体指针->成员)
int main()
{
struct Book b1 = {"C语言程序设计",55}
struct Book* pb = &b1;
printf("%s\n",(*pb).name);
printf("%d\n",(*pb).price);
printf("%s\n",pb->name);
printf("%d\n",pb->price);
return 0;
}