6
int a = a ;  

According to the rule of assignment operator it should read the line from right to left. After seeing 'a' undeclared compiler should give compilation error.
But it is giving garbage value . Please Clarify it .

rforritz
  • 253
  • 1
  • 6
  • Wouldn't it actually allow `a` to be declared - with garbage - and then presumably print that garbage? Also based on your logic nothing would be able to be assigned in a declaration. `int a = 1;` would fail because the assignment "reads right to left" and therefore `a` is undeclared; no? – ChiefTwoPencils Jul 12 '13 at 05:24
  • 3
    it is the same garbage value that was in `a` to begin with since you never initialized `a`. – dandan78 Jul 12 '13 at 05:25
  • possible duplicate of [Is self-initialization 'A a = a;' allowed?](http://stackoverflow.com/questions/981714/is-self-initialization-a-a-a-allowed) Also here: http://stackoverflow.com/questions/3173462/int-var-1-void-main-int-i-i – jogojapan Jul 12 '13 at 05:26
  • 1
    @jagojapan : well my question is that why doesn't this give compilation error. Since 'a' is undeclared at time of assigning.But it is still unanswered :( – rforritz Jul 12 '13 at 05:43
  • @rforritz I think you like to read this short note: [Point of Declaration](http://msdn.microsoft.com/en-us/library/179x7xb9%28v=vs.80%29.aspx) to understand why compiler don't give error. – Grijesh Chauhan Jul 12 '13 at 05:51
  • This probably makes more sense as a duplicate of [Does initialization entail lvalue-to-rvalue conversion? Is `int x = x;` UB?](http://stackoverflow.com/questions/14935722/does-initialization-entail-lvalue-to-rvalue-conversion-is-int-x-x-ub) or [int var = 1; void main() { int i = i; }](http://stackoverflow.com/questions/3173462/int-var-1-void-main-int-i-i) – Shafik Yaghmour May 06 '14 at 01:28

2 Answers2

10

§3.3.2/1:

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 ]

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Note, in the latest C++1y draft [this has changed](http://stackoverflow.com/questions/23415661/has-c-standard-changed-with-respect-to-the-use-of-indeterminate-values-and-und). – Shafik Yaghmour May 05 '14 at 02:53
2

Actually, compiler might give you a hint. Mine says: "warning C4700: local variable 'a' used without having been initialized".

But this is not an error per se, declaration just gives a variable name to some bits of memory without touching it, which is valid and fast. And the assigment here is not really an assigment, just moving bits from right to left. No checks again. Very productive, very unsafe.

Every operation is legal, but the whole thing is pointless. So compiler does the best thing it can - it compiles the code, but gives a warning too.

akalenuk
  • 3,815
  • 4
  • 34
  • 56
  • 1
    Initialization does not "just give a variable name to some bits of memory without touching it". _Declaration_ does that. Initialization is the step where you provide that named bit of memory with an initial value. – This isn't my real name Jul 12 '13 at 17:24
  • Yes, exactly. But in this case there's basically no initialization. What seems to be one is just a declaration and assignment. – akalenuk Jul 12 '13 at 21:17
  • Oh, I probalby should edit my answer to avoid confusion. Thanks! – akalenuk Jul 12 '13 at 21:20