1

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.

beastboy
  • 157
  • 1
  • 3
  • 11

2 Answers2

2
    { 
        // 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.
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Why i am getting different result in both the codes int y = 40 , is also like assigning a new value to variable y again. – beastboy Oct 24 '18 at 12:34
  • Like I said, you're simply assigning to `y`; you're just changing its value. There's no new variable. That's why you see the change in the outer `printf`. – Jonathon Reinhart Oct 24 '18 at 12:37
  • If int y is just a declaration then why as per https://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration int y; is a definition and not declaration – beastboy Oct 24 '18 at 12:40
  • It would be better to use word *definition* rather than *declaration* when speaking of local variables. It's impossible *declare* local variable without also *defining* it. – user694733 Oct 24 '18 at 12:40
1

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. I.e.:

//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.

absoluteAquarian
  • 510
  • 3
  • 12
  • Very Thanks Tau for the nice explanation.. have one more scenario , what if i declare and initializes int y = 5 outside the main , it will be like a global then – beastboy Oct 25 '18 at 04:06
  • ` int y=5; int main() { { int x = 10; int y = 20; { printf("x = %d, y = %d\n", x, y); { int y; x++; y++; printf("x = %d, y = %d\n", x, y); } printf("x = %d, y = %d\n", x, y); } } return 0; } ` I am getting the below result ` x = 10, y = 20 x = 11, y = 32765 **x** = 11, y = 20 ` – beastboy Oct 25 '18 at 04:10
  • @beastboy make sure to give the inner `y` a value before you use it. otherwise, you'll get **Undefined Behaviour** since the inner `y` will have garbage data in it before you use it. – absoluteAquarian Oct 25 '18 at 13:18
  • and yes, if you declare and instantiate `y` outside of `main()`, then it will be global. – absoluteAquarian Oct 25 '18 at 13:19