在一个文件中声明并在另一个文件中访问它的全局静态变量

时间:2018-09-26 06:45:48

标签: c

headers.h

#include<stdio.h>
#include<stdlib.h>
static int static_val=10;

main.c

#include "headers.h"
int main()
{
static_val += 10;
if (static_val <= 100)
{
printf("main called\n");
printf("%d\n",static_val);
main();
}
else
printf("stopped calling 'main', with final value = %d\n",static_val);

function1();
}

file.c

#include "headers1.h"

void function1(void)
{
static int inc;
inc++;
printf("*************\n");
printf("function1 called %d time.\n",inc);
printf("%d\n",static_val);
printf("Initial Value = %d\n",static_val);
static_val += 20;
printf("%d\n",static_val);
printf("**************\n");
}

说明:

  • 因此,我已经全局声明了静态变量并将其放在 单独的头文件,我想在两个文件中都使用它, 我这样做只是为了了解存储类。
  • 主叫递归调用直到条件为真,并且 一旦条件失败,最后一个主调用“ function1”,并且 一旦调用function1,我的理解是该功能 将采用静态变量的最后更新值,但是 不会发生,而是以新的声明开始,为什么会这样?
  • 注意:执行程序并查看输出以得到更好的结果 了解我的问题。

0 个答案:

没有答案
相关问题