失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > C语言auto register static extern关键字

C语言auto register static extern关键字

时间:2022-02-23 12:37:33

相关推荐

C语言auto register static extern关键字

1.auto

#include<stdio.h>#include<stdlib.h>#include<string.h>int a = 0;void show(){a++;printf("hello: %d\n",a);}void main(int *argv, char *args[]){show();/**Auto变量:局部变量不作任何说明,都作为自动变量auto,及动态存储的。Auto可省略。*/volatile unsigned auto int b = 10;}

2.register

#include<stdio.h>#include<stdlib.h>#include<string.h>int a = 0;void show(){a++;printf("hello: %d\n",a);}void main(int *argv, char *args[]){show();/**Register变量:暗示编译程序相应的变量将将被频繁使用,如果可能的话,应将其保存在CPU的寄存器中,以指加快其存取速度,有几点使用限制:1 register变量必须是能被CPU寄存器所接受的类型2为register变量可能不存放在内存中,所以不能用取址符运算符“& ”来获取取址符运算符3只有局部变量和形参可以作为register变量,全局变量不行4 80x86系列CPU最多可使用的register变量数目有限。int型可使用8个通用寄存器5 静态变量不能定义为register。*/register int b = 10;}

3.static

#include<stdio.h>#include<stdlib.h>#include<string.h>int A;void show(){volatile static int a ;//静态局部变量(static) 静态局部变量定义时前面加static关键字。a++;f1(a);f2(a);//printf("show: B = %d\n",B);}void main(int *argv, char *args[]){show();show();show();show();int c;static int b = 10;}/**//head1.c#include<stdio.h>#include<stdlib.h>#include<string.h>static int B ;int A;void f1(int a){A++,B++;printf("head1.h:f1(%d), A = %d, B = %d\n",a,A,B);}//head2.c#include<stdio.h>#include<stdlib.h>#include<string.h>static int B;int A;void f2(int a){A += 2, B += 2;printf("head2.h:f2(%d), A = %d, B = %d\n",a,A,B);}*//**-bash-4.1$ gcc -o a Demo.c head*.c-bash-4.1$ ./ahead1.h:f1(1), A = 1, B = 1head2.h:f2(1), A = 3, B = 2head1.h:f1(2), A = 4, B = 2head2.h:f2(2), A = 6, B = 4head1.h:f1(3), A = 7, B = 3head2.h:f2(3), A = 9, B = 6head1.h:f1(4), A = 10, B = 4head2.h:f2(4), A = 12, B = 8-bash-4.1$ */

4.extern

#include<stdio.h>#include<stdlib.h>#include<string.h>void main(int *argv, char *args[]){extern const int num;extern void show();show();printf("main: num = %d\n",num);}/**//extern.c#include<stdio.h>#include<stdlib.h>#include<string.h>const int num = 3;void show(){printf("show: num = %d\n",num);}*/

如果觉得《C语言auto register static extern关键字》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。