//仅适用于超级菜鸟及初初初初学者,共同学习,高手请query其他文章哈。 /笑哭
基础知识部分:
1.关键字extern:仅仅是声明,而不是定义(个人感觉此处像是JAVA中的声明,在栈中创建一个引用)且将参数声明为外部变量。例如:
1.
extern int i; //声明,不是定义
int i; //声明,也是定义
//file Testa.c
#include <stdio.h>
extern int x;
extern int y;
int addTwo() {
return x+y;
}
//file Testb.c
#include <stdio.h>
int x = 1;
int y = 2;
int addTwo();
int main(void) {
int result;
result = addTwo();
printf("result %d", result);
return 0;
}
可以在使用时赋值。
2.存储类:
(1)auto:所有局部变量默认的存储类,auto只能用在函数内,修饰局部变量。
(2)register:定义存储在寄存器中的局部变量,不能使用一元‘&’运算符(没有存储位置)。register意味着变量可能存于寄存器中,取决于硬件和实现的限制。
(3)static存储类:编译器在程序的生命周期中保持局部变量的存在,不需要每次进入和离开作用域时进行创建和销毁。static挂件子也可以应用于全局变量,当static修饰全局变量时,时变量作用域限制在生命它的**文件**内。举例:
#include <stdio.h>
/* 函数声明 */
//void func1(void);
static int count=10; /* 全局变量 - static 是默认的 */
void func1(void)
{
/* 'thingy' 是 'func1' 的局部变量 - 只初始化一次, 每次调用函数 'func1' 'thingy' 值不会被重置。
*/
static int thingy=5;
thingy++;
printf(" thingy 为 %d , count 为 %d\n", thingy, count);
}
int main()
{
while (count--) {
func1();
}
return 0;
}
//输出为
thingy 为 6 , count 为 9
thingy 为 7 , count 为 8
thingy 为 8 , count 为 7
thingy 为 9 , count 为 6
thingy 为 10 , count 为 5
thingy 为 11 , count 为 4
thingy 为 12 , count 为 3
thingy 为 13 , count 为 2
thingy 为 14 , count 为 1
thingy 为 15 , count 为 0
(4)extern存储类:提供一个全局变量的引用,对于无法初始化的变量指向之前定义过的存储位置。
3.符号:
(1)&:返回变量的地址,
(2)*:指向一个变量。
4.全局变量和局部变量:
局部变量被定义时,系统不会对其进行初始化,需要使用者自行进行初始化;全局变量则不同,系统会进行初始化操作。
更多推荐
菜鸟初学c和c++之基础知识(跟着菜鸟教程学)---(1)
发布评论