A simple but can examine C language-based topic

Look at the title:

#include<stdio.h>
int a=1;
int main(void)
{
    int a=a;
    printf("a=d%\n",a);
    return 0;
}

The problem is simple, just ask what outputs? This is nothing more than a few answers: 1,0, error, random number. That really is how much? Of course, we declare the actual work would be so few people write, we are mainly borrowed this question to tell you some knowledge of C, in fact, knowledge is the main question relates to the issue of global and local variables, that is, variable scope problem.
First, a simple analysis: the beginning of a global variable declaration and assignment, this time variables have a global scope, here there have been two cases: First, if a variable declaration does not appear in the following function, but using a variable, which is a global variable legally valid, at this time is that we start a statement, the value is 1; if the second case is again in the following function in a statement to declare variables, but also this is the case in question, that at this time just a beginning of a global variable is not the same, this is a either-a, you can understand it as a new variable, but it also happens with the letter a representation only.
Here we should understand that, in the main function again declares a new variable a, this time not initialize variables, and then assign a value of the course is random (There seems to be an error in only vs, tips use uninitialized variables a), one more thing to note: Some people say that is not initialized to zero it is not the default, so the result is output to 0, and remind you that only global variables and static variables are not initialized will be assigned the value 0, BSS segment on the other uninitialized value is random, but a great probability is 0, the answer to this question is a random value, if the change static int a = a, the answer is 0 a.

Guess you like

Origin www.cnblogs.com/Hijack-you/p/11960800.html