2

I have a C code snippet as follows:

const int x = 5;
void main()
{
    int x[x];
    int y = sizeof(x) / sizeof(int);
    printf("%d",y);
}

The code snippet would be compiled and run correctly. But I don't understand how to differentiate x 'variable' and x 'const'.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
duong_dajgja
  • 4,196
  • 1
  • 38
  • 65
  • 1
    Why don't you save yourself some grief and use different, and meaningful, identifiers? The standard dictates what should happen if two of the same name are within scope. – Weather Vane Mar 18 '15 at 19:02
  • It's not the problem in coding style, just a question to strengthen my knowledge :) – duong_dajgja Mar 18 '15 at 19:04
  • 1
    There is no syntax in C that allows you to access the `const int x`, after declaring `x[]` in `main`. The declaration of `x[]` hides the `const int x` as long as `x[]` is in scope, which would be all of `main` in this example. – user3386109 Mar 18 '15 at 19:06
  • 1
    Please note `int main` is standard not `void main`. – Shafik Yaghmour Mar 18 '15 at 19:25
  • @ShafikYaghmour: Specifically `int main(void)` in C, or `int main()` in C++. – Keith Thompson Mar 19 '15 at 03:08

1 Answers1

9

For C++ this is covered in the draft C++ standard section 3.3.2 Point of declaration:

The point of declaration for a name is immediately after its complete declarator (Clause 8) and before its initializer (if any), except as noted below. [ Example:

int x = 12;
{ int x = x; }

Here the second x is initialized with its own (indeterminate) value. —end example ]

and:

[ Note: a name from an outer scope remains visible up to the point of declaration of the name that hides it.[ Example:

const int i = 2;
{ int i[i]; }

declares a block-scope array of two integers. —end example ] —end note ]

So in your case:

int x[x];

The const int x is visible until the closing ]. To refer to const int x after that point you can use a qualified identifer:

::x

Of course this begs the question, why not just use different names and not have to deal with these edge cases?

C

The equivalent quotes form the draft C99 standard would be from section 6.2.1 Scopes of identifiers (emphasis mine):

Structure, union, and enumeration tags have scope that begins just after the appearance of the tag in a type specifier that declares the tag. Each enumeration constant has scope that begins just after the appearance of its defining enumerator in an enumerator list. Any other identifier has scope that begins just after the completion of its declarator.

and:

[...] 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.

there is no way to make the x in the outer scope visible in C.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • so, let say I define a variable like this: int x = x; after that, when I use y = x; what would be x here? – duong_dajgja Mar 18 '15 at 19:03
  • @duong_dajgja that would be undefined behavior since the value of `x` is indeterminate in that case: see [Has C++ standard changed with respect to the use of indeterminate values and undefined behavior in C++14?](http://stackoverflow.com/q/23415661/1708801) ... It is also [undefined behavior in C as well](http://stackoverflow.com/q/22416319/1708801) – Shafik Yaghmour Mar 18 '15 at 19:06