b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

c 类 static 函数外部链接:多文件中使用内部链

电脑杂谈  发布时间:2018-02-01 04:02:45  来源:网络整理

static int_c   类 static 函数_ucase函数

c 类 static 函数

外部链接:多文件中使用

内部链接:只在一个翻译单元(文件)内使用

无链接 :具有块,

1.自动储存期,

2.块作用域,

3.无链接

4.auto关键字(显式)

void doSome(){
    int d = 3;
};
int main(int argc, const char * argv[]) {
    int a = 0;
    for (int i = 0 ;  i < 10 ; i++) {
        int b = 1;
    }
    {
       auto int c = 2;
    }
    return 0;
}
// a,b,c,d都是自动变量

1.static显式声明,

static int_c   类 static 函数_ucase函数

2.静态储存期,

3.内部链接,

4.文件作用域.

static int statc_a = 2; //只能在main.c中访问
int main(int argc, const char * argv[]) {
    printf("%d",statc_a);
    return 0;
}

1.静态储存期,

2.外部链接,

3.文件作用域.

/* sub.h */
#include "sub.h"
int sub_a = 2;

/* main.c */

#include <stdio.h>

int main(int argc, const char * argv[]) {
    extern int sub_a;
    printf("sub_a : %d\n",sub_a);
    return 0;
}
/*
sub_a : 2
Program ended with exit code: 0
*/
//编译器会找到sub_a这个变量的。

这样使用静态外部链接的存储类别变量是有问题的,如果全局的变量名有重复的就会报错,比如在main.c中又定义了一个相同名字的sub_a ,编译无法通过,显示链接错误。

ucase函数_static int_c   类 static 函数

#include <stdio.h>

int sub_a = 2;

int main(int argc, const char * argv[]) {
    extern int sub_a;
    printf("sub_a : %d\n",sub_a);
    return 0;
}
//clang: error: linker command failed with exit code 1 (use -v to see invocation)

一个比较规范办法是使用头文件,在头文件中申明需要外部使用的变量extern

/* .h */
#include <stdio.h>
extern int sub_a;
#endif /* sub_h */

/* main.c */
#include <stdio.h>
#include "sub.h"

int main(int argc, const char * argv[]) {
    printf("sub_a : %d\n",sub_a);
    int sub_a = 3;
    printf("sub_a : %d\n",sub_a);
    return 0;
}
//但是依然并不能完全解决我们上面产生的问题。

1.静态储存期,

2.无连接,

3.块作用域

PS:静态变量只能初始化一次,并且定义时候必须赋值。

static:在声明局部变量时,使用关键字 static 将局部变量指定为“静态局部变量”,这样在函数调用结束后不消失而保留原值,即占用的存储单元不释放,在下一次函数调用时,该变量已有值就是上次函数调用结束时的值。c 类 static 函数

1.在块作用域内将变量的存储期变成静态

void add(){
    static int static_a = 1;
    int a = 1;
    printf("static_a:%d---a:%d\n",static_a,a);
    static_a++;
    a++;
}
//调用三次
static_a:1---a:1
static_a:2---a:1
static_a:3---a:1

2,将文件作用域内的变量从外部链接变成内部链接


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-64247-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      • 高丽丽
        高丽丽

        只要官不贪

      • 张洪圣
        张洪圣

        快快

        • 喜安浩平
          喜安浩平

          但诺列加虽是毒枭却很爱国

      • 嫣花
        嫣花

        全民屏住那口气直到民族必须一战时一举打趴日本军国主义才是可以高调时

        • 利基
          利基

          #杨洋icon##杨洋微微一笑很倾城##杨洋肖奈##杨洋轻奢young#

      热点图片
      拼命载入中...