I had a doubt that there is something i am missing seeking for the difference between Declaration and Definition and i found the link https://www.geeksforgeeks.org/commonly-asked-c-programming-interview-questions-set-1/ Its stated here that
// This is only declaration. y is not allocated memory by this statement
extern int y;
// This is both declaration and definition, memory to x is allocated by this statement.
int x;
Now if I go by the below piece of code
int main()
{
{
int x = 10;
int y = 20;
{
// The outer block contains declaration of x and y, so
// following statement is valid and prints 10 and 20
printf("x = %d, y = %d\n", x, y);
{
// y is declared again, so outer block y is not accessible
// in this block
int y = 40;
x++; // Changes the outer block variable x to 11
y++; // Changes this block's variable y to 41
printf("x = %d, y = %d\n", x, y);
}
// This statement accesses only outer block's variables
printf("x = %d, y = %d\n", x, y);
}
}
return 0;
}
I will get the below result
x = 10, y = 20
x = 11, y = 41
x = 11, y = 20
If i modify just the int y = 40 in the innermost block to y = 40 then the code will look like
//int y;
int main()
{
{
int x = 10;
int y = 20;
{
// The outer block contains declaration of x and y, so
// following statement is valid and prints 10 and 20
printf("x = %d, y = %d\n", x, y);
{
// y is declared again, so outer block y is not accessible
// in this block
y = 40;
x++; // Changes the outer block variable x to 11
y++; // Changes this block's variable y to 41
printf("x = %d, y = %d\n", x, y);
}
// This statement accesses only outer block's variables
printf("x = %d, y = %d\n", x, y);
}
}
return 0;
}
and the result will be
x = 10, y = 20
x = 11, y = 41
x = 11, y = 41
My friend told me this is because we are declaring a new y which is local to the block in the first code and that is not in the second case , i didn't get why as we are only writing the data type in front of the variable a second time , does this mean by writing data type we reserved a new memory space and created a new variable , please explain .
If i go by another article on Stackoverflow on the Link What is the difference between a definition and a declaration?
i see that whenever we say that we are Declaring a Variable, the variable is preceded by the extern keyword and i am speaking strictly related to C and not any other language.
So can we generalize to Declaration of variable as preceded by extern Keyword.
I understand my English might be bad and difficult for you to understand , please bear with me.
{
// y is declared again, so outer block y is not accessible
// in this block
y = 40;
Your comment is wrong. This is not declaring y
again. It's simply assigning to it.
It should instead say:
// This changes the value of y in the outer block
does this mean by writing data type we reserved a new memory space and created a new variable
Yes.
int y
is a declaration of a new variable. int y = 40
is a declaration and initialization. y = 40
is an assignment to an existing variable. extern int y;
tells the compiler that y
may be declared in another file that is linked to this program, be it library, header file, et cetera, and that if the compiler finds that global variable called y
, then it should use its memory address for the y
declared in this file. Ie:
//Look for a global variable called "y" in the included files
//If it is found, use its memory address
extern int y;
Now, for your first code example...
First, you declare and instantiate two variables, x
and y
.
int x = 10;
int y = 20;
Then you print them, resulting in x = 10, y = 20
being printed. This makes sense, since x
is 10 and y
is 20.
Then, you create a new scope using curly brackets and declare a new variable called y
. Since this variable has the same name as a variable in a scope higher than it, it hides the outer variable called y
until this scope is exited.
{
int y = 40;
Any changes to this y
will not affect the outer y
, and the outer y
cannot be accessed until the scope of this y
has been exited.
Then you increment x
and y
, and then print the result. x = 11, y = 41
is printed due to the above behaviour.
After printing the above string, the code exits the current scope. Because of this, the outer y
is no longer hidden, and it can now be accessed.
Finally, you print x
and y
again. x
is 11 because it was incremented earlier, but the outer y
was not incremented , thus x = 11, y = 20
is printed.
Replacing int y = 40
with y = 40
like you did in your second code example results in only one y
variable being declared and instantiated. Therefore, y
ends up being 41 at the third printf()
statement.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.