Returning a local static item in C

In C, the scope of a variable staticthrough a file. In the following code, the function returns a static variable.

int fun(){
    static int i = 10;
    return i;
}

int main() {
    printf("%d\n", fun());
    return 0;
}

And printed output 10.

So, returns local static to C behavior undefined or well-defined?

+1
source share
1 answer

It seems you missed all the logic for the instruction return.

In this snippet, you are actually returning the value (of the variable), so without storage the staticcode is fine.

, , . static, ( ) , . ,

  • , , malloc() family
  • , static.
+8

Source: https://habr.com/ru/post/1689243/


All Articles