c语言的关键字

文章详细解释了C语言中的关键字如auto、static和typedef的作用,重点讨论了static如何改变变量存储位置、影响生命周期以及在全局变量和函数上的应用。同时提到了register的含义及其在内存存储中的特殊情况。
摘要由CSDN通过智能技术生成

关键字是c语言设定好的,不可自己创建

auto 、break、case、char、 const、continue、default、do、double、else、enum、extern、float、for、goto、if、int、long、register、return、short、signed、sizeof、static、struct、switch、typeddef、union、unsigned、void、volatile、while

typedef---类型重命名

 static---用来修饰变量和函数的,修饰全局、局部变量和函数。

对全局变量的修饰

#include<stdio.h>
void test()
//void是不让程序有返回值
{
    int a = 1;
    a++;
    printf("%d\n", a);

}
int main()
{
    int i = 0;
    while (i < 10)
    {
        test();
        i++;
    }
    return 0;
}

加上static后的结果

#include<stdio.h>
void test()
//void是不让程序有返回值
{
    static int a = 1;
    a++;
    printf("%d\n", a);

}
int main()
{
    int i = 0;
    while (i < 10)
    {
        test();
        i++;
    }
    return 0;
}

本质上,static改变了变量的存储位置,影响了变量的生命周期,和程序的生命周期一样长了。

将储存位置从栈区变到了静态区

对全局变量的修饰、

#include<stdio.h>
int a=20;
int main()
{
    printf("%d", a);
    return 0;
}

加上static后程序报错

原因是static使得全局变量的外部链接变成了内部链接,其他源文件就不能使用这个全局变量了。

对函数的修饰

#include<stdio.h>
 int add (int x, int y)
{
    return x + y;
}
int main()
{
    int a = 10;
    int b = 20;
    int z = add (a, b);
    printf("%d", z);
    return 0;
}

#include<stdio.h>
 static int add (int x, int y)
{
    return x + y;
}
int main()
{
    int a = 10;
    int b = 20;
    int z = add (a, b);
    printf("%d", z);
    return 0;
}

输出错误的原因是static将函数的外部链接变成了内部链接,其他源文件就不能使用这个全局变量了。

register-----寄存器

电脑的存储设备

寄存器

高速缓存

内存

硬盘

int main()

{

        //寄存器变量

        register int num=3;

//建议把3放在寄存中(放不放的进去还要看编译器本身)

        return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值