常见关键字的一般用法

auto

--自动的--每个局部变量都是auto修饰的~

#include<stdio.h>

int main()
{
    int a = 0;
    return 0;
}

也可以说

#include<stdio.h>

int main()
{
    auto int a = 0;
    return 0;
}

extern

--用来声明外部符号的~

register

--寄存关键词

#include<stdio.h>

int main()
{
    register int a = 100;
//建议a的值存放在寄存器中

    return 0;
}

static

--静态的

1.static修饰局部变量

代码1

#include<stdio.h>

void test()

{

int a = 1;

a++;

printf("%d\t", a);

}

int main()

{

int i = 0;

while (i <10)

{

test();

i++;

}

return 0;

}

结果:2 2 2 2 2 2 2 2 2 2

当用static修饰int a = 1;时,结果如何?

代码2

#include<stdio.h>

void test()

{

static int a = 1;

a++;

printf("%d\t", a);

}

int main()

{

int i = 0;

while (i <10)

{

test();

i++;

}

return 0;

}

结果:2 3 4 5 6 7 8 9 10 11

为什么?

static--静态的,在代码1运行时,当i在运行时,只要小于10就可以一直运行,共运行了10次,故有十个结果,二为什么全输出的是2呢?代码1原因是每次计算都是从a = 1开始运算,然后计算a++,结果就是1+1=2,\t是字符串--水平制表符,故结果为2 2 2 2 2 2 2 2 2 2

对于代码2,通过static修饰后,将int a = 1变为静态的,这样每次结果都会固定上一次计算结果,将动态值变成静态值,这样第1次,1+1=2,第2次2+1=3,第3次3+1=4,这样进行着循环语句控制的代码,计算到第十次结束,结果为2 3 4 5 6 7 8 9 10 11

2static修饰全部变量

代码1

源文件1

#include<stdio.h>

int grval;

int main()

{

printf("%d\n", grval);

return 0;

}

源文件2

int grval = 2023;

结果:2023

代码2

源文件1

#include<stdio.h>

int grval;

int main()

{

printf("%d\n", grval);

return 0;

}

源文件2

static int grval = 2023;

结果:0

解释:

static修饰全局变量,使得这个全局变量只能在自己所在的源文件内部可以使用,其他源文件不能使用!

全局变量,在其他源文件内部可以被使用,是因为全局变量具有外部连接属性,但是被static修饰之后,就变成内部连接属性,其他源文件不能连接到这个静态全局变量了!

代码3

源文件1

#include<stdio.h>

int grval;

int main()

{

printf("%d\n", grval);

return 0;

}

源文件2

extern int grval = 2023;

结果:2023

3.extern修饰函数

代码1

源文件

#include<stdio.h>

extern int Add(int, int);

int main()

{

int a = 10;

int b = 20;

int mun = Add(a, b);

printf("%d\n", mun);

return 0;

}

函数源文件

int Add(int x, int y)

{

return x + y;

}

结果:30

代码2

源文件

#include<stdio.h>

extern int Add(int, int);

int main()

{

int a = 10;

int b = 20;

int mun = Add(a, b);

printf("%d\n", mun);

return 0;

}

函数源文件

static int Add(int x, int y)

{

return x + y;

}

结果:报错

static修饰函数,使得函数只能在自己所在的源文件内部使用,不能在其他源文件使用

本质上:static是将函数的外部链接属性变成了内部属性!(和static修饰全局变量相同)

singed

--有符号的<-->无符号的--unsinged

typedf

--类型重定义(重命名)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值