zoukankan      html  css  js  c++  java
  • (C/C++ interview) Static 详解

    C Static

    http://stackoverflow.com/questions/572547/what-does-static-mean-in-a-c-program

    Static could be used for (1) variable and (2) function.

    1. A static variable inside a function keeps its value between invocations.(functions call).
    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
    ", 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.

    Short answer ... it depends.

    1. Static defined local variables do not lose their value between function calls. In other words they are global variables, but scoped to the local function they are defined in.

    2. Static global variables are not visible outside of the C file they are defined in.

    3. Static functions are not visible outside of the C file they are defined in.

    QUESTION - static variables are local in scope to the block or file in which they are defined, but their lifespan is throughout the program.

    -True 
    -False
    View Answer / Hide Answer

    ANSWER - True

    QUESTION - Which of the following is not correct for static variables?

    -The value of a static variable in a function is retained between repeated function calls to the same function.
    -static variables are allocated on the heap
    -static variables do not retain its old value between function calls 
    -The scope of static variable is local to the block in which it is defined.
    View Answer / Hide Answer

    ANSWER - static variables do not retain its old value between function calls
  • 相关阅读:
    Codeforces 385 D Bear and Floodlight
    Linux经常使用的命令(两)
    hadoop编程技巧(6)---处理大量的小型数据文件CombineFileInputFormat申请书
    android Notification分析—— 您可能会遇到各种问题
    人类探索地外文明的显著取得的进展
    腰带“兄弟”事实上,投资
    C++机器学习古典材料
    [Recompose] Render Nothing in Place of a Component using Recompose
    [Recompose] Replace a Component with Non-Optimal States using Recompose
    [Recompose] Show a Spinner While a Component is Loading using Recompose
  • 原文地址:https://www.cnblogs.com/fdyang/p/4502980.html
Copyright © 2011-2022 走看看