ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

C语言关键字static extern const

2022-06-07 00:31:45  阅读:135  来源: 互联网

标签:const 变量 int void num static extern main


一.关键字static

有三个作用:隐藏性,记忆性,初始化为0.

具有隐藏的作用:

当同时编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可见性。

a.c:

1 char a = 'A';  //  global variable
2 void msg() 
3 {
4     printf("Hello\n"); 
5 }

main.c:

1 int main( void)
2 {    
3      extern  char a;     //  extern variable must be declared before use
4     printf("%c ", a);
5     ( void)msg();
6      return 0;
7 }

程序的运行结果是:

A Hello

在a.c中定义的全局变量a和函数msg能在main.c中使用?前面说过,所有未加static前缀的全局变量和函数都具有全局可见性。

保持变量内容的持久:

变量不加 static 修饰

 1 #include <stdio.h>
 2  
 3 void test()
 4 {
 5     int num = 0;
 6     num++;
 7     printf("%d ", num);
 8 }
 9 int main()
10 {
11     int i = 0;
12     for (i = 0; i < 10; i++)
13     {
14         test();
15     }
16     return 0;
17 }

输出:1 1 1 1 1 1 1 1 1 1 1

变量被 static 修饰

 1 #include <stdio.h>
 2  
 3 void test()
 4 {
 5     static int num = 0;
 6     num++;
 7     printf("%d ", num);
 8 }
 9 int main()
10 {
11     int i = 0;
12     for (i = 0; i < 10; i++)
13     {
14         test();
15     }
16     return 0;
17 }

输出:1 2 3 4 5 6 7 8 9 10

默认初始化为0:

1 static int x;  //初始化时x=0;

 

二.关键字extern

引用同一个文件中的变量:变量的调用在变量声明的前面。

 1 #include<stdio.h>
 2  
 3 int func();
 4  
 5 int main()
 6 {
 7     func(); //1
 8     extern int num;
 9     printf("%d",num); //2
10     return 0;
11 }
12  
13 int num = 3;
14  
15 int func()
16 {
17     printf("%d\n",num);
18 }

引用另一个文件中的变量和函数:这个变量应该是全局的。

b.c:

1 char a = 'A';  //  global variable
2 void msg() 
3 {
4     printf("Hello\n"); 
5 }

main.c

1 int main( void)
2 {    
3      extern  char a;     //  extern variable must be declared before use
4     printf("%c ", a);
5     ( void)msg();
6      return 0;
7 }

 

三.关键字const

在定义该const变量时,需先初始化,以后就没有机会改变他了。

const修饰一般变量的时候:

1 int const number =2   或者   const int number = 2;

const 修饰函数的参数:

1 void StringCopy(char *strDestination, const char *strSource);

给strSource 加上const修饰后,如果函数体内的语句试图改动strSource 的内容,编译器将指出错误。

canst修饰指针:

规则:const离谁近,谁就不能被修改;

 1 ​
 2 int a = 5;
 3     
 4     // 第一种
 5     const int *p1;        // p本身不是cosnt的,而p指向的变量是const的
 6     // 第二种
 7     int const *p2;        // p本身不是cosnt的,而p指向的变量是const的
 8     
 9     
10     // 第三种
11     int * const p3;        // p本身是cosnt的,p指向的变量不是const的
12     // 第四种
13     const int * const p4;// p本身是cosnt的,p指向的变量也是const的
14     
15     *p1  = 3;        // error: assignment of read-only location ‘*p1’
16     p1 = &a;        // 编译无错误无警告
17     
18     *p2 = 5;        // error: assignment of read-only location ‘*p2’
19     p2 = &a;        // 编译无错误无警告
20     
21     *p3 = 5;        // 编译无错误无警告
22     p3 = &a;        // error: assignment of read-only variable ‘p3’
23     
24     p4 = &a;        // error: assignment of read-only variable ‘p4’
25     *p4 = 5;        // error: assignment of read-only location ‘*p4’
26  
27 ​

 

参考链接:

https://blog.csdn.net/weixin_49303682/article/details/118643958

https://blog.csdn.net/weixin_49303682/article/details/118652834

https://blog.csdn.net/weixin_49303682/article/details/118653208

 

标签:const,变量,int,void,num,static,extern,main
来源: https://www.cnblogs.com/xiaogudexuexibiji/p/16350277.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有