1
#include <stdio.h>

int main () {
    int i = 3; 
    printf("%d\n", i);
    if (i == 3) {
        i = i + 1;
        int i = 6; 
        printf("%d\n", i);
        i = i + 1;
    }

    if (i == 3) {
        printf("%d\n", i); 
    }
    return 0;
}

My Question is Why the 4 and 7 disappear?

When I run the code they only print the output 3 and 6 ?

4
  • 2
    The first i = i + 1 increments the outer i, so the second if is never entered. Commented May 10, 2017 at 11:03
  • Which line you expect should print 7? Maybe you missed one more printf("%d\n", i); after second i = i + 1;? Commented May 10, 2017 at 11:03
  • 1
    You only have three printfs so you can't expect four numbers to be printed. And it's YOUR code, YOU can debug it. Hint: use a debugger! Commented May 10, 2017 at 11:05
  • Probably you have warning messages from the compiler - don't ignore them! Commented May 10, 2017 at 12:00

2 Answers 2

9

Nothing here "disappears", this happens because, the inner scope takes precedence over the outer scope for identifiers with overlapping scope.

Quoting C11, chapter §6.2.1 (emphasis mine)

[...] If an identifier designates two different entities in the same name space, the scopes might overlap. If so, the scope of one entity (the inner scope) will end strictly before the scope of the other entity (the outer scope). Within the inner scope, the identifier designates the entity declared in the inner scope; the entity declared in the outer scope is hidden (and not visible) within the inner scope.

So, in your case, (follow the comments)

#include <stdio.h>

int main (void) {   //note the correct signature
    int i = 3;            //outer scope of i
    printf("%d\n", i);
    if (i == 3) {
        i = i + 1;         //this happens in "outer" scope, i value changed...
        //--------------------->     |-----inner scope starts, outer scope gets hidden
        int i = 6;                // |
        printf("%d\n", i);        // |
        i = i + 1;                // |
    }//------------------------>     |-----inner scope ends, outer scope resumes

    if (i == 3) {                 // hey. look , other scope is back!!!
        printf("%d\n", i); 
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

5 Comments

OMG THANK YOU, now i know where is the wrong haha and finally understand~ but why the "Other scope" cannot output the number of 3 ? Cus the "Other Scope" now is outer scope already right ? so it should print 3 ?
int main () and int main (void) are different ? void means nothing right ?
@ThomasLeong it outputs 3, as you said...3 (from outer) and 6 (from inner), right?
@ThomasLeong no, they are not the same, at least not in this case. () means no constraint on number/type of args, but (void) means explicitly none.
Now lets say the code have 3 printf. The First print output 3, the second print output 6, and the Third print nothing ? @@
1

The output is so because the local variables have a greater preference over the global variables or variables declared not in the same scope. Each time you initialize an existing variable with a value in a scope, the value becomes dominant in its scope. This similar situation in java is referred to as "Namespace Collision". It is to be noted here that the c complier allows auto storage class as default storage class which allows you to initialize again.I am referring to int i=6. However, using static storage class won't allow you to initialize the same variable again !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.