1.main()
2.{
3. int a=0;
4. while(...)
5. {
6. int a=9;
7. }
8.}

now friendz question is
question is what make a of 3 global and a of line 6 local what is happend at the time of local decla.. and at the time of global decl...

would u plz help me to understand this...

Dani AI

Generated

Short answer: neither a in the snippets is global. The outer a is local to main (it has block scope for the main function); the inner a is local to the loop block and shadows the outer name while that block is active. The inner variable is created and initialized each time execution reaches its declaration and is destroyed when the block exits; the outer variable keeps its value across the block boundary.

was right to say these are not global, and is correct about the inner declaration taking precedence inside the loop. If observed the outer value changing after the loop, the usual causes are: the inner declaration was accidentally omitted (so the code was assigning to the outer variable), braces/scoping were different than expected, or the test prints were placed where they showed the assigned value. To debug, print the value before, inside, and after the block and compile with warnings that catch shadowing (for GCC/Clang use -Wshadow).

A minimal reproducible test:

#include <stdio.h>

int main(void) {
    int x = 1;
    printf("outer before: %d\n", x);
    for (int i = 0; i < 1; ++i) {
        int x = 2; /* shadows outer x */
        printf("inside: %d\n", x);
    }
    printf("outer after: %d\n", x);
    return 0;
}

Compile with gcc -std=c99 -Wall -Wextra -Wshadow test.c -o test and run; expected output shows inside different from outer after. For authoritative details on scope and storage duration see C: scope and C: storage duration.

Recommended Answers

All 9 Replies

1.main()
2.{
3. int a=0;
4. while(...)
5. {
6. int a=9;
7. }
8.}

now friendz question is
question is what make a of 3 global and a of line 6 local what is happend at the time of local decla.. and at the time of global decl...

would u plz help me to understand this...

hi there,
i don't know if i understood your question...
if you want to know how those variables are stored in memory
well, in my opinion, none of them are global. the one declared in line 3 is local to main function, and resides on the respective stack, and the one declared in the while loop, should be put in a register, because it is accessed very often, and the compiler can see this.

1.main()
2.{
3. int a=0;
4. while(...)
5. {
6. int a=9;
7. }
8.}

now friendz question is
question is what make a of 3 global and a of line 6 local what is happend at the time of local decla.. and at the time of global decl...

would u plz help me to understand this...

i think a in 3rd line is not global its local to the main function and if you change the value of a in the 6th line or ne where inside the program it will display the new value...

Use code tags :D

i think a in 3rd line is not global its local to the main function and if you change the value of a in the 6th line or ne where inside the program it will display the new value...

ABSOLUTELY WRONG !!!

a on line 3 is local to main(), while the scope of a in line 6 is limited to the while loop. Inside the while loop, the most local variable is given preference. And if you change a inside the while loop, the value of a on line 3 will remain unaffected. Try it out by using printf statements in between.

jishnu is right :D

i tried it but not working as u r saying...!!!!!!!!!

but the updation is done in the while loop and so the value of a will be the new value even if we print the value after the termination of the loop.......:-/

How about posting your latest code instead of just moaning it doesn't work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.