&是什么QUOT;静态"在C程序是什么意思? [英] What does "static" mean in a C program?

查看:166
本文介绍了&是什么QUOT;静态"在C程序是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过的字静态在用C code不同的地方使用;这是像在C#中(其中实现跨对象共享)静态函数/类?


解决方案

  1. 在函数内部静态变量保持调用之间的值。

  2. 静态全局变量或函数被看到只有在它在声明的文件

(1)为更多的外国的话题,如果你是一个新手,所以这里是一个例子:

 的#include<&stdio.h中GT;无效美孚()
{
    INT A = 10;
    静态INT SA = 10;    A + = 5;
    SA + = 5;    输出(A =%d个,SA =%d个\\ N,一,SA);
}
诠释的main()
{
    INT I;    对于(I = 0; I&小于10 ++ⅰ)
        富();
}

这将打印:

  A = 15,SA = 15
一个= 15,SA = 20
一个= 15,SA = 25
一个= 15,SA = 30
一个= 15,SA = 35
一个= 15,SA = 40
一个= 15,SA = 45
一个= 15,SA = 50
一个= 15,SA = 55
一个= 15,SA = 60

这是哪里的函数需要保持调用之间的一些国家的情况下很有用,你不希望使用全局变量。但请注意,这个功能应该非常谨慎使用 - 它使你的code不是线程安全的,更难理解

(2)被广泛用作访问控制功能。如果你有一个.c文件实现一些功能,它通常只公开了一些公的功能给用户。它的功能,其余应静态,这样用户将无法访问它们。这是封装,一个很好的做法。


  

在C语言编程,静
  用于与全局变量和
  功能在其范围设定为
  包含文件。在局部变量,
  静态用于存储可变
  在静态分配的内存
  代替自动分配
  记忆。虽然语言并不
  规定的实施要么
  内存类型,静态分配
  内存通常被保留在数据
  在编译的程序段
  时间,而自动
  分配的内存通常
  作为一个短暂的调用堆栈实现的。


这里请参见和的这里了解更多详情。

和回答你的第二个问题,它不是在C#一样。

在C ++中,然而,静态也可以用来定义类的属性和方法(同一类的所有对象之间共享)。在C语言中有没有课,所以这个功能是无关紧要的。

I've seen the word static used in different places in C code; is this like a static function/class in C# (where the implementation is shared across objects)?

解决方案

  1. A static variable inside a function keeps its value between invocations.
  2. A static global variable or a function is "seen" only in the file it's declared in

(1) is the more foreign topic if you're a newbie, so here's an example:

#include <stdio.h>

void foo()
{
    int a = 10;
    static int sa = 10;

    a += 5;
    sa += 5;

    printf("a = %d, sa = %d\n", a, sa);
}


int main()
{
    int i;

    for (i = 0; i < 10; ++i)
        foo();
}

This prints:

a = 15, sa = 15
a = 15, sa = 20
a = 15, sa = 25
a = 15, sa = 30
a = 15, sa = 35
a = 15, sa = 40
a = 15, sa = 45
a = 15, sa = 50
a = 15, sa = 55
a = 15, sa = 60

This is useful for cases where a function needs to keep some state between invocations, and you don't want to use global variables. Beware, however, this feature should be used very sparingly - it makes your code not thread-safe and harder to understand.

(2) Is used widely as an "access control" feature. If you have a .c file implementing some functionality, it usually exposes only a few "public" functions to users. The rest of its functions should be made static, so that the user won't be able to access them. This is encapsulation, a good practice.

Quoting Wikipedia:

In the C programming language, static is used with global variables and functions to set their scope to the containing file. In local variables, static is used to store the variable in the statically allocated memory instead of the automatically allocated memory. While the language does not dictate the implementation of either type of memory, statically allocated memory is typically reserved in data segment of the program at compile time, while the automatically allocated memory is normally implemented as a transient call stack.

See here and here for more details.

And to answer your second question, it's not like in C#.

In C++, however, static is also used to define class attributes (shared between all objects of the same class) and methods. In C there are no classes, so this feature is irrelevant.

这篇关于&是什么QUOT;静态&QUOT;在C程序是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