Initialise a variable to its own undefined value

前端 未结 4 718
你的背包
你的背包 2021-02-04 01:50

In C, does initialising a variable to it\'s own value make sense? If yes, what for?

Allow me to elaborate. In Git sources there are some examples of initialising a varia

4条回答
  •  無奈伤痛
    2021-02-04 02:45

    On MacOS X 10.7.2, I tried this example - with the result shown...

    $ cat x3.c
    #include 
    
    int status = -7;
    
    int main()
    {
        printf("status = %d\n", status);
        int status = status;
        printf("status = %d\n", status);
        return 0;
    }
    $ make x3
    gcc -O -std=c99 -Wall -Wextra  x3.c -o x3  
    $ ./x3
    status = -7
    status = 1787486824
    $
    

    The stack space where the local status in main() has been used by printf() so the self-initialization copies garbage around.

提交回复
热议问题