c语言静态函数调用静态变量

C中的静态变量 (Static Variables in C)

Basically, when static variables are declared, they create only a single copy of them and are also termed as Class Variables.

基本上,声明静态变量时,它们仅创建它们的单个副本 ,也称为类变量

They retain the value assigned in the corresponding scope and do not initialize the variables again in the new scope of it.

它们保留在相应范围内分配的值,并且不会在其新范围内再次初始化变量。

  • Static variables remain in the memory space during the execution of the code.静态变量在代码执行期间保留在内存空间中。
  • The default value of the initialization of the Static variable is zero (0).静态变量初始化的默认值为零(0)。
  • In C programming, it is mandatory for the static variables to be initialized using the constant literal else it returns an error.在C编程中,必须使用常量常量初始化静态变量,否则它将返回错误。

Syntax:

句法:


static Data_Type variable = value;

Example:

例:


#include<stdio.h>
void implement()
{ int x = 0;
static int counter = 0;
counter++;
x++;
printf("\nStatic Variable's value:\n");
printf("%d",counter);
printf("\nLocal Variable's value:\n");
printf("%d",x);
} int main()
{
implement();
implement();
return 0;
}

In the above code snippet, the value of the local variable gets initialized again after every function call.

在上面的代码片段中,每次调用函数后,都会再次初始化局部变量的值。

Unlike the local variable (x), the static variable (counter) was initialized only once and it did retain the same copy of variable throughout the execution of the program.

与局部变量(x)不同,静态变量(counter)仅初始化一次,并且在程序执行期间确实保留了相同的变量副本。

Output:

输出:


Static Variable's value:
1
Local Variable's value:
1
Static Variable's value:
2
Local Variable's value:
1


C中的静态函数 (Static Functions in C)

Static functions in C basically restrict the scope of the method to the corresponding file. These functions can also be called without having the object initialized.

C语言中的静态函数基本上将方法的范围限制为相应的文件 。 也可以在不初始化对象的情况下调用这些函数。

  • Static functions usually enhance the usability of the code i.e. makes the code ready to be reused again.静态函数通常会增强代码的可用性,即使代码准备好再次使用。
  • It limits the visibility of the scope of the function within that particular program.它限制了该特定程序内功能范围的可见性。

Syntax:

句法:


static Return_Type method_name(parameters)
{body;
}

Example:

例:


#include <stdio.h>//static function definition
static int sum(int a, int b){return (a+b);
}int main()
{int number1,number2;;printf("Enter the number1: ");scanf("%d",&number1);printf("Enter the number2: ");scanf("%d",&number2);printf("Addition of two numbers = ");int res = sum(number1,number2);printf("%d",res);return 0;
}

Output:

输出:


Enter the number1: 10
Enter the number2: 20
Addition of two numbers = 30


结论 (Conclusion)

Thus, in this article, we have understood the importance and behavior of static variables and static functions in the C programming language.

因此,在本文中,我们已经了解了C编程语言中静态变量和静态函数的重要性和行为。

翻译自: https://www.journaldev.com/35163/static-variables-functions-in-c

c语言静态函数调用静态变量

