Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this give compilation error? [duplicate]

Tags:

c++

c

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 .

like image 667
rforritz Avatar asked Jul 12 '13 05:07

rforritz


2 Answers

§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 ]

like image 160
Jerry Coffin Avatar answered Nov 04 '22 09:11

Jerry Coffin


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.

like image 32
akalenuk Avatar answered Nov 04 '22 11:11

akalenuk