c语言常见关键字typedef和static的用法

enum 枚举

struct 结构体

union 联合体(公用体)

extern 声明外部符号

register 寄存器

signed 有符号的

unsigned

static 静态的

typedef 类型重命名

void 无类型(函数返回类型)

一.typedef

//typedef 类型定义  把一个复杂的类型简化

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>

typedef unsigned int uint; //把unsigned int重新起名为uint

typedef struct node

{

int data;

struct node* next;

}node;

int main()

{

auto int a = 10;

unsigned int num = 0;

uint num2 = 1;   //那么uint = unsined int

struct node n;

node n2; //这是简化后的效果,与struct node n2效果相同

return 0;

}

二.static的三个作用

//static

//1.修饰局部变量

//2.修饰全局变量

//3.修饰函数

1.修饰局部变量

void test()

{

static int a = 1; //修饰局部变量,局部变量出了作用域不销毁。

//本质上,static改变了变量的存储位置,从不加时候应该存在的栈区到静态区,影响了变量的生命周期,和程序的生命周期一样

a++;

printf("%d\n", a);//因此,依次输出2~11的整数

}

int main()

{

int i = 0;

while (i < 10)

{

test();

i++;

}

return 0;

}

2.修饰全局变量

static int g_val = 2022;

//static 修饰全局变量的时候,全局变量的外部链接属性就变成了内部链接属性

//其他源文件就不能再使用这个全局变量,使用范围变小了

//仍然储存在静态区

3.static 修饰函数

首先,函数具有外部链接属性

extern int add(int x, int y);   //可以调用其它源文件中的函数

static int add(int x, int y)

{

return x + y;

}

//此时add()函数不再具有外部链接属性,为内部链接属性,其它源文件无法使用

三.register

创建寄存器变量

register int num = 3;   //建议3存放在寄存中,那么它的访问速度可能会更快

  • 13
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值