c语言静态函数调用静态变量_C语言中的静态变量和函数相关推荐

  1. @value 静态变量_C语言基本知识:变量

    变量其实只不过是程序可操作的存储区的名称.C 中每个变量都有特定的类型,类型决定了变量存储的大小和布局,该范围内的值都可以存储在内存中,运算符可应用于变量上. 变量的名称可以由字母.数字和下划线字符组 ...

  2. jstl动态取变量_C语言的变量名

    变量名是用来标识某个内存块的 地址就是地址啦,如是变量名的话,用取地址运算符&就可以得到它标识的内存块的地址, 而指针变量呢,它本身也是一个变量名,只不过它标识的那块内存存放的是一个地址值 变 ...

  3. c语言webbrowser加载html,向WebBrowser中添加静态HTML,执行脚本,载入HTML

    *使TWebBrowser中的复制.剪切操作有效: //把下面4行加到有WebBrowser的单元最后 { 可能需要引用 ActiveX } initialization OleInitialize( ...

  4. 语言const的生命周期_C语言的角落——这些C语言不常用的特性你知道吗?

    变长参数列表 头文件定义了一些宏,当函数参数未知时去获取函数的参数 变量:typedef va_list 宏: va_start() va_arg() va_end() va_list类型通过stda ...

  5. c语言局部变量存在什么区_C语言程序设计变量运用指南 C语言局部变量的存储方式和生存期...

    动态存储方式与静态存储方式 从变量的作用域(即从空间)的角度来观察,变量可以分为全局变量和局部变量.从变量值存在的时间(即生存期)来观察,有的变量在程序运行的整个过程都是存在的,而有的变量则是在调用其 ...

  6. vc 运行c语言步骤,第1章_C语言概述(vc++环境如何运行c语言程序)[精选].ppt

    第1章_C语言概述(vc环境如何运行c语言程序)[精选] C语言程序设计 授课对象:电子商务06 时间安排: 理论课(1-9周): 星期一 5-6节 9407D 星期三 5-6节 9410D 实验课( ...

  7. c语言代码大全表解释_C语言常用错误代码释义大全,值得收藏!

    对于刚学编程,刚接触C++的新手来说,编译运行报错是最头疼的一件事,爆出一堆英文,英语差一点的又不知道什么意思,所以也不知道如何去改,在此,我给大家传一份常见错误中英文对照表及简单解释,希望可以帮到大 ...

  8. 【C语言 MOOC】程序设计入门_C语言(翁恺)

    文章目录 程序设计入门-C语言(MOOC-翁恺) Week 1 程序设计与C语言 lesson 1 计算机和编程语言 辗转相除法 程序的执行 lesson 2 第一个程序 Week 2 计算 less ...

  9. java中的静态初始化是什么意思,Java中static静态变量的初始化完全解析

    静态变量初始化顺序 1.简单规则 首先先看一段最普遍的JAVA代码: ? 这里先猜下控制台输出结果是什么? OK, 或许你已经猜到下面了结果了,那么你还是熟悉Java的. 复制代码 代码如下: 0 1 ...

最新文章

  1. tree view 後台編寫
  2. SQLserver模糊查询
  3. 吴恩达《Machine Learning》精炼笔记 7:支持向量机 SVM
  4. windows2016安装.netFramework 3.5
  5. 理想汽车10月份新增12家直营交付中心 蔚来第1000座充电站上线
  6. python3操作excel(xls与xlsx版本的爱恨情仇)
  7. vue中的render函数介绍
  8. java web 调度_javaweb车辆调度信息管理平台
  9. 【企鹅电竞直播源】浏览器抓取真实直播源地址(纯前端JS PHP解析源码)
  10. 小米无线显示与电脑连接到服务器,小米手机和电脑进行怎么无线连接
  11. 写给三维建模入门小白的建议
  12. h5优秀控件_7个效果震憾的HTML5应用组件
  13. Python报错too many values to unpack解决方案
  14. 推荐一个免费的生成词云(word cloud)的在线工具
  15. 生信小白学习日记Day2——NGS基础 illumina高通量测序原理
  16. 学习前端-微信小程序
  17. 苹果手机丢了如何通过定位找回?iPhone手机丢失定位找回方法
  18. SRIO学习(二)——SRIO结构
  19. 离散数学——命题逻辑(命题、联结词、命题公式、对偶式、大小项、主范式、推论、直接证明、反证法、cp规则)
  20. 从《福布斯》发布的2011年度的全球富豪榜中看到:中国太需要“互联网精神”...

热门文章

  1. C++ 内存分配 学习笔记
  2. enableEventValidation是干什么的?
  3. 设计模式--适配器1模式实现C++
  4. [转载] 利用c/c++编写python模块
  5. [转载] 夯实Java基础系列8:深入理解Java内部类及其实现原理
  6. 详解k8s一个完整的监控方案(Heapster+Grafana+InfluxDB) - kubernetes
  7. 【luogu】P1772物流运输(最短路+DP)
  8. win10 64位 安装TensorFlow
  9. 题目1008:最短路径问题(SPFA算法)
  10. 基础学习day03---程序结构与控制、函数与数组入门