菜鸟初学c和c++之基础知识(跟着菜鸟教程学)---(1)

编程知识 行业动态 更新时间:2024-06-13 00:22:04

//仅适用于超级菜鸟及初初初初学者,共同学习,高手请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)

本文发布于:2023-04-01 16:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/6720d5b00ae908133a9229a94a6f13ed.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:菜鸟   基础知识   教程

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!